Error checking registry

0

My problem is when checking if there is a record in the database

$category_id = $_POST["category_id"]; 
    $name = $_POST["name"];
    $lastname = $_POST["lastname"];
    $address = $_POST["address"];
    $email = $_POST["email"];
    $phone = $_POST["phone"];
    $created_at = date('Y-m-d');

    $veri = "SELECT count(*) FROM medic WHERE name = '$name' ";
    $result = $db->select($veri); 
    $row = $result->fetch_row();
    $user_count = $row[0];
    if($user_count>0) {echo "1";}
    else {
        $query = "INSERT INTO medic (name, lastname, email, address, phone, created_at, category_id) VALUES  ('$name',
                     '$lastname',
                     '$email',
                     '$address',
                     '$phone',
                     '$created_at',
                     '$category_id')";
        $r = $db->select($query);
        echo "Datos Insertado";
    }

I get the error:

  

"Trying to get property of non-object in"   I have no idea what may be failing

    
asked by Hernandez 28.02.2017 в 15:44
source

2 answers

0

You still have to add an alias in the query.

Try changing:

$veri = "SELECT count(*) FROM medic WHERE name = '$name' ";

by:

$veri = "SELECT count(*) as total FROM medic WHERE name = '$name' ";

Then you get the value like this:

$row = $result->fetch_assoc();
$user_count = $row["total"];
    
answered by 28.02.2017 в 16:01
0

Since you're using fetch_row I guess you're using mysqli . In that case, I commented that there is no select method for mysqli. (Not for PDO, actually)

Where it says

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

I should say

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

Otherwise you will be assigning a null value to $ result, and trying to execute the fetch_row method on something that is not an object throws at you the error you are seeing.

Fixed the above, you will have a second problem where you do

$r = $db->select($query);

When in reality it should be

$r = $db->query($query);
    
answered by 28.02.2017 в 21:49