PHP seems unable to read from ultra-simple database
Posted: Thu Jul 23, 2009 4:02 pm
Did PHP or MySQL change in the last year, or something? None of the code I wrote a year ago works on my computer at work, and none of the code I've found online recently works, either. I'm at the very basics again, trying to get *anything* to work with PHP-MySQL communication, despite my having become reasonably proficient at PHP and MySQL last year.
Here are the details. Here is the code I wrote in PHP, in a file called "mini.php". I commented out some lines that didn't work, to give an example of what I tried earlier.
----------
----------
I created the MySQL database by hand in the comand prompt, using the simple login...
...and I inserted two rows of data to be safe. Everything is just about as simple as it can be, but PHP doesn't want to read from that database or write anything to that database, for some reason. Is there some password issue, or host issue, or user name issue that I'm overlooking?
Here is what results when I run the PHP program...
$mysql_access = Resource id #2
$db =
$result =
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\if3\mini.php on line 12
$row =
Here are the details. Here is the code I wrote in PHP, in a file called "mini.php". I commented out some lines that didn't work, to give an example of what I tried earlier.
----------
Code: Select all
<?
$mysql_access = mysql_connect("localhost", "root");
echo '$mysql_access = ', $mysql_access, "<br>"; // Displays: Resource id #3
mysql_select_db($db, $mysql_access);
echo '$db = ', $db , "<br>"; // Displays: (blank)
$result = mysql_query("select * from onetable"); // Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\if3\mini.php on line 13
//$result = mysql_query("select * from onetable", $mysql_access); // Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\if3\mini.php on line 12
//$result = mysql_query("SHOW tables", $mysql_access); // Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\if3\mini.php on line 11
echo '$result = ', $result, "<br>"; // Displays: (blank)
$row = mysql_fetch_row($result); // <--- problem is said to be here
echo '$row = ', $row , "<br>"; // Displays: (blank)
mysql_close($mysql_access);
?>I created the MySQL database by hand in the comand prompt, using the simple login...
Code: Select all
C:\xampp\mysql\bin>mysql -u root -h localhost
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.1.33-community MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> create database onedb;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| cdcol |
| mysql |
| onedb |
| phpmyadmin |
| readings |
| test |
| webauth |
+--------------------+
8 rows in set (0.03 sec)
mysql> use onedb;
Database changed
mysql> create table onetable(onecol int);
Query OK, 0 rows affected (0.06 sec)
mysql> insert into onetable values(3);
Query OK, 1 row affected (0.02 sec)
mysql> insert into onetable values(4);
Query OK, 1 row affected (0.00 sec)
mysql>Here is what results when I run the PHP program...
$mysql_access = Resource id #2
$db =
$result =
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\if3\mini.php on line 12
$row =