1

I am writing PHP code for a dynamic web page. The SQL query code below looks correct, but whenever the page runs, I keep getting the message within the die parameter.

$recipeid = $_GET['id'];
$query = "select title from recipes where recipeid = $recipeid";
$result = mysql_query($query) or die('Could not retrieve file');

Why does the query keep failing?

8
  • 4
    Try to run $result = mysql_query($query) or die(mysql_error()); Commented Jun 3, 2011 at 21:47
  • 6
    Exploits of a Mom Commented Jun 3, 2011 at 21:49
  • 2
    SQL injection is your biggest concern here. Commented Jun 3, 2011 at 21:49
  • 1
    Do what @nick says above and tell use the error you get. Here's a tip, make you SQL easier to read by capitalising key words like so, SELECT title FROM recipes WHERE recipeid = $recipeid Commented Jun 3, 2011 at 21:51
  • Too little to tell. Could be a number of things. Commented Jun 3, 2011 at 21:52

3 Answers 3

2

Aside from the SQL Injection ( look into mysql_real_escape_string ) add this to your error statement to see the exact error:

 $result = mysql_query($query) or die('Could not retrieve file: ' . mysql_error());

And it will tell you what is wrong.

Sign up to request clarification or add additional context in comments.

Comments

1

Could be a handful of different things, since we're only seeing a little snippet of the code, it would just be speculation. Posting code or more details about what you know the error isn't would be helpful.

First try putting single quotes around $recipeid. like so:

$query = "select title from recipes where recipeid = '$recipeid'";

If that doesn't work, time for more debugging.

Comments

1

If invoke the page without passing the id parameter in url (?id=22, for instance) you'll get a syntax error.

Try checking if $recipeid has a value prior to execute the query.

Comments

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.