比较提交Vs数据库的价值

时间:2011-08-02 23:40:33

标签: php mysql warnings

“dbcred.php”类

<?php
   # pdo_testdb_connect.php - function for connecting to the "test" database

   function testdb_connect ()
   {
     $dbh = new PDO("mysql:host=localhost;dbname=dbname", "root", "");
     return ($dbh);
   }
?>

PHP

<?php
#connect mysql
require_once "dbcred.php";
$dbh = testdb_connect ();

session_start(); 
$username = $_POST['regduser']; 
$userpass = md5($_POST['regdpass']); 
$result = mysql_query("SELECT * FROM Student WHERE regduser= '$username' AND regdpass = '$regdpass'");

if (mysql_num_rows($result)!= 1) {   $error = "Login failed"; 
 #include "loginform.php"; 
} else { 
 $_SESSION['username'] = "$username"; 
 $_SESSION['ip'] = $_SERVER['REMOTE_ADDR']; 
 #include "membersection.php"; 
}
?>

HTML

  <form action="inc/check_regUsr.php" method="post" id="userLogon">
        <div class="field required">
        Username: <input type="text" name="regduser" tabindex="1" /><br />
        </div>
        <div class="field required">
        Password: <input type="text" name="regdpass" tabindex="2" /><br />
        </div>
        <input type="submit" name="submitUser" />
    </form>

当我尝试提交有效凭据时,它会说:警告:mysql_num_rows()要求参数1为资源,第12行中给出布尔值

为什么它不喜欢这个,我该如何解决?

谢谢

1 个答案:

答案 0 :(得分:1)

$result = mysql_query("SELECT * FROM Student WHERE regduser= '$username' AND regdpass = '$regdpass'");

我不知道你的数据库类是如何工作的。所以我将编写一个通用的mysql连接代码。

mysql_connect($host,$username,$password);
mysql_select_db($db);

不,你可以执行查询。