Computer Hope
Internet & Networking => Web design => Topic started 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!
-
<?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);
?>
-
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
-
Try this:
<?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'];
?>