PHP seems unable to read from ultra-simple database

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Simnia
Forum Newbie
Posts: 9
Joined: Tue Jul 07, 2009 5:37 pm

PHP seems unable to read from ultra-simple database

Post by Simnia »

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.
----------

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>
...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 =
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: PHP seems unable to read from ultra-simple database

Post by andyhoneycutt »

I could be wrong, but if you're not using a password, I believe you still need to send an empty string as it's argument:

Code: Select all

mysql_connect($host,$user,'');
You might want to put "or die()" clauses after your connect and select attempts, just to see what's going on:

Code: Select all

mysql_connect() or die("Connection to the database failed.");
mysql_select_db() or die("Can't select a catalog.");
Functionally, I don't see anything wrong with your code, but I've been staring down php all day, and am a little fried. Sorry if this doesn't help much!

-Andy
Simnia
Forum Newbie
Posts: 9
Joined: Tue Jul 07, 2009 5:37 pm

Re: PHP seems unable to read from ultra-simple database

Post by Simnia »

The following code with a blank password...

Code: Select all

<?
$mysql_access = mysql_connect("localhost", "root"," ") or die("Connection to the database failed.");
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);
?>
...resulted in the following messages...

Code: Select all

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'root'@'localhost' (using password: YES) in C:\xampp\htdocs\if3\mini.php on line 2
Connection to the database failed.
...and the same code with an empty password ("") produced the same error I was getting earlier...

Code: Select all

$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 =
I think the issue is something simple like you suggested--I just don't have enough recent experience to know what it is. Maybe it's the difference between single quotes and double quotes, path to the database folder, intolerance to missing passwords, a missing parameter, or something silly like that.
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: PHP seems unable to read from ultra-simple database

Post by andyhoneycutt »

When you setup mysql did you specify a root password? This has me a bit boggled because the default xamp mysql install uses 'root' as the user with no password. If this is the case for your setup, you should be good to go on your first example of your usage of the mysql_connect function.
Simnia
Forum Newbie
Posts: 9
Joined: Tue Jul 07, 2009 5:37 pm

Re: PHP seems unable to read from ultra-simple database

Post by Simnia »

I figured out the problem this morning. All it was is I hadn't defined variable "$db" before using it. I didn't remember exactly what those two initial PHP functions did, and whether the parameters were being sent vs being returned. The following code works...

Code: Select all

<?
$mysql_access = mysql_connect("localhost", "root", "") or die("Connection to the database failed."); // works
echo '$mysql_access  = ', $mysql_access, "<br>"; // Displays: Resource id #3
 
$db = "onedb";
mysql_select_db($db, $mysql_access) or die("Can't select a catalog.");
 
$result = mysql_query("select * from onetable");
echo '$result  = ', $result, "<br>";
$row = mysql_fetch_row($result);
echo '$row  = ', $row , "<br>"; // Displays: Array
 
mysql_close($mysql_access);
?>
 
Thanks for the suggestions, though. I rebuilt the database without a password, so I didn't think a password could be responsible for the essential problem, unless it had something to do with syntax or parameters relating to the password.
Post Reply