Link to home
Start Free TrialLog in
Avatar of pdheady
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","usernamexyz","passwordxyz");
mysql_select_db("dbxyz",$handle_db2);
     
 if (eregi('^[[:alnum:]\.\'\-]{6,10}$', stripslashes(trim($_POST['username']))) ) {
              $user = mysql_real_escape_string($_POST['username']);
              $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>";
              }

Avatar of Rok-Kralj
Rok-Kralj
Flag of Slovenia image

add this to start of your document:

error_reporting(E_ALL);
ini_set('display_errors', '1');

It will report all errors.
Avatar of pdheady
pdheady

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.php on line 5

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /blab/blah/blah/blah/db.php on line 8

   $result = @mysql_query($query,$which);

I dont believe which is defined. Please try $handle_db2.
ASKER CERTIFIED SOLUTION
Avatar of Rok-Kralj
Rok-Kralj
Flag of Slovenia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
$handle_db2 = mysql_connect("127.0.0.1","usernamexyz","passwordxyz");
mysql_select_db("dbxyz"); //you can escape handle here
Avatar of pdheady

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","usernamexyz","passwordxyz");

Avatar of pdheady

ASKER

Thanks Rok!
$handle_db2 = mysql_connect("localhost","usernamexyz","passwordxyz");
No problem. Thanks for points.
People who thank for solution really make me feel great. Thanks.