1

Trying to do some simple PHP coding to start on my project. I am trying to get database info by the url entered, but when i type the valid test url, it returns nothing. It outputs as:

ID: DISCUSSION NAME:

And right next to those, they should have the value from the database. But they don't. So can anyone help me?

$getdata = mysql_query("SELECT * FROM discussions WHERE urlname ='" . $_SERVER["REQUEST_URI"] . "'") or die(mysql_error());
 while ($info = mysql_fetch_array( $getdata ));
{
 Print "<tr>"; 
 Print "<th>ID: </th> <td>".$info['id'] . "</td> "; 
 Print "<th>DISCUSSION NAME:</th> <td>".$info['discussion'] . "</td> "; 
}

MY DATABASE INFO: https://i.sstatic.net/b89I8.png

enter image description here

6
  • Does $_SERVER["REQUEST_URI"] have a trailing slash? Also, this is a huge security hole :) Try using mysql_real_escape_string() around your server call Commented Jun 1, 2013 at 1:02
  • Thank you for reminding me that, this is just a test script so no worries :) and yes, it does have a trailing slash Commented Jun 1, 2013 at 1:03
  • 1
    Well there's your problem! The urlname field is just /test1 instead of /test1/. Try editing it to that and see if it works. Either that or ...WHERE urlname ='" . $_SERVER["REQUEST_URI"] . "/...' Commented Jun 1, 2013 at 1:05
  • what does var_dump($info); in the whole loop show ? Commented Jun 1, 2013 at 1:06
  • 1
    Just wanted to say it's nice of you actually post the database info and the code. It makes it easier for people to quickly spot the problem. Commented Jun 1, 2013 at 1:07

1 Answer 1

2

your code has a bad semi colon

$getdata = mysql_query("SELECT * FROM discussions WHERE urlname ='" . $_SERVER["REQUEST_URI"] . "'") or die(mysql_error());
 while ($info = mysql_fetch_array( $getdata ));
{
 Print "<tr>"; 
 Print "<th>ID: </th> <td>".$info['id'] . "</td> "; 
 Print "<th>DISCUSSION NAME:</th> <td>".$info['discussion'] . "</td> "; 
}

this line

 while ($info = mysql_fetch_array( $getdata ));

should be

 while ($info = mysql_fetch_array( $getdata ))
Sign up to request clarification or add additional context in comments.

3 Comments

Well I'll be damned. Nice!
Everyone overlooks the obvious parts.. chuckle.
Good time to mention that you should look into mysqli_* functions instead of the mysql_* ones that you are using which are deprecated. Also you are vulnerable to injection because you are not sanitizing the input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.