Computer Hope

Internet & Networking => Web design => Topic started by: -Oscar- on May 29, 2008, 07:34:25 AM

Title: PHP/MySQL help - Warning: mysql_fetch_array(): supplied argument is not a...
Post by: -Oscar- on May 29, 2008, 07:34:25 AM
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!
Title: Re: PHP/MySQL help - Warning: mysql_fetch_array(): supplied argument is not a...
Post by: Astoria on May 29, 2008, 11:41:22 AM
Code: [Select]
<?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);

$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);

?>


Title: Re: PHP/MySQL help - Warning: mysql_fetch_array(): supplied argument is not a...
Post by: -Oscar- on May 30, 2008, 06:49:13 AM
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 19
Title: Re: PHP/MySQL help - Warning: mysql_fetch_array(): supplied argument is not a...
Post by: Astoria on June 01, 2008, 07:00:54 AM
Try this:

Code: [Select]
<?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');

// Retrieve all the data from the "persons" table
$result mysql_query("SELECT * FROM persons")
or die(
mysql_error());  

// store the record of the "persons" table into $row
$row mysql_fetch_array$result );
// Print out the contents of the entry 

echo "First Name: ".$row['firstname'];
echo 
" Last Name: ".$row['lastname'];

?>