php$ link and page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
scifirocket
Forum Commoner
Posts: 31
Joined: Fri Aug 13, 2010 1:24 am

php$ link and page

Post 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).
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: php$ link and page

Post 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.
scifirocket
Forum Commoner
Posts: 31
Joined: Fri Aug 13, 2010 1:24 am

Re: php$ link and page

Post 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
Last edited by scifirocket on Sat Aug 14, 2010 1:12 am, edited 1 time in total.
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: php$ link and page

Post 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.
Post Reply