2011-05-29 19 views
0

我想在MySql數據庫中運行查詢,但我每次都有相同的警告。有人可以幫忙嗎?幫助我有一個錯誤消息使用PHP「警告:mysql_num_rows():提供的參數是不是一個有效的MySQL結果資源...」

警告:mysql_num_rows():提供的參數不是在/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/public_html/login.php一個有效的MySQL結果資源上線279 您提供的用戶名不存在!

我強調線279 ++++++++++++ $ NUM = mysql_num_rows($水庫); ++++++++++++

請讓我知道我該如何糾正這個問題。

<?php 
    //If the user has submitted the form 
    if($_POST['submit']) { 
    //protect the posted value then store them to variables 
    $username = protect($_POST['username']); 
    $password = protect($_POST['password']); 

    //Check if the username or password boxes were not filled in 
    if(!$username || !$password) { 
     //if not display an error message 
     echo "<center>You need to fill in a <b>Username</b> and a <b>Password</b>!</center>"; 
    } else { 
     //if the were continue checking 

     //select all rows from the table where the username matches the one entered by the user 
     $res = mysql_query("SELECT * 
          FROM `users` 
          WHERE `username` = '".$username."'"); 
     ++++++++++++++$num = mysql_num_rows($res);++++++++++++++++++ 

     //check if there was not a match 
     if($num == 0) { 
     //if not display an error message 
     echo "<center>The <b>Username</b> you supplied does not exist!</center>"; 
     } else { 
     //if there was a match continue checking 

     //select all rows where the username and password match the ones submitted by the user 
     $res = mysql_query("SELECT * FROM `users` 
          WHERE `username` = '".$username."' 
           AND `password` = '".$password."'"); 
     $num = mysql_num_rows($res); 

     //check if there was not a match 
     if($num == 0) { 
      //if not display error message 
      echo "<center>The <b>Password</b> you supplied does not match the one for that username!</center>"; 
     } else { 
        //if there was continue checking 

        //split all fields fom the correct row into an associative array 
        $row = mysql_fetch_assoc($res); 

        //check to see if the user has not activated their account yet 
        if($row['active'] != 1){ 
         //if not display error message 
         echo "<center>You have not yet <b>Activated</b> your account!</center>"; 
        }else{ 
         //if they have log them in 

         //set the login session storing there id - we use this to see if they are logged in or not 
         $_SESSION['uid'] = $row['id']; 
         //show message 
         echo "<center>You have successfully logged in!</center>"; 

         //update the online field to 50 seconds into the future 
         $time = date('U')+50; 
         mysql_query("UPDATE `users` SET `online` = '".$time."' WHERE `id` = '".$_SESSION['uid']."'"); 

         //redirect them to the usersonline page 
         header('Location: usersOnline.php'); 
        } 
       } 
      } 
     } 
    } 

    ?> 
+0

保護功能在做什麼? – Ibu 2011-05-29 22:14:14

+0

您的錯誤表示您的查詢不正確 – Ibu 2011-05-29 22:14:52

+0

可能的重複[警告:mysql_fetch_ *期望參數1是資源,布爾給定錯誤](http://stackoverflow.com/questions/11674312/warning-mysql-fetch-expects-參數-1要資源布爾錯誤) – 2012-08-02 12:54:23

回答

0

可能您的查詢失敗。如果發生錯誤,則mysql_query()返回false。使用mysql_error()來確定真正的問題。

$res = mysql_query("SELECT * FROM `users` WHERE `username` = '".$username."'"); 
if ($res === false) { 
    echo mysql_errno() . ': ' . mysql_error(); 
    exit; 
} 
$num = mysql_num_rows($res); 
+0

只是告訴我,1146:表'XXXXXXXXXXXXXXXXXXXX.users'不存在 – 2011-05-29 22:24:58

+1

然後可能表'XXXXXXXXXXXXXXX.users'不存在。它現在取決於你創建它(以及這樣)。 – KingCrunch 2011-05-29 22:25:36

+0

所有排序感謝這麼多:) – 2011-05-29 22:39:45

0

$ username有可能包含特殊字符嗎?你應該檢查mysql日誌,如果你的查詢從數據庫的角度來看是正確執行的。

0

作爲的mysql_query的documentation()規定:

對於SELECT,SHOW,描述,解釋 等語句返回 結果集,請求mysql_query()返回成功則 資源,或FALSE在 錯誤。

所以,只要檢查一下,在連接的數據庫中是否有這張表,列出的列和您的查詢是否可以 - 這可能只是您查詢的錯誤。

還有就是文檔中的例子(見開頭的鏈接),怎麼看實際上發生了什麼(見例#1,用自己的查詢替換):

$result = mysql_query('SELECT * WHERE 1=1'); 
if (!$result) { 
    die('Invalid query: ' . mysql_error()); 
} 
1

我不知道什麼保護功能就做什麼,但這裏是你可以做什麼:

$username = mysql_real_escape_string($_POST['username']); 
$password = mysql_real_escape_string($_POST['password']); 

,並在您的查詢,用於調試使用它來獲取正確的錯誤

$res = mysql_query("SELECT * 
        FROM `users` 
        WHERE 
        `username` = '$username' 
        AND 
        `password` = '$password'") or die('Error: '.mysql_error()); 
相關問題