mysql_num_rows () expects parameter 1 to be resource, object given in

0

I want to check if a user has already been registered in the database to not have duplicate users, but when I use mysqli_num_rows to know if there is any line with that user the error of the title.

<?php 
include("setting.php");

$user   = $_GET['user'];
$pas    = $_GET['pass'];
$check  = "SELECT * FROM user WHERE name='$user'";
$sql    = mysqli_query($mysqli, $check);
$ln     = mysql_num_rows($sql);

if ($ln == 0) {
    $code = "INSERT INTO users(id, name, pass, sp) VALUES (null, '$user', '$pass', 0)";
    $query = $mysqli->query($code);

    if ($query) {
        header("location: ../index.php");
    }
}else{
    echo "Este usuario já existe";
}

 ?>

I was able to solve this by saying:

<?php 
include("setting.php");
$user = $_GET['user'];
$pass = $_GET['pass'];

$sql = mysqli_query($mysqli, "SELECT * FROM user WHERE name='$user'");
$ln  = mysqli_num_rows($sql);

if ($ln == 0) {
    $query = mysqli_query($mysqli, "INSERT INTO user(id, name, pass, sp) VALUES(null, '$user', '$pass', 0)");
    header("location: ../index.php");
}
 ?>
    
asked by anonymous 16.08.2017 / 00:20

2 answers

0

You're mixing mysql with mysqli and procedural mode with object-oriented mode, which is wrong. Try to do everything in one way:

$check  = "SELECT * FROM user WHERE name='$user'";
$sql    = $mysqli->query($check);
$ln     = $sql->num_rows;
    
16.08.2017 / 00:28
0

mysql is obsolete in newer versions of php, so use mysqli

mysqli_num_rows($sql);

On the insert, if the id is a primary key it is not possible to make an null insert. Ideally the id would be Auto_increment and be primary key, so you do not need to give input.

Should be

INSERT INTO users (name, pass, sp) VALUES ('$ user', '$ pass', 0) ";

    
16.08.2017 / 00:23