[英]php/mySQL error: mysql_num_rows(): supplied argument is not a valid MySQL result
I'm trying to INSERT INTO a mySQL database and I'm getting this error on: 我正在尝试插入一个mySQL数据库,我收到此错误:
if (mysql_num_rows($login) == 1){
Here is the php, The php does add the user to the database. 这是php,php确实将用户添加到数据库中。 I can't figure it out.
我无法弄清楚。
<?
session_start();
require("config.php");
$u = $_GET['username'];
$pw = $_GET['password'];
$pwh = $_GET['passwordhint'];
$em = $_GET['email'];
$zc = $_GET['zipcode'];
$check = "INSERT INTO family (loginName, email, password, passwordhint, location) VALUES ('$u', '$pw', '$pwh', '$em', '$zc')";
$login = mysql_query($check, $link) or die(mysql_error());
if (mysql_num_rows($login) == 1) {
$row = mysql_fetch_assoc($login);
echo 'Yes';exit;
} else {
echo 'No';exit;
}
mysql_close($link);
?>
Thanks, 谢谢,
You're doing an INSERT
query, so $login
won't contain any result set, it's either true
or false
. 您正在执行
INSERT
查询,因此$login
将不包含任何结果集,它可以是true
或false
。
So instead of this: 所以不是这样的:
if (mysql_num_rows($login) == 1) {
You can either do this: 你可以这样做:
if (mysql_affected_rows($link) == 1) {
Or this: 或这个:
if ($login == true) {
Also, you don't need this line anymore: 此外,您不再需要此行:
$row = mysql_fetch_assoc($login);
Unrelated to your question, but a very important thing to remember when doing SQL queries: you'll need to escape all your $_GET
data with mysql_real_escape_string()
just before inserting, so as to prevent SQL injection . 与您的问题无关,但在进行SQL查询时要记住这一点非常重要:在插入之前,您需要使用
mysql_real_escape_string()
转义所有$_GET
数据,以防止SQL注入 。
mysql_query()
为INSERT,UPDATE,DELETE,DROP返回True / False ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.