Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

php/mysql db connection problem

Status
Not open for further replies.

wagnj1

Programmer
Jul 21, 2003
154
CA
I'm writing a php page that will connect to a mysql Db and insert a row. I have set up a user account with select and insert privlidges and I am using it to connect. If I use this account/pw to log on in a command shell it lets me log on but I cannot connect using the php script. My code looks as follows:
Code:
	@ $db = mysql_pconnect("localhost","username","password");

	if (!$db)
	{
		echo "Error: Could not connect to database. Please try again later.";
		echo $quote;
		echo $author;
		exit;
	}
no matter what I try I always get the could not connect error. anyone had this problem before?
 
if you change:

echo "Error: Could not connect to database. Please try again later.";

to

echo "Could not connect to database. " . mysql_error();


What is output?

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
good idea thanks...I'm getting the "Client does not support authentication protocol requested by server; consider upgrading MySQL" error, so I'll have to update that account's password using the OLD_PASSWORD command.right?
 
haha now I'm getting the message:

-1 quote(s) inserted into database.

and nothing appears to be going into the table.
 
I guess that means you managed to connect. What is the INSERT query you are doing?
 
ok heres where my problem is now...I have the first column in the table set to auto-increment and I'm only wanting to insert the two columns of data after it, how do I do this?

something like so? :

insert into quotes values (,,'quote','author');
 
the rest of my code after the connect is:
Code:
mysql_select_db("databasename");
	$query = "insert into quotes values ('".$quote."','".$author."')";

	$result = mysql_query($query);
	echo mysql_affected_rows()." quote(s) inserted into database.";

	mysql_close($db);
 
k I figured it out:

insert into table (column2, column3) values ('val2','val3');
 
I have one more question...is there a way for me to set my auto increment so that if one gets deleted it does the following:

--before--
1 cat
2 dog
3 fish

--dog row gets deleted

--after--
1 cat
2 fish
 
ok I found another problem...I use my insert page to insert a row into the database and if I go back to my html page with the form on it and add another row (of different data) I keep getting the "-1 row added to table." output, and then nothing is added to the table. what causes a -1 when you echo the mysql_affected_rows()?
 

I've determined that my problem has something to do with max lengths...the column thats having problems is of varchar type and is set to the 255 max. when I insert data that is relativly short it inserts correctly but if I enter longer data it comes back with the -1 error. Either way, I'm sure the length of the data is less than the 255 max.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top