Patrick Patrick - 26 days ago 9
PHP Question

How can I check if there is a record with this ID AND DATE php?

Have a table with street_id, date fields. And I want to check if the date already exist for the street_id before inserting a new record.

$is_exist = mysql_query("SELECT street_id, date FROM streets_dates WHERE street_id='".$_GET['street']."' AND date='".$ize[$i]." LIMIT 1 ");
$row = mysql_fetch_row($is_exist);
if ($row[0] > 0)
{
echo "Already exist. ";
}else{
mysql_query("INSERT INTO streets_dates(street_id, date) VALUES ('".$_GET['street']."', '".$ize[$i]."')");
echo "Success... ";
}



Gives error: Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given

Answer

The message:

Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given

Always means that the query has failed!

When a mysql_query() succeeds it returns a mysql_resource handle and when it fails it returns FALSE (a boolean)

You should always check for this situation, at least while you are developing by checking the variable

$is_exist = mysql_query("SELECT street_id, date 
                        FROM streets_dates 
                        WHERE street_id='".$_GET['street']."' 
                        AND date='".$ize[$i]." LIMIT 1 ");
if ( ! $is_exist ) {
    echo mysql_error();
    exit;
}
$row = mysql_fetch_row($is_exist);

Your error or possibly 2 errors are in your string concatenation when building the query and a possible spelling error

$is_exist = mysql_query("SELECT street_id, date 
                        FROM streets_dates 
                        WHERE street_id='".$_GET['street']."' 
                        AND date='".$ize[$i]." LIMIT 1 ");
//----------------------------------^ (possible spelling error)
//--------------------------------------------^ (missing single quote)

Simplified string concatenation

$is_exist = mysql_query("SELECT street_id, date 
                        FROM streets_dates 
                        WHERE street_id='{$_GET['street']}' 
                        AND date='{$size[$i]}' LIMIT 1");