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

Paul Morris
  • 1,763
  • 10
  • 33
  • 47

5 Answers5

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.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
lovenish
  • 71
  • 1
  • 2
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.

Community
  • 1
  • 1
Chandresh M
  • 3,808
  • 1
  • 24
  • 48
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.

Mat
  • 202,337
  • 40
  • 393
  • 406
1

Check the return code on mysql_select_db.

Scott C Wilson
  • 19,102
  • 10
  • 61
  • 83
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 ...

Rukmi Patel
  • 2,619
  • 9
  • 29
  • 41
  • 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 – Paul Morris Jul 26 '11 at 12:31