PHP search box, output as link

Hi, ive been using the following pieces of code to search my database and then output the search results underneath, however i cant seem to make the result it outputs become a link? how would i go about doing this, would i need to change it in the database? At the moment it searches the database for the title column and display anything with a matching title, but i want it to then make what it outputs a link, would this mean me having to alter something in the database to make a connection between titles and links to another page?

[php]

<?php $db_hostname = 'localhost'; $db_username = 'root'; $db_password = 'password'; $db_database = 'days'; // Database Connection String $con = mysql_connect($db_hostname,$db_username,$db_password); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db($db_database, $con); ?> Search:
<?php if (!empty($_REQUEST['term'])) {

$term = mysql_real_escape_string($_REQUEST[‘term’]);

$sql = “SELECT * FROM adrenaline WHERE title LIKE '%”.$term."%’";
$r_query = mysql_query($sql);

while ($row = mysql_fetch_array($r_query)){
echo ’
'.$row[‘title’];

}

}
?>[/php]

many thanks.

What’s the database field hold? Is it a web address, a page in your directory?

to echo it as a link just wrap in an a tag and then you can use $_GET to query your database against the desired title.

[php]
while ($row = mysql_fetch_array($r_query)){
echo ’
‘. $row[‘title’] .’;
}
[/php]

then in pagename.php

[php]
$sql = “SELECT * FROM adrenaline WHERE title = '”.$_GET[‘title’]."’ ";
[/php]

if you need a better example let me know, but first try that out and make sure to change ‘pagename.php’ to whatever you want the page name to be.

Also, you should not use mysql as it is deprecated. You can easily make the switch to mysqli without much hassle.

Hi thanks for the quick reply, but im having some problems with this, im knew to php an im sturggling to get the code to posted to work, there was some errors in it i think so i had to remove the end bit of it, it now looks like this

[php]while ($row = mysql_fetch_array($r_query)){

'. $row[‘title’];

} [/php]

Im not sure where to go from there, i created a test page called pagename.php and posted the code you said to at the top in php brackets. but when i test it it displays no results where as before it would display all titles with a match to the search results. Im not sure if ive set this up wrong, but I want for it to search the titles in my database then find any matches and display the title along with a link to the webpage it corresponds to, here is how my database is currently set up:

http://tinypic.com/r/1zbz47s/5

Im not sure if i need another collum with links in or what? like I say im knew to all this and am not sure how to go about doing it, hope you can help :slight_smile:

p.s: this is a screenshot of it working without the links:

http://tinypic.com/r/nv4air/5

Bill.

Sorry about that, I was missing a quote after

It should be this…
[php]
while ($row = mysql_fetch_array($r_query)){
echo ’
‘. $row[‘title’] .’’;
}
[/php]

The code I posted for pagename.php was not complete, what I gave was just the query to use.
The complete code would look like this…

[php]
$sql = “SELECT * FROM adrenaline WHERE title = '”.$_GET[‘title’]."’ ";
$r_query = mysql_fetch_object( $sql );

echo 'Title: '. $r_query->title .‘
Description: ‘. $r_query->description .’
’;
[/php]

After some thought, you should really pass the id that belongs to the title so you don’t have the possibility o retrieving more that one result (there may be two rows or more that share the same title).
So after fetching the results from the search terms your link would look like this…
[php]
while ($row = mysql_fetch_array($r_query)){
echo ’
‘. $row[‘title’] .’’;
}
[/php]

I have not tested this code, if there are errors please let me know so I can help you out!

Hi, thanks alot the code seems to working good now, it displays them as links, however when i click the link it crashes with:

Notice: Undefined index: title in C:\xampp\htdocs\pagename.php on line 8

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\pagename.php on line 9

Notice: Trying to get property of non-object in C:\xampp\htdocs\pagename.php on line 11

Notice: Trying to get property of non-object in C:\xampp\htdocs\pagename.php on line 11
Title:
Description:

I think this is as I Havant set where i want to links to go to, where can i set this? sorry if its something obvious, im not the best at this sort of thing, im learning slowly.

Many thanks
Bill.

Try changing the query to you fetch_array instead of fetch_object then use your while loop to display the results like you did with showing the links.

dosnt seem to make any difference ;/, im sure its because i have not specified any where what for it to link to? but you know more than me, this is driving me nuts thought been trying to crack it for days now. it all works fine but as soon as i click the links it just gives me them errors i posted before?

I think i might try and get someone on teamviewer or skype to help me fix it. Its starting to give me headache now

thanks.

You have never answered what the database field is holding. It does make a difference when trying to link to it.

Hi, sorry i posted a link before of an image of my database, showing all fields.

http://tinypic.com/r/1zbz47s/5

Bill.

[php]

<?php #using what mdahlke provided while ($row = mysql_fetch_row($r_query)){ echo '
'. $row['title'] .''; } ?>

[/php]

Try this. It assumes you are using the script outside of the root directory, one level up. To change that you need to modify the directory preceding the $row array.

that seems to break it completely it now wont even output any search results ;/.

Notice: Undefined index: image_path in C:\xampp\htdocs\main.php on line 799

Notice: Undefined index: title in C:\xampp\htdocs\main.php on line 799

this seems to be so much hassle for such simple thing, there must be something simple im missing out

bill.

Sorry, should have been

mysql_fetch_array

AHA progress it seems to work now ;D , the only snag is that it links to images and not the pages? so can i fix that by placing a link in my database?

Yes , ive got it to work now, by placing links in my database, thanks so much all of you guys for your help!

That’s why it is important to know what you are trying to get out of the database.

How are the pages named? or are they dynamic?

If the name of the page is the same, you can use the same method. If it is a dynamic page it’s a little different.

Sponsor our Newsletter | Privacy Policy | Terms of Service