-1

here's my code

<?php
    mysql_connect('localhost','root','');
    $conn=mysql_select_db('project');

    $course="snacks";
    echo $q="select r_name,prep_time,cook_time,ingredient,procedure from recipe where course=$course";
    $res=mysql_query($q);


    while($disp=mysql_fetch_array($res))
    {
        echo "$disp[r_name]</br>";
        echo "$disp[ingredient]</br>";
        echo "$disp[procedure]</br>";   
    }

?>  

the error is

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\web\course.php

i have tried every way to fix it but it still shows this error.

  • 1
    Have you tried printing `mysql_error()` to see what the problem with the query is? Isn't that in all the related questions that you looked at before posting the question? – Barmar Mar 15 '14 at 10:28
  • You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Quentin Mar 15 '14 at 10:30

5 Answers5

1

You haven't quoted the course in the query:

    echo $q="select r_name,prep_time,cook_time,ingredient,`procedure` from recipe where course='$course'";

Also, procedure is a reserved keyword in MySQL, you need to quote it with backticks.

Barmar
  • 669,327
  • 51
  • 454
  • 560
1

procedure is mysql reserved keyword try use backticks around it

    `procedure`

reserved keywords

echo_Me
  • 36,552
  • 5
  • 55
  • 77
0

Try to add single quotes to $course variable.

$q="select r_name,prep_time,cook_time,ingredient,procedure from recipe where course='$course'";
Dulitha K
  • 2,038
  • 1
  • 19
  • 18
0

Your mysql statement gives no data back and sets $res to false

Try:

"select r_name,prep_time,cook_time,ingredient,procedure from recipe where course='$course'"

I just added '' around the $course.

Birdey
  • 5
  • 1
  • 4
0

First off, Try switching to mysqli, for mysql is getting outdated.

That being said, the trigger isn't that hard to find. The fetch_assoc() function takes an resource, one send as a restult from the database.

So you go up 1 step, to your query. If you read the documentation on mysql_query(), you'll find it return a resource, or a false (->boolean) when it fails.

As has been pointed out, the error is your usage of procedure. In mysql thats a reserved keyword which you can not use like that. You have to add backticks ( ` ) to escape it. The other error is you that $course needs to be wrapped in single quotes. In queries, all strings have to be wrapped in single quotes.

How can you find this yourself? by changing your query to the following:

$res = mysql_query($q) or die(mysql_error());

That will result in an error on your screen, which will tell you about the procedure :)
Small sidenote: Using die() is not the best of habbits, replace that with a custom function to have more influence towards the user (like a pretty 'oops' screen)

Community
  • 1
  • 1
Martijn
  • 15,365
  • 4
  • 34
  • 66