php mysql_num_rows() 根本不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6235528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mysql_num_rows() not working at all
提问by Mentalhead
I have this piece of PHP code:
我有这段PHP代码:
<?php
$username=$_POST['username'];
$password=$_POST['password'];
if($username&&$password){
$connect=mysql_connect("localhost","root","") or die(" Couldnt connect");
mysql_select_db("phplogin") or die ("Can't find database" .mysql_error());
$query=mysql_query("SELECT * users WHERE username='$username' ");
$numrows=mysql_num_rows($query);
if (!$query) {
die('Invalid query: ' . mysql_error());
}
}
else
die ("Enter username and password!") .mysql_error();
?>
However, when I try to run this code I get these errors:
但是,当我尝试运行此代码时,出现以下错误:
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\PHP testing\login.php on line 9
警告:mysql_num_rows() 期望参数 1 是资源,布尔值在 C:\wamp\www\PHP testing\login.php 第 9 行中给出
and
和
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'users WHERE username='alex'' at line 1
您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行的“users WHERE username='alex'”附近使用的正确语法
Can someone explain to me what I'm I doing wrong here?
有人可以向我解释我在这里做错了什么吗?
回答by Headshota
You must specify a table from which you're selecting with FROM keyword:
您必须使用 FROM 关键字指定要从中选择的表:
$query=mysql_query("SELECT * FROM users WHERE username='$username' ");
$numrows=mysql_num_rows($query);
回答by bumperbox
you should really check for errors after your query, then the system will tell you what is wrong
你真的应该在查询后检查错误,然后系统会告诉你什么是错误的
$query = mysql_query("SELECT * users WHERE username='$username' ");
if (mysql_error() {
die(mysql_error());
}
$numrows = mysql_num_rows($query);
as @mike commented, your select query is missing the from bit
正如@mike 评论的那样,您的选择查询缺少 from 位
"SELECT * FROM users WHERE username='$username' "
回答by mr_eclair
Well Your code is vulnerable to SQL InjectionAttack
那么你的代码容易受到SQL 注入攻击
$username=$_POST['username'];
$password=$_POST['password'];
instead of above use this code
$username= mysql_real_escape_string($_POST['username']);
$password=mysql_real_escape_string($_POST['password']);
回答by ankit chouksey
$connect = mysql_connect("localhost","root","") or die("Couldn't connect!");
mysql_select_db("phplogin") or die("Couldn't find db");
$result = mysql_query("SELECT * FROM admin", $connect);
$numrows = mysql_num_rows($result);
and it will evaluate resource
它将评估资源
回答by Sujit Agarwal
$query = mysql_query("SELECT * users WHERE username='$username' ");
if (mysql_error() {
die(mysql_error());
}
$numberOfRows = mysql_num_rows($query);
echo $numberOfRows;