Jump to content

[SOLVED] Can't read/write to MySQL


finks

Recommended Posts

I recently upgraded from php4 to php5. I would of done it a long time ago but I always get problems with the conversion. Like this for instance. Now I cannot seem to read and write to MySQL what-so-ever. It works fine under the php4 environment but is just not wanting to work under php5. I can make the connection to the MySQL server and even to the MySQL database, but I cannot read or write to the tables! I'm sure it is just some minor detail that I am missing, but meh, I cannot figure it out. Here is my info:

 

MySQL client version: 4.1.18

PHP Version 5.2.4

 

MySQL is showing up as being supported under phpinfo and it can make the connection between PHP and MySQL, just cannot read or write to it. Anyways, I am repeating myself so here is the PHP code (works under php4 not php5) so any info would be greatly appreciated.

 

<?php
require_once('conf.inc.php');
require_once('functions.php');
// ---
// register new user
// ---
function register($username,$pass,$email,$question,$answer)
{
   GLOBAL $db, $table1;
   $username = trim($username);
   $pass = trim($pass);
   $email = trim($email);
   $question = addslashes(trim($question));
   $answer = addslashes(trim($answer));
   $validEmail = valid_email($email);
   $validName = valid_userName($username);
   $validPass = valid_password($pass);
   if(!$validName) return "error=invalid name";
   if(!$validPass) return "error=invalid password";
   if(!$validEmail) return "error=invalid email";
   $pass = md5(trim($pass));
   // all checks ok
   $query = mysql_query("INSERT INTO $table1 (userName,userPassword,userMail,userQuestion,userAnswer) VALUES "
   ."('$username','$pass','$email','$question','$answer')");
   if(!$query)
   {
      return "error=" . mysql_error();
   } else {
      return "user=ok";
   }
}

// ---
// user login, check user
// ---
function login($username,$pass)
{
   GLOBAL $db,$table1;
   $username = trim($username);
   $pass = md5(trim($pass));
   $query = mysql_query("SELECT * FROM $table1 WHERE userName = '$username' AND userPassword = '$pass'");
   return mysql_num_rows($query);
}

// ---
// perf login, check user
// ---
function login2($username,$pass)
{
   GLOBAL $db,$table2;
   $username = trim($username);
   $pass = md5(trim($pass));
   $query = mysql_query("SELECT * FROM $table2 WHERE userName = '$username' AND userPassword = '$pass'");
   return mysql_num_rows($query);
}

// ---
// forget password
// ---
function forget($email)
{
   GLOBAL $db,$table1;
   $email = trim($email);
   $query = mysql_query("SELECT userName, userQuestion from $table1 WHERE userMail = '$email'");
   if(mysql_num_rows($query)<1)
   {
      return "error=email not present in the database";
   }
   $row = mysql_fetch_array($query);
   return "userName=$row[userName]&userQuestion=" . stripslashes($row['userQuestion']);
}

// ---
// retrieve bio
// ---
function bio($username)
{
   GLOBAL $db,$table2;
   $username = trim($username);
   $query = mysql_query("SELECT userMail, userBio, userSite, userPic from $table2 WHERE userName = '$username'");
   if(mysql_num_rows($query)<1)
   {
      return "error=username not present in the database";
   }
   $row = mysql_fetch_array($query);
   return "userMail=$row[userMail]&userBio=$row[userBio]&userSite=$row[userSite]&userPic=$row[userPic]";
}

// ---
// create profile directory
// ---
function make_pro_dir($username)
{
   $username = trim($username);
   $folder = explode( DIRECTORY_SEPARATOR , $username );
   $mkfolder = '';
   for(  $i=0 ; isset( $folder[$i] ) ; $i++ )
    {
       $mkfolder .= $folder[$i];
       if(!is_dir($mkfolder)) {
   mkdir( "$mkfolder" ,  0777);
   if(!is_dir($mkfolder)) { print "stuff=0"; }
   else { print "stuff=1"; } }
       $mkfolder .= DIRECTORY_SEPARATOR;
    }
}

// ---
// generate new password
// ---
function new_password($username,$email,$answer)
{
   GLOBAL $db,$table1;
   $username = trim($username);
   $email = trim($email);
   $answer = addslashes(trim($answer));
   $query = mysql_query("SELECT * FROM $table1 WHERE userName = '$username' AND userMail = '$email' AND userAnswer = '$answer'");
   if(mysql_num_rows($query) < 1)
   {
      return "error=wrong answer";
   }
   $rand_string = '';
   // ---
   // generating a random 8 chars length password
   // ---
   for($a=0;$a<7;$a++)
   {
      do
      {
         $newrand = chr(rand(0,256));
      } while(!eregi("^[a-z0-9]$",$newrand));
      $rand_string .= $newrand;
   }
   $pwd_to_insert = md5($rand_string);
   $new_query = mysql_query("UPDATE $table1 SET userPassword = '$pwd_to_insert' WHERE userName = '$username' AND userMail = '$email'");
   if(!$new_query)
   {
      return "error=unable to update value";
   }
   return "userName=$username&new_pass=$rand_string";
}

// ---
// decisional switch
// ---
if(isset($HTTP_POST_VARS["action"]))
{
   switch($HTTP_POST_VARS["action"])
   {
      case "register":
         $result = register($HTTP_POST_VARS['username'],$HTTP_POST_VARS['pass'],$HTTP_POST_VARS['email'],$HTTP_POST_VARS['question'],$HTTP_POST_VARS['answer']);
         print $result;
         break;
      case "login":
         $result = login($HTTP_POST_VARS['username'],$HTTP_POST_VARS['pass']);
         print "user=" . $result;
         break;
      case "login2":
         $result = login2($HTTP_POST_VARS['username'],$HTTP_POST_VARS['pass']);
         print "user=" . $result;
         break;
      case "forget":
         $result = forget($HTTP_POST_VARS['email']);
         print $result;
         break;
      case "new_password":
         $result = new_password($HTTP_POST_VARS['username'],$HTTP_POST_VARS['email'],$HTTP_POST_VARS['answer']);
         print $result;
         break;
      case "bio":
 $result = bio($HTTP_POST_VARS['username']);
 print $result;
 break;
      case "profile":
 $result = $HTTP_POST_VARS['username'];
 make_pro_dir('profiles/'.$result);
 break;
   }
}
?>

 

The weirdest part about all of it is that when I run the register function, it is returning the query as OK as if it was successful, but it wasn't! Arghhh....

 

There could be 2 possibilities off the top of my head.

1) The MySQL functions changed in php5 so I need to migrate the code to php5.

2) The code is fine, but Flash isn't communicating with php5 properly. As this is a flash based website, this might be the problem and really has nothing to do with MySQL.

 

The Flash communications are located at the bottom of the code above. I don't think this is the reason but at this point I really have no clue as to why this script works fine under php4 but won't let me do squat under php5. And I will repeat this, php5 completely supports MySQL on the server as I am able to connect to MySQL still. Any help would be greatly appreciated. Thank you in advance.

Link to comment
https://forums.phpfreaks.com/topic/68298-solved-cant-readwrite-to-mysql/
Share on other sites

Have you tried printing out your queries?  Instead of

 

$query = mysql_query("INSERT INTO $table1 (userName,userPassword,userMail,userQuestion,userAnswer) VALUES "
   ."('$username','$pass','$email','$question','$answer')");

 

use

 

$sql = "INSERT INTO $table1 (userName,userPassword,userMail,userQuestion,userAnswer) VALUES "
   ."('$username','$pass','$email','$question','$answer')";
print "About to execute $sql<br>";
$query = mysql_query($sql);

After a lot more testing I came to the conclusion that this has nothing to do with PHP to MySQL. For some reason, Flash is no longer communicating with PHP after I upgraded to PHP5. So it lies somewhere in the following code:

 

// ---
// decisional switch
// ---
if(isset($HTTP_POST_VARS["action"]))
{
   switch($HTTP_POST_VARS["action"])
   {
      case "register":
         $result = register($HTTP_POST_VARS['username'],$HTTP_POST_VARS['pass'],$HTTP_POST_VARS['email'],$HTTP_POST_VARS['question'],$HTTP_POST_VARS['answer']);
         print $result;
         break;
      case "login":
         $result = login($HTTP_POST_VARS['username'],$HTTP_POST_VARS['pass']);
         print "user=" . $result;
         break;
      case "login2":
         $result = login2($HTTP_POST_VARS['username'],$HTTP_POST_VARS['pass']);
         print "user=" . $result;
         break;
      case "forget":
         $result = forget($HTTP_POST_VARS['email']);
         print $result;
         break;
      case "new_password":
         $result = new_password($HTTP_POST_VARS['username'],$HTTP_POST_VARS['email'],$HTTP_POST_VARS['answer']);
         print $result;
         break;
      case "bio":
 $result = bio($HTTP_POST_VARS['username']);
 print $result;
 break;
      case "profile":
 $result = $HTTP_POST_VARS['username'];
 make_pro_dir('profiles/'.$result);
 break;
   }
}

 

Something in the above is not working in PHP5 as it did in PHP4....

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.