-1

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

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Patrick
  • 43
  • 1
  • 5
  • Gives error: Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given – Patrick Nov 14 '16 at 10:33
  • 1
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Nov 14 '16 at 10:35
  • _Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given_ **The query FAILED!** Add `if ( ! $is_exists ) { echo mysql_error(); exit;}` after the `mysql_query()` ___Then convert code to use `mysqli_` or `PDO`___ – RiggsFolly Nov 14 '16 at 10:36
  • try instead of mysql of mysqli – A. Jain Nov 14 '16 at 10:40
  • @Patrick please have a look at below. I have posted one suggestion to you. that definitely work in your case. – Pranav MS Nov 14 '16 at 11:16

2 Answers2

0

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");
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
0
<?php

$knownRecordCount=0;// new record
if( isset($con) && !empty($con) && $con!="" )
{ 
    $checkStmt=mysqli_prepare($con, "SELECT COUNT(*) FROM streets_dates WHERE street_id=? AND date=? ;");
    if( $checkStmt )
    {    
        mysqli_stmt_bind_param($checkStmt,"ds",$_GET['street'],$ize[$i]);
        mysqli_stmt_execute($checkStmt);
        mysqli_stmt_bind_result($checkStmt,$txt_RecordCount);
        mysqli_stmt_fetch($checkStmt);
        mysqli_stmt_close($checkStmt);
        $knownRecordCount=$txt_RecordCount;
    } 
}
    if ($knownRecordCount > 0)
    {
        echo "Already exist. ";
    }else{
        mysql_query("INSERT INTO streets_dates(street_id, date) VALUES ('".$_GET['street']."', '".$ize[$i]."')");
        echo "Success... ";
    }
 ?>

Here im using the mysqli binding concept.And using this im trying to find the count of the record as per my condition. If the count no is greater than 0 the i conclude that the record is already exist in my database.Else it is new data so ii have to insert that data.

Please try this definitely this will help you. This is working in my case. Please have a try on this.

Pranav MS
  • 2,235
  • 2
  • 23
  • 50
  • 1
    Why are you not using a parameterised query and binding values. Not much point preparing a concatenated query as it still leaves the query Wide Open to [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – RiggsFolly Nov 14 '16 at 11:32
  • @RiggsFolly extremely sorry for my mistake. – Pranav MS Nov 14 '16 at 11:44