Error verifying if a record exists in the database

0

The following code gives me the error:

Notice : Trying to get property of non-object in ... What it does is verify if a user exists in the database.

$cate = $_POST['cat'];
$veri = "SELECT * FROM category WHERE name = '$cate' ";
$result = $db->select($veri); 
$numRowsNew = $result->num_rows;   
if($numRowsNew >= 1) {
    echo "El nombre de usuario ya existe.";
} else {
    echo "Disponible.";
}
    
asked by Josbert Hernandez 13.02.2017 в 02:23
source

2 answers

2

I think your fault is that it is not select , it is query .

Try as follows:

$cate = $_POST['cat'];
$sql = "SELECT * FROM category WHERE name = '$cate'";
$result = $conn->query($sql);

if ($result->num_rows >= 1) {
    echo "El nombre de usuario ya existe.";
} else {
    echo "Disponible.";
}
    
answered by 13.02.2017 в 12:37
2

At the moment of constructing the string of your query you must concatenate the variable so that it prints it within the same string:

$ cat = $ _POST ['cat'];
$ veri="SELECT * FROM category WHERE name = '". $ cat. "'";

Now, if you will use MySQLI for objects you must do:

$ result = $ db-> query ($ veri);

You can also use the prepare form to concatenate variables in your consumption:

// you must prepare the query substituting where the variable will go for a? ...
$ prepare = $ db-> prepare ('SELECT * FROM category WHERE name =?');
// now you dump the variables using bind_param where you must first indicate the type of variable you expect and then the variable ...
$ prepare-> bind_param ('s', $ cate);
// execute the query ...
$ prepare-> execute ();

With this you can easily use variables within the queries with the advantage that doing so with the prepare automatically applies a mysqli_real_escape_string

    
answered by 14.02.2017 в 02:25