Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetching bit field with mysql_query()

Tags:

php

mysql

Here's my code:

$query = "SELECT Username, EmailVerified, Blocked FROM user";
$result = mysql_query($query, $link);
$row = mysql_fetch_assoc($result);
print_r($row);

Field Username is string and EmailVerified and Blocked are of type bit. The line print_r($row) displays the value of Username field but not the other two. I tried mysql_fetch_object(), mysql_fetch_row(), mysql_fetch_array() also, but same result.

Can't we fetch bit fields with mysql_query()?

like image 846
Kumar Kush Avatar asked Dec 09 '22 03:12

Kumar Kush


2 Answers

I think you need to cast the BIT field to an integer ->

SELECT Username, CAST(EmailVerified AS unsigned integer) AS EmailV, CAST(Blocked AS unsigned integer) AS Block FROM User
like image 67
nicky77 Avatar answered Jan 05 '23 01:01

nicky77


Yes you can but they are retrieved as strings, and most likely end up being unprintable characters. You can get the values as numbers like so:

$query = "SELECT Username, EmailVerified, Blocked FROM user";
$result = mysql_query($query, $link);
$row = mysql_fetch_assoc($result);

$row['EmailVerified'] = ord( $row['EmailVerified'] );
$row['Blocked'] = ord( $row['Blocked'] );
print_r($row);
like image 31
Esailija Avatar answered Jan 05 '23 02:01

Esailija