I need some help with PHP & Mysql. Can I use MySQL if-statement
instead of this. thanks.
if ($last_id > 0) {
$query = mysql_query("select * from chat where chat.to = '".$_SESSION['user_id']."' and id < '".$last_id."'");
}else{
$query = mysql_query("select * from chat where chat.to = '".$_SESSION['user_id']."' ");
}
You can simplify this like
$last_id_query = '';
if ($last_id > 0) {
$last_id_query .= " and id < '".$last_id."'";
}
$query = mysql_query("select * from chat where chat.to = '".$_SESSION['user_id']."' ".$last_id_query);
You can write query with condition.
if ($last_id > 0) { $query="select * from chat where chat.to = '".$_SESSION['user_id']."' and id < '".$last_id."'";
}else{$query = "select * from chat where chat.to = '".$_SESSION['user_id']."' ");}
$query = mysql_query($query);
You can simplify it more like this
$query_part = '';
if ($last_id > 0) {
$query_part = "and id < '".$last_id."'";
$query = mysql_query("select * from chat where chat.to = '".$_SESSION['user_id']."' ".$query_part.");