0

Possible Duplicate:
Check if value exist in mysql

I have a small script to upload data to mysql database from a csv file and I want to check the list of values that are inside of the csv file.

This is the CSV File to check if values from csv exist in db:

code,alert_quantity
12345,10

This my proeject PHP File:

 <?php
    $link_id = mysql_connect("localhost", "root", "")
               or die("Could not connect.");

    if(!mysql_select_db("database",$link_id))
    die("database was not selected.");

    function _checkIfCodeExists($code){

    $sql        = "SELECT COUNT(*) AS count_no FROM products WHERE code = ?";
    $count      = $sql;
    if($count > 0){
        return true;
    }else{
        return false;
    }
    }

    function _updateData($line_of_data){
    $code            = $line_of_data[0];
    $newAlert       = $line_of_data[1];

    $sql = "UPDATE ";
}

    $file_handle = fopen("file.csv", "r");

    while (($line_of_data = fgetcsv($file_handle, 1000, ",")) !== FALSE) {

    $query = mysql_query("SELECT * FROM products WHERE code ='$line_of_data[0]'") or die(mysql_error());
    $message = '';
    $count   = 1;

    if(_checkIfCodeExists($line_of_data[0])){
        try{
            _updateData($_data);
            $message .= $count . '> Success:: Product with code  (' . $line_of_data[1] . ') Exist (' . $line_of_data[0] . '). <br />';

        }catch(Exception $e){
            $message .=  $count .'> Error:: While updating alert (' . $line_of_data[1] . ') of code (' . $line_of_data[0] . ') => '.$e->getMessage().'<br />';
        }
    }else{
        $message .=  $count .'> Error:: Product code   (' . $line_of_data[0] . ')   Doesnt exist<br />';
    }
    $count++;
echo $message;
}


?>
Community
  • 1
  • 1
Dar
  • 159
  • 1
  • 4
  • 15
  • I dont know how to use pdo, if you know how, please redo the connection in PDO. Thanks – Dar Jan 15 '13 at 19:58
  • It takes all of thirty minutes to [learn how to use PDO](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/). There's really no excuse. – tadman Jan 15 '13 at 20:12
  • valor has not existed since about 1359 AD (or CE as they like to say nowadays). – Buttle Butkus May 21 '13 at 18:33

1 Answers1

0

I don't know what you're doing here, but it isn't going to work:

$sql        = "SELECT COUNT(*) AS count_no FROM products WHERE code = ?";
$count      = $sql;
if($count > 0){
    return true;

This is equivalent to:

if ("SELECT COUNT(*)..." > 0)

That will never be true.

tadman
  • 194,930
  • 21
  • 217
  • 240
  • Just i want to create a simple csv request from table and see if the value requested exist. Thanks – Dar Jan 15 '13 at 20:18
  • You're going to have to fix mistakes like this before anything resembling that is going to happen. – tadman Jan 15 '13 at 20:26