2010-11-07 52 views
-2

解決方案:我指出了錯誤的數據庫感謝您的幫助。php/mysql select語句可以手動工作,但不能通過代碼

下面的計數返回0但是當我手動運行它有一個結果。

通過手動我的意思是複製我的代碼echo'd SQL並粘貼到mySQL命令。

<? 
$host="localhost"; // Host name 
      $username="userName"; // Mysql username 
      $password="userPW"; // Mysql password 
      $db_name="dbName"; // Database name 
      $tbl_name="userBase"; // Table name 

      // Connect to server and select databse. 
      $link=mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
      mysql_select_db("$db_name")or die("cannot select DB"); 

      // username and password sent from form 
      $user=$_POST['user']; 
      $pass=$_POST['pass']; 


      // To protect MySQL injection (more detail about MySQL injection) 
      $user = stripslashes($user); 
      $pass = stripslashes($pass); 
      $user = mysql_real_escape_string($user); 
      $pass = mysql_real_escape_string($pass); 

       $salt = substr($pass, 0, 1); 
       $encrypted_pswd = crypt($pass, $salt); 

      $sql="SELECT * FROM $tbl_name WHERE user=\"$user\" and pass=\"$encrypted_pswd\";"; 
      echo $sql."<br>"; 
      $result=mysql_query($sql); 




      // Mysql_num_row is counting table row 
      $count=mysql_num_rows($result); 
      echo "count=".$count."<br>"; 
    ?> 
+1

界定 「不工作」。 – 2010-11-07 19:38:34

+0

輸出不正確。應該是count = 1,但count = 0。不知道爲什麼 – 2010-11-07 19:39:28

+0

不確定這是否可行,但您可以嘗試在用戶和密碼周圍放置單引號。所以user ='$ user' – Matt 2010-11-07 19:42:16

回答

1

嘗試:

$sql = sprintf("SELECT * FROM %s WHERE user='%s' and pass='%s'", $tbl_name, $user, $encrypted_pswd); 
+0

它可以提供什麼幫助? – 2010-11-07 20:12:41