How do I find the first column with no data?
//column pet1='dog'
//column pet2=''
//column pet3=''
//column pet4='cat'
if($F=="getempty"){
$uid=$_GET["uid"];
$sql = mysql_query("SELECT DISTINCT pet1,pet2,pet3,pet4 FROM a WHERE uid='".$uid."' AND pet1='' OR pet2='' OR pet3=''OR pet4=''");
while($column=mysql_fetch_field($sql)){
$empty=$column->name;
echo json_encode(array("empty"=>$empty));
}
}else{}
I keep trying but so far it just returns all column names if one is empty
pet1,pet2,pet3,pet4
DISTINCTwill combine the rows which are exactly alike (having all the selected columns the same) and return them.To get the name of the first empty column, use a query like:
The idea is that the
CASEstatement checks each one in order, and finding an empty one returns the column name without checking the rest of them.Don’t forget to escape
$uidwithmysql_real_escape_string()before passing it to the query.Since we’re now getting the column name inside a field called
firstempty, we’ll also need to change the way it’s fetched: