Page 1 of 1
php$ link and page
Posted: Fri Aug 13, 2010 6:54 pm
by scifirocket
I have a implemented a search (search.php) that selects elements of a database and returns a list of links in the format of search.php?id=1. I'm new to php and mysql, so I was wondering what i needed to do to develop the "id=some_number" page specifically for each result . (id=1 refers to the primary key in the sql database).
Re: php$ link and page
Posted: Fri Aug 13, 2010 10:50 pm
by requinix
Use $_GET["id"] to get the ID number from the URL. After making sure it's a number, look in the database for whatever it corresponds to.
Re: php$ link and page
Posted: Fri Aug 13, 2010 11:36 pm
by scifirocket
Code: Select all
<?php
$val = $_GET['id'];
//connect to the database
$db=mysql_connect ("doop", "deep", "boop-") or die ('I cannot connect to the database because: ' . mysql_error());
//-select the database to use
$mydb=mysql_select_db("something");
//-query the database table
$sql="SELECT ID, name FROM Foodlist WHERE ID=$val;
//-run the query against the mysql query function
$result=mysql_query($sql);
//-create while loop and loop through result set
while($row=mysql_fetch_array($result)){
(line 31) $name =$row['name'];
//-display the result of the array
echo "<p>" .$name . "</p>";
}
?>
i thought something like this would work using that variable, but i get this error message:
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/eatade5/public_html/result.php on line 31
Re: php$ link and page
Posted: Fri Aug 13, 2010 11:48 pm
by shawngoldw
You missed the " at the end of the sql statement.
Also you don't need to select ID if you're also putting where ID=id, you clearly already know the ID.
You should also make sure that $_GET['id'] is a number and use mysql_real_escape_string on it, just to be safe.