Bluefire
Bluefire

Reputation: 14099

Return the string equivalent of a SELECT that only returns one value

I have a SELECT query that only returns one value (not one row, but just one "thing"). It looks like

SELECT IF( EXISTS( SELECT * FROM `Blah` WHERE <whatever> ), 1, 0);

I tried fiddling around with mysqli_result::fetch_assoc, mysqli_result::fetch_object and mysqli_result::fetch_array, and all of them are being really weird. Is there a mysqli_result::fetch_string for special cases like this that just outputs a string on which I can just call intval?

Upvotes: 1

Views: 77

Answers (2)

Jon
Jon

Reputation: 99

Aswell as casraf's answer, another option might be to incorporate a count into this - you might want to find out just how many blah's match your criteria later. Might be easier to read back when you re-visit your code in a years time, too.

SELECT count(*) cnt FROM `Blah` WHERE <whatever>

You'd be able to do a boolean check on the "cnt" variable (i.e. a count of 0 would == false) but also use it to find out how many matches you've got.

Upvotes: 3

casraf
casraf

Reputation: 21684

You can do:

SELECT IF( EXISTS( SELECT * FROM `Blah` WHERE <whatever> ), 1, 0) AS exists;

And use any of the functions above to get row number 0's 'exists' column.

Upvotes: 3

Related Questions