[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • Last Modified:

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

0
pdheady
Asked:
pdheady
  • 6
  • 3
1 Solution
 
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
 
rschoenbachCommented:
   $result = @mysql_query($query,$which);

I dont believe which is defined. Please try $handle_db2.
0
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
Rok-KraljCommented:
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:
$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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now