0

I am very new to PHP.

Can some one please verify this code and tell me what is the issue.

    <?php
    require_once 'excel_reader2.php';
    $data = new Spreadsheet_Excel_Reader("NZ_Price_List.xls");
    error_reporting(E_ALL ^ E_NOTICE);
    //start mysql connection 
    $con=mysqli_connect("localhost","root","ebooks3","newlocalabc");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
    for($i=1;$i < $data->rowcount();$i++)
    {
     $p = $data->val($i,'A');
     $sql = sprintf("select count(*) as cnt from products where `isbn` = '%s'", mysql_real_escape_string($p));
     $row_dup = mysql_fetch_assoc(mysql_query($sql,$con));
     if ($row_dup['cnt'] == 0) {

            //insert
        }
else{
//update
}
    }

    echo "hello done";
    mysqli_close($con);

    ?>

I am trying to compare the values from an Excel spreadsheet to database. If the value is already present then just update or if the value is not there then I have to insret the row.

I am getting the following error message:

mysql_fetch_assoc() expects parameter 1 to be resource, null given

Some please let me asap.

Marc Audet
  • 46,011
  • 11
  • 63
  • 83
user2636163
  • 39
  • 1
  • 5
  • 5
    You are mixing mysql and mysqli database calls, you will need to fix that for a start. They should all be mysqli based on your connection – bumperbox Aug 26 '13 at 00:11
  • possible duplicate of [Insert to table or update if exists (MySQL)](http://stackoverflow.com/questions/4205181/insert-to-table-or-update-if-exists-mysql) – George Yates Aug 26 '13 at 00:12

2 Answers2

0

You are using a mysqli connection, so you need to use mysqli_ functions, not mysql_. So your code should be -

 $sql = sprintf("select count(*) as cnt from products where `isbn` = '%s'", mysqli_real_escape_string($con,$p));
 $row_dup = mysqli_fetch_assoc(mysqli_query($con,$sql));
Sean
  • 12,443
  • 3
  • 29
  • 47
-1
$num = mysql_num_rows(mysql_query($sql,$con));
Andy G
  • 19,232
  • 5
  • 47
  • 69
Orel Eraki
  • 11,940
  • 3
  • 28
  • 36