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

Internet and Networking Web design in Internet and Networking . 3 years ago

  0   0   0   0   0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

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'];

?>

Posted on 18 May 2022, this text provides information on Internet and Networking related to Web design in Internet and Networking. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Tuteehub forum answer Answers

Post Answer

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.