1.

Solve : PHP/MySQL help - Warning: mysql_fetch_array(): supplied argument is not a...?

Answer»

Hello.

I'm currently learning php and mysql. I've set up a local "wamp" host on my pc to test my websites. All I want is to simply list the content from a test database I've created. Here's my code for my php FILE.

<?php

//Login details
//Host, Port, Username, Password
//
$host = 'localhost'; //Def localhost
$port = '3306'; //Def 3306
$user = ''; //Def blank or root
$pass = ''; //Def blank
//

$connect = @mysql_connect($host.':'.$port.','.$user.','.$pass) or die (mysql_error());
echo ('Connected!');

$database = mysql_select_db('mydb');
$sql = 'SELECT * FROM persons';
$result = mysql_query($sql);

while($rad = mysql_fetch_array($result)){
echo ($rad[firstname].$rad[lastname].'
');
}
mysql_close($connect);

?>

When I test my PAGE in the browser I get the following output:
Connected!
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in F:\wamp\www\FAILS\mysql\mysql_connect_alt2.php on line 18

Acording to the error this is the line causing the trouble:
while($rad = mysql_fetch_array($result)){

Any help is well appreciated!Code: [Select]<?php

//Logindetails
//Host,Port,Username,Password
//
$host='localhost';//Deflocalhost
$port='3306';//Def3306
$user='';//Defblankorroot
$pass='';//Defblank
//

$connect=@mysql_connect($host.':'.$port.','.$user.','.$pass)ordie(mysql_error());
echo('Connected!');

$database=mysql_select_db('mydb');
$sql='SELECT*FROMpersons';
$result=mysql_query($sql);

$NUM=mysql_numrows($result);
$i=0;

while($i<$num){

$firstname=mysql_result($result,$i,"firstname");
$lastname=mysql_result($result,$i,"lastname");

echo"$firstname$lastname<br/>";
}
mysql_close($connect);

?>

Thanks for the relpy but it still outputs an error :/

Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in F:\wamp\www\FAILS\mysql\mysql_connect_alt2.php on line 19

Also, changing mysql_numrows to mysql_num_rows(): does not help either. Gives this:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in F:\wamp\www\FAILS\mysql\mysql_connect_alt3.php on line 19Try this:

Code: [Select]<?php

//Logindetails
//Host,Port,Username,Password
//
$host='localhost';//Deflocalhost
$port='3306';//Def3306
$user='';//Defblankorroot
$pass='';//Defblank
//

$connect=@mysql_connect($host.':'.$port.','.$user.','.$pass)ordie(mysql_error());
echo('Connected!');

$database=mysql_select_db('mydb');

//Retrieveallthedatafromthe"persons"table
$result=mysql_query("SELECT*FROMpersons")
ordie(mysql_error());

//storetherecordofthe"persons"tableinto$row
$row=mysql_fetch_array($result);
//Printoutthecontentsoftheentry

echo"FirstName:".$row['firstname'];
echo"LastName:".$row['lastname'];

?>



Discussion

No Comment Found