How to check if field in database is empty?

Hi,im trying to make advanced registration system, with ‘‘Home town’’ not required,so at profile page ‘‘Home town: blabla’’ will show only if field in database is set.I have field in users called home_town and its empty,but it still shows ''Home town: ‘’ how do i check if field is empty or not?

Tried something like this:

[php]$home_town_query = mysql_query(“SELECT home_town FROM users WHERE user_id=’$my_id’”);
if(mysql_num_rows($home_town_query) <> 0) {
echo 'Home town: '.$profile_data[‘home_town’];
}[/php]

It gets all variables,and everything is working perfectly,i think i must change that mysql_num_rows but to what? Thx for advice.

Well, you really should be using mysqli or PDO, but if it is OK with you that in the future your script won’t work or gets hacked then continue using mysql. As for you answer just check to see if the variable isn’t empty

[php]if (!empty($row)) {
// Display or do whatever you want…
}[/php]

or

[php]if (isset($row)) {
// Is true if $row is set hence isset function/method :wink:
}[/php]

[php]<?php

$my_var = NULL;

if (!empty($my_var)) {
echo ‘is not empty’ . $my_var . ‘
’;
}

if (isset($my_var)) {
echo 'isset ’ . $my_var . ‘
’;
}

$my_var = “Hey, I contain something”;

if (!empty($my_var)) {
echo 'is not empty ’ . $my_var . ‘
’;
}

if (isset($my_var)) {
echo 'isset ’ . $my_var . ‘
’;
}[/php]

I personally like the isset method better for then you are almost 99 percent certain that it will do what it is expected to do. However, both should work fine for what you want it to do.

Okay,but im very unfamiliar with mysqli,and having problem changing from mysql to mysqli,could anyone change my whole website to mysqli? :smiley:

of course, you could try posting it in the “programmer for hire” section

Sponsor our Newsletter | Privacy Policy | Terms of Service