0

I'm not sure if my title is even worded correctly. Hopefully I can explain this in a concise manner. I'm trying to select all from a database:

if (! empty ( $fname ) && ! empty ( $lname )) {
        $query5 = "select * from " . $db_prefix . "customer_det where (fname = '" . $fname . "' and lname = '" . $lname . "')";
            $result5 = $mysqli->query ( $query5 ) or die ( mysql_error () );
    } else {
        $query5 = "select * from " . $db_prefix . "customer_det where phone = '" . $phone . "'";

        $result5 = $mysqli->query ( $query5 ) or die ( mysql_error () );

    }

Then my next statement basically says if $result5 === NULL then insert stuff. Problem is I'm var_dump($result5) and it looks like it's printing out an array.

object(mysqli_result)#4 (5) { ["current_field"]=> int(0) ["field_count"]=> int(34) ["lengths"]=> NULL ["num_rows"]=> int(0) ["type"]=> int(0) }

Basically what I'm trying to say is how can I write an IF $result5 = non existent statement. Is it a count or num_rows function im looking for here?

1 Answer 1

1

mysqli_query() returns a mysqli_result object.

You want to check one of it's properties not for null, for example:

if ($result5->num_rows == 0)

Note: I also encourage you to read about SQL Injection.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Jason I'll go ahead and try that. This is the first time using the improved MySQLi module after three weeks using PHP 4.8 without MySQLi, etc
No worries. If you are new mysqli, you may want to jump straight to PDO. Be a better PHP Developer.
Jason, I read your comments on that blog. Why do you advocate PDO over MySQLi? I've seen a little documentation on both and I have an entire volume of OReilly and Apress books on both but I'm curious if there's more benefits with PDO. I did read that the PDO connector does work with a number of databases. I have read all about SQL injection and sanitizing input. I have some form validation regarding the input boxes. I've seen the real escape functions, etc. Fortunately or unfortunately, what I'm coding is in a protected area. A user account would have to be compromised first.
Why don't you move your comment to that post and we'll continue our discussion there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.