pdheady
asked on
PHP code broke all of a sudden why?
My programming logic for the code below broke all of a sudden for no apparent reason!
For example, I have a form to setup a username and password, and post to the database but i have logic to check if the username already exists and if so output an error message. The php code to check if the username row exists for say "johndoe" isnt working and Ive been banging my head against the wall as to why it broke all of sudden. The only thing that has changed is i setup my server as a slave to replicate a different database off the master server, but that should not cause the problem should it?
In the code below it should ouput the error because i am testing with a username that already exists. All of my error checks, such as password length and match, output the errors correctly. I'm going to write a test debug script to see if it can read and write to the database, maybe i messed up my priveleges?
$handle_db2 = mysql_connect("127.0.0.1", "usernamex yz","passw ordxyz");
mysql_select_db("dbxyz",$h andle_db2) ;
if (eregi('^[[:alnum:]\.\'\-] {6,10}$', stripslashes(trim($_POST[' username'] ))) ) {
$user = mysql_real_escape_string($ _POST['use rname']);
$query = "SELECT username FROM users WHERE username = '$user'"; $which = $handle_db2;
$result = @mysql_query($query,$which );
$num = @mysql_num_rows($result);
if ($num> 0) {
$errors[] = 'The username you have chosen has already been taken, please try again.';
}
foreach ($errors as $msg) {
echo "<p><font color=\"red\">$msg</font>< /p>";
}
For example, I have a form to setup a username and password, and post to the database but i have logic to check if the username already exists and if so output an error message. The php code to check if the username row exists for say "johndoe" isnt working and Ive been banging my head against the wall as to why it broke all of sudden. The only thing that has changed is i setup my server as a slave to replicate a different database off the master server, but that should not cause the problem should it?
In the code below it should ouput the error because i am testing with a username that already exists. All of my error checks, such as password length and match, output the errors correctly. I'm going to write a test debug script to see if it can read and write to the database, maybe i messed up my priveleges?
$handle_db2 = mysql_connect("127.0.0.1",
mysql_select_db("dbxyz",$h
if (eregi('^[[:alnum:]\.\'\-]
$user = mysql_real_escape_string($
$query = "SELECT username FROM users WHERE username = '$user'"; $which = $handle_db2;
$result = @mysql_query($query,$which
$num = @mysql_num_rows($result);
if ($num> 0) {
$errors[] = 'The username you have chosen has already been taken, please try again.';
}
foreach ($errors as $msg) {
echo "<p><font color=\"red\">$msg</font><
}
ASKER
Thanks Rok.
Looks like it cant connect to the database now for some reason.
Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on '127.0.0.1' (13) in /blab/blah/blah/blah/db.ph p on line 5
Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /blab/blah/blah/blah/db.ph p on line 8
Looks like it cant connect to the database now for some reason.
Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on '127.0.0.1' (13) in /blab/blah/blah/blah/db.ph
Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /blab/blah/blah/blah/db.ph
$result = @mysql_query($query,$which );
I dont believe which is defined. Please try $handle_db2.
I dont believe which is defined. Please try $handle_db2.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
$handle_db2 = mysql_connect("127.0.0.1", "usernamex yz","passw ordxyz");
mysql_select_db("dbxyz"); //you can escape handle here
mysql_select_db("dbxyz"); //you can escape handle here
ASKER
Well heres the problem! My server localhost IP is not set! Using localhost for my second db connection fix the problem. So have to go into etc/hosts and defin the localhost IP.
$handle_db2 = mysql_connect("127.0.0.1", "usernamex yz","passw ordxyz");
$handle_db2 = mysql_connect("127.0.0.1",
ASKER
Thanks Rok!
$handle_db2 = mysql_connect("localhost", "usernamex yz","passw ordxyz");
No problem. Thanks for points.
People who thank for solution really make me feel great. Thanks.
error_reporting(E_ALL);
ini_set('display_errors', '1');
It will report all errors.