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>";
              }

LVL 1
pdheadyAsked:
Who is Participating?
 
Rok-KraljConnect With a Mentor Commented:
rschoenbach it is not problem with query, but with connection.

pdheady, check password, host and username again. (write localhost instead of 127.0.0.1)
0
 
Rok-KraljCommented:
add this to start of your document:

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

It will report all errors.
0
 
pdheadyAuthor Commented:
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

0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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

I dont believe which is defined. Please try $handle_db2.
0
 
Rok-KraljCommented:
$handle_db2 = mysql_connect("127.0.0.1","usernamexyz","passwordxyz");
mysql_select_db("dbxyz"); //you can escape handle here
0
 
pdheadyAuthor Commented:
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");

0
 
pdheadyAuthor Commented:
Thanks Rok!
0
 
Rok-KraljCommented:
$handle_db2 = mysql_connect("localhost","usernamexyz","passwordxyz");
0
 
Rok-KraljCommented:
No problem. Thanks for points.
0
 
Rok-KraljCommented:
People who thank for solution really make me feel great. Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.