4

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()?

1
  • What does it display for those two? They should be 0 or 1 Commented Jan 8, 2012 at 15:55

3 Answers 3

8

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
Sign up to request clarification or add additional context in comments.

Comments

4

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);

3 Comments

Thanks. That worked. Yet, I would prefer nicky77's approach.
@kush.impetus cool, they are still strings though so you need to convert to number anyway in PHP after that query if you were to use them :P
@Esalija: You are right. But you know, they will costly operations in terms of CPU time, in case it counts.
2

Instead of using BIT and converting it each time you need to use, you can use BOOL (which is already a TINYINT) and store TRUE (1) or FALSE (0) values.

2 Comments

I am sorry but, I am using Navicat for MySQL version 8.0.26 for managing my MySQL databases. I don't see any datatype named BOOL there. That might be because my version of Navicat is older. I will see to it. Thank anyways.
@kush the BOOL type is just an alias to TINYINT(1), so you can use this type in your field.

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.