0

Background:

I have a form that I use to add information to a database and everything works fine.

I tried to get a users id by his username(filled in the form)

this is the users database:

users database

The Problem:

When I try to get a result from the database I get an error, my $result is apparently not an object type but a bool.

here is my code:

$sql1 = "SELECT idUsers FROM users WHERE uidUsers=?;";
        $stmt1 = mysqli_stmt_init($conn);
        if(!mysqli_stmt_prepare($stmt1, $sql1)){
        header("Location:../add_sale.php?error=sqlerror");
        exit();
        }
        else{
            mysqli_stmt_bind_param($stmt1, "s", $name);
            $result = mysqli_query($conn, $sql1);
                if($result->num_rows === 0){
                    header("Location:../add_sale.php?error=nouser");
                    exit();
                    }
                else{
                    while($row = mysqli_fetch_assoc($result)){
                        $a = $row["idUsers"];
                    }

after that I insert all the wanted data and $a.

the script gets this errors:

Notice:Trying to get property 'num_rows' of non-object in ...
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, bool given in ...
Dharman
  • 30,962
  • 25
  • 85
  • 135
Meon
  • 47
  • 9
  • 1
    I think it needs to read `$result = mysqli_stmt_execute($stmt1);` rather than calling `mysqli_query()` which will ignore your preparing. You get the error because that isn't a valid query without the `prepare()` and `bind_param()` calls. – droopsnoot Apr 27 '20 at 11:41
  • Correct duplicate [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Apr 27 '20 at 11:46

0 Answers0