Reputation: 3411
I have a simple command in php to test whether or not data exists in my mySQL database:
$query = "SELECT distinct name from table where number = '$part'";
$result = mysqli_query($con, $crossSupplierQuery);
I do a bunch of code after this, but I want to enclose the entire thing inside of an if statement so that if this initial query doesn't provide any results, I can skip over the rest of the code. I tried a simple if ($result) {
, but that doesn't work as $result
is always populated with SOMETHING. I tried digging deeper into the object it creates and I get this object:
object(mysqli_result)#2 (5) {
["current_field"]=>
int(0)
["field_count"]=>
int(1)
["lengths"]=>
NULL
["num_rows"]=>
int(0)
["type"]=>
int(0)
}
The value that determines whether or not the initial query found results is (I believe) held in ["num_rows"]=>
, but I can't access that using $result['num_rows']
since this is an object, not an array.
What syntax would I use to accomplish what I'm trying to do with my if
statement?
Upvotes: 0
Views: 234
Reputation: 171
You can access elements of an object using the following syntax:
echo $crossSupplierResult->{'num_rows'};
put curly braces around the element, (in this case, num_rows
)
Upvotes: 1