我是新来的PHP,所以请裸照。我正在创建一个登录表单,用于查询数据库是否存在用户。这个脚本在我自己的服务器上正常工作,但是一旦我在我的大学服务器上尝试过,我在标题中遇到了错误。我认为这个问题与我的SQL查询有关,但我不太确定。任何帮助将不胜感激,谢谢。PHP的错误消息:mysql_num_rows()期望参数1是资源,布尔给定
<?php
$dbhost="localhost";
$username="v22comp_B09052";
$password="-";
$db="v22comp_B09051-12-13";
$table="secrets";
// Connect to the database
mysql_connect("$dbhost", "$username", "$password") or die ("Can not connect to the database");
mysql_select_db("$db") or die ("Database is not selectable");
//Post input
$inputusername=$_POST['uid'];
$inputpassword=$_POST['upassword'];
//Check input
$sql="SELECT * FROM $table WHERE username='$inputusername' and password='$inputpassword'";
$sqlop=mysql_query($sql);
//If a row matches, output a string informing the user that they are logged in. If not, output a string informing them that they are not
$rowmatch=mysql_num_rows($sqlop);
if($rowmatch==1)
{
echo "You are now logged in";
}
else
{
echo "Invalid ID or password";
}
?>
如果'mysql_query()'失败,则返回'FALSE',这不是一个有效的'资源'。这就是为什么当你尝试使用'mysql_num_rows()'时你收到错误。执行查询后检查错误。由'mysql_error()'提供的数据可能很有用。 – Havenard
你很容易遭受[SQL注入攻击](http://bobby-tables.com)。在学习如何防守编程之前,请停止使用此代码。 –
检查查询是否失败:'$ sqlop = mysql_query($ sql)or die('Error');' – WebNovice