2

I am very new to PHP, trying to write a script which connects to a MySQL database and simply displays the contents in list format under each heading;

My table contains an ID (AutoIncrement), FName, SName & EAddress fields.

The database is called iphonehe_MGFSales and the username is iphonehe_MGFSale - I have added the user to the DB with full privileges.

I am trying to establish my connection to the DB using the mysql function with this code;

mysql_connect ("localhost", "iphonehe_MGFSale", "xxxxxxx") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("iphonehe_MGFSales");

The table I have created is called MGFSales DB. I am using this code to attempt to build the query;

$query = mysql_query("SELECT * FROM MGFSales_DB");

And finally I am trying to display the results using the following code;

while ($row = mysql_fetch_array ($query)) {
echo "<br /> ID: " .$row['ID']. "<br /> First Name: ".$row['FName']. "<br /> Last Name: ".$row['LName']. "<br /> Email: ".$row['EAddress']. "<br />";
}

I have named the file index.php and uploaded to my server, when running I get the following error 'Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/iphonehe/public_html/pauldmorris.co.uk/mgf/index.php on line 16'

Anyone point me in the right direction? Line 16 of my code seems pretty tight from what I can see, am i overlooking something? Thanks

5
  • Does the table you created contain any data? Commented Jul 26, 2011 at 12:16
  • Yes, I have entered two test entries into it Commented Jul 26, 2011 at 12:19
  • possible duplicate of MySQL query problem Commented Jul 26, 2011 at 12:21
  • It was going to suggest the examples in the PHP manual page but most of them fail to do proper error checking :( Commented Jul 26, 2011 at 12:23
  • @Rasel - Table name is correct Commented Jul 26, 2011 at 12:24

5 Answers 5

7

The error is in mysql_fetch_array line only, correcting this should be first step in troubleshooting.

change mysql_fetch_array to

mysqli_fetch_array

becuase the latest updates in mysql or php does not accept mysql but accepts only mysqli. Also change everywhere mysql to mysqli in your code.

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

Comments

4

this is because of null resource found in $query.. you need to check this like below code

$query = mysql_query("SELECT * FROM MGFSales_DB"); or die("Error: ". mysql_error(). " with query ");

if(mysql_num_rows($query) > 0 ){
 while ($row = mysql_fetch_array ($query)) {
echo "<br /> ID: " .$row['ID']. "<br /> First Name: ".$row['FName']. "<br /> Last Name: ".$row['LName']. "<br /> Email: ".$row['EAddress']. "<br />";
 }
}

OR you can also refer this link

Try this may help you.

Thanks.

1 Comment

The error message now changes to Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/iphonehe/public_html/pauldmorris.co.uk/mgf/index.php on line 16
1

mysql_query returns false when it fails, which will produce the error you're getting during mysql_fetch_array.

Please add some error checking to your code, and print out/log the error messages - can't help any more than that without knowing what the source error is.

1 Comment

Ok, thanks Mat. I guess I will now go and learn about error logging and how to do it ha
1

Check the return code on mysql_select_db.

Comments

1

You should not apply mysql_fetch_array directly..

you should first check for data ..

if(mysql_num_row($query)>0){
   your code   
}
else{
   echo 'it brings no data....';
}

it checks if there is no data then it will execute else block other wise you will have smooth execution ...

1 Comment

Altering my code to incorporate the above changes the error message to Fatal error: Call to undefined function mysql_num_row() in /home/iphonehe/public_html/pauldmorris.co.uk/mgf/index.php on line 16

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.