Forum Moderators: phranque
I started with IIS and at first I was getting the error 'cannot connect to mySQL server localhost" along with a long list of other errors which must have been directly related to the first one. But I figured IIS was having a problem communicating with mySQL so I turned off IIS, started Apache, and moved my files into Apache's root directory and now it gives me one error: 1049: Unknown database 'mydb'. No matter what I name the file, where I move it to, or what I call it in the code, it still gives me the same exact error message.
I got the PHP and database code from this PHP/mySQL tutorial [hotwired.lycos.com].
Here is the database code [hotwired.lycos.com] (at the bottom of the page)
Here is the PHP code [hotwired.lycos.com]
I searched on Google and it looks like a lot of other people are having the same problem because 3/4 of the reults on the first 3 pages were pages which generated this error message :)
Needless to say, I was unable to find a solution there. I also tried WW's site search, but to no avail.
well i would start with the database first and worry about the php later.
Have you logged into mysql via the commandline or using a product such as phpmyadmin to see if the database actually exists and has some data in it?
Is mysql configured properly? Have you set the permissions and/or user/pass for the user you are trying to connect with?
Get mysql working properly and then you can move onto the php. Otherwise you will be running around chasing errors from multiple sources not knowing what's coming from where.
The true key to errors is isolation. Isolate the first error, fix it, and move onto the next from there.
As far as php help goes, if you are sure that mysql is running properly and the db is created etc we can look more into the php probs.
I think MySQL is running now but I'm still having trouble pulling information from the db. Instead of "cannot connect to db" or "unknown db", now I get these 4 errors:
Warning: Supplied argument is not a valid MySQL result resource in c:\apache\htdocs\phpmysql.php on line 11
First Name:
Warning: Supplied argument...line 12
Last Name:
Warning: Supplied argument...line 13
Address:
Warning: Supplied argument...line 14
Position:
Here is the database code:
CREATE TABLE employees ( id tinyint(4) DEFAULT '0' NOT NULL AUTO_INCREMENT, [b]first[/b] varchar(20), [b]last[/b] varchar(20), [b]address[/b] varchar(255), [b]position[/b] varchar(50), PRIMARY KEY (id), UNIQUE id (id));INSERT INTO employees VALUES (1,'Bob','Smith','128 Here St, Cityname','Marketing Manager'); INSERT INTO employees VALUES (2,'John','Roberts','45 There St , Townville','Telephonist'); INSERT INTO employees VALUES (3,'Brad','Johnson','1/34 Nowhere Blvd, Snowston','Doorman'); Here is the PHP code:
$db = mysql_connect("localhost", "root");
mysql_select_db("mydb",$db);
$result = mysql_query("SELECT * FROM employees",$db);
printf("First Name: %s<br>\n", mysql_result($result,0,"[b]first[/b]"));
printf("Last Name: %s<br>\n", mysql_result($result,0,"[b]last[/b]"));
printf("Address: %s<br>\n", mysql_result($result,0,"[b]address[/b]"));
printf("Position: %s<br>\n", mysql_result($result,0,"[b]position[/b]")); mydb is actually called mydb.dump if that makes any difference.
Also, I ran this page: localhost/phpmyadmin/sql.php with Apache and MySQL-D-NT running, and it said:
Error
MySQL said:
Query was empty
So I'm guessing that MySQL is in fact running or else it wouldn't say anything?
Oh and jatar_k, when I run mySQL-D-NT, a DOS-like window appears for a second and then it immediately disappears. Is this the commandline? Is it suppose to automatcially close like that or is it suppose to stay open?
[edited by: dougmcc1 at 11:01 pm (utc) on June 7, 2003]
Supplied argument is not a valid MySQL result resource
It depends on what function this is coming from but it usually means you have a problem with the query you are sending.
Some things I noticed (these aren't necessarily the problem)
CREATE TABLE employees ( id tinyint(4) DEFAULT '0' NOT NULL AUTO_INCREMENT, first varchar(20), last varchar(20), address varchar(255), position varchar(50), PRIMARY KEY (id), UNIQUE id (id));
You dont need default 0 or not null on an auto_increment primary key and they are unique by definition, so..
CREATE TABLE employees (id tinyint(4) AUTO_INCREMENT PRIMARY KEY, first varchar(20), last varchar(20), address varchar(255), position varchar(50));
INSERT INTO employees VALUES (1,'Bob','Smith','128 Here St, Cityname','Marketing Manager')
because id is auto increment you could do this
INSERT INTO employees VALUES ('','Bob','Smith','128 Here St, Cityname','Marketing Manager')
and it will place the appropriate value in the field.
on to the PHP
$db = mysql_connect("localhost", "root");
I assume you are using an empty password other wise this won't work.
$result = mysql_query("SELECT * FROM employees",$db);
you don't need to pass the link_identifier every time because
If link_identifier isn't specified, the last opened link is assumed. That is also true for mysql_select_db.
for the display and looping through the results try something like this
while ($row = mysql_fetch_array [php.net]($result)) {
echo "<p>First Name: ",$row['first'],"\n";
echo "<br>Last Name: ",$row['last'],"\n";
echo "<br>Address: ",$row['address'],"\n";
echo "<br>Position: ",$row['position'],"\n";
}
mysql_result is very slow having to do one call per cell where as mysql_fetch_array gets a whole row at a time as an associative/numeric array and puts it into the designated variable.
If the response was query was empty then the inserts may not have worked because there is no data in the table (at least returned from that particular query). You could do inserts via the script. Like so
$iq = "INSERT INTO employees VALUES ('','John','Roberts','45 There St , Townville','Telephonist')";
mysql_query($iq);
hope some of this helps.