I'm new to PHP/SQL and very confused.
In my SQL database, I have the following values for a user:
In my PHP script, echoing these values gives me 0.
Query:
$query = "SELECT * FROM user WHERE email = '{$email}'";
$result = mysqli_query($con, $query);
$row = mysqli_fetch_array($result);
I specify the email linked to the above user in the database. I get the desired row as a result (user ID 13 and these null values). Still, the code gets inside this if statement:
if (!empty($row['first_name']) && !empty($row['last_name']) &&
!empty($row['age']) && !empty($row['picture_path'])) {
echo "inside";
}
Echoing the values themselves gave me 0 instead of null. Why? And how can I properly check for empty/null?
Thanks loads.
You can just assign a certain string if a certain value is null using COALESCE()
function. See below:
"SELECT
COALESCE(user_id,'null')user_id,
COALESCE(first_name,'null')first_name,
COALESCE(middle_name,'null')middle_name,
COALESCE(last_name,'null')last_name,
COALESCE(rs_status,'null')rs_status,
COALESCE(age,'null')age,
COALESCE(town,'null')town,
COALESCE(phone,'null')phone,
COALESCE(picture_path,'null')picture_path
FROM user
WHERE email = '{$email}'"
PHP empty('0')
returns true
; to test for null use is_null($var)
.
Not intuitive but that's what it is.