tags:

views:

35

answers:

3

Hey I'm a beginner in mySQL, this is the code that i wrote,`

        <?php

        echo "Here is the table";
        $con = mysql_connect("localhost","root","mypassword");

        if(!$con)
            die('Error:'.mysql_error());


        // Create table
        mysql_select_db("my_db", $con);
        $sql = "CREATE TABLE persons
        (
            f_name varchar(15),
            l_ame varchar(15),
            age int
        )";


        mysql_query("INSERT INTO persons (f_name, l_name, age)
        VALUES ('Peter', 'Griffin', '35')");

        mysql_query("INSERT INTO persons (f_name, l_name, age)
        VALUES ('Glenn', 'Quagmire', '33')");

        $res = mysql_query("SELECT * FROM persons");
        while($row = mysql_fetch_array($res))
        {
            echo $row['f_name'] . " " . $row['l_name'];
            echo "<br />";
        }
    ?>`

I'm getting the following error

Here is the table Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /var/www/PhpProject1/index.php on line 40

why am I getting this error and how do I resolve it?

+2  A: 

matschie's answer is correct but that's not the only error.

Your column is also spelled incorrectly in your create table statement:

l_ame varchar(15),

should be:

l_name varchar(15),

To catch errors like this yourself you can check the return value of mysql_query to see if there is an error and if so display the error using mysql_error.

An example of how to do this is shown in the PHP manual for mysql_query:

$result = mysql_query('SELECT * WHERE 1=1');
if (!$result) {
    die('Invalid query: ' . mysql_error());
}
Mark Byers
i changed it to l_name , still gettin the same error though
Tarun
@Tarun: There is one more error in your code. I would suggest that you spend five minutes to learn how to use mysql_error (see the manual for mysql_query and mysql_error). That five minutes spent now will save you many hours in the future.
Mark Byers
^ sure, thanks Mark
Tarun
+4  A: 

You have not created the Table "persons", you just have a String with the create Statement in it. Also there is a typo near "l_ame varchar(15)," which should be "l_name varchar(15),". So you should try the following code:

$sql = "CREATE TABLE persons 
( 
f_name varchar(15), 
l_name varchar(15), 
age int 
)";     
$res = mysql_query($sql) or trigger_error(mysql_error().$sql); ;
Adding that extra line will still give the same error. (Look carefully at the query - there's also a typo.)
Mark Byers
True. So you could just combine the answers of Mark Byers, Col. Shrapnel and me and the script should work or atleast you can see the Errors which occured.
+2  A: 

that is not actually mysql_fetch_array error. this error message says that this function's argument is wrong.
and this argument returned by mysql_query() function
so it means that there was an error during query execution, on the mysql side.
ans mysql has plenty of error messages, quite informative ones.

Thus, instead of guessing, you have to get in touch with certain error message that will tell you what is wrong.

to achieve that, always run your queries this way:

$sql = "SELECT * FROM persons";
$res = mysql_query($sql) or trigger_error(mysql_error().$sql);
Col. Shrapnel
+1 This! Just fixing the errors in this query won't help the OP in the long run.
Mark Byers