2

How can I return the 1 or 0 as true and false for my select query? I've tried every combination of fetch_assoc, arrays, etc. It usually returns as null, which doesn't make sense.

$querydetails = "select exists (select * from customer_det where id = $uid)";
$resultdetails = mysql_query($querydetails) or die(mysql_error());
while ($row = mysql_fetch_assoc($resultdetails)) {
    $resultdetails1 = $row[0];
}

That's the latest version of my query. I guess it's not an array, but I'm not sure how to pull the value if there's no row to be named?

This is a post question to my previous thread: MySQL Update WHERE

Community
  • 1
  • 1
smada
  • 227
  • 1
  • 7
  • 17
  • possible duplicate of [The quick way to check if SELECT EXISTS(...) returns a value, using PHP?](http://stackoverflow.com/questions/4484974/the-quick-way-to-check-if-select-exists-returns-a-value-using-php) – Anirudh Ramanathan Nov 01 '12 at 13:41
  • You should switch to mysqli, mysql is deprecated. That means there is no support for it from now on, a better version exists, and people here will get mad if you post a question that is using it. – Asad Saeeduddin Nov 01 '12 at 13:41
  • I apologize is this is a duplicated question, I did not see that question posted. Nonetheless, I can return a 1 or a 0 while inserting that into the SQL query of phpMyAdmin, but I can't get it to return with the PHP – smada Nov 01 '12 at 13:44
  • @asad The php version running on the server does not support mysqli. I realize this is an issue, but hopefully someone will be updating it in the future. – smada Nov 01 '12 at 13:44

5 Answers5

9
SELECT IF(EXISTS(...),1,0) AS result
dev-null-dweller
  • 29,274
  • 3
  • 65
  • 85
3

simple use COUNT and IF

SELECT IF(COUNT(*) = 0, 0, 1)
FROM table1
WHERE s = 2
John Woo
  • 258,903
  • 69
  • 498
  • 492
0
SELECT count(*) FROM (
 SELECT id FROM table WHERE condition LIMIT 1
) AS result;
0

i am using mysql version 8.0.23 on AMS

SELECT
column1,
column2,
IF (exists(select * from MeasurementItemNos measureItem where measureItem.itemNo = ib.itemNo and measureItem.IsActive = 1),1,0) as isUploadedSizeSpec
from table
Keith POON
  • 86
  • 6
-1

I would do:

select * from customer_det where id = $uid LIMIT 1

You will get either 1 row or zero rows.

Brian
  • 8,418
  • 2
  • 25
  • 32