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:
if (mysql_num_rows(开发者_运维问答$login) == 1){
Here is the php, The php does add the user to the database. 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
.
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.
mysql_query()
returns True/False for INSERT, UPDATE, DELETE, DROP...
精彩评论