Search code examples
mysqlmagentorowfetchfgetcsv

How to fetch single row/data from Mysql_fetch_array?


I am reading .csv file through PHP using fgetcsv(). Now I am able to fetch I mean read data from .csv file. Then I need to check SKU column with my fetch result & accordingly have to perform either Insertion Or Updation. Suppose SKU is already present there, then I have to update my row in table else I need to insert new record. I have write following code...Plz check n tell me where I am doing mistake-:

<?php
$row = 1; 
if (($handle = fopen("localhost/data/htdocs/magento/var/import/Price.csv", "r")) !== FALSE) 
{     
    while (($data = fgetcsv($handle, 8000, ",")) !== FALSE) 
    {         
        $num = count($data);         
        echo "<p> $num fields in line $row: <br /></p>\n";           
        for ($c=0; $c < $num; $c++) 
        {             
            $temp = $data;             
            $string = implode(";", $temp);           
        }
            $pieces = explode(";", $string);             
            $col1 = $pieces[0];  
            $col2 = $pieces[1];
            $col3 = $pieces[2];      
            $col4 = $pieces[3];             

            $db_name = "magento";
                        $con = mysql_connect("localhost", "magento", "password");
                     If (!$con)
            {
                die('Could not connect: ' . mysql_error());
                mysql_close($con);
            }
            $seldb = mysql_select_db($db_name, $con);

            $query_fetch = "SELECT `sku` from `imoprt_prices`";
            $result_fetch = mysql_query($query_fetch);
            $num_rows = mysql_num_rows($result_fetch); 

            for($i = 0; $i < $num_rows; $i++) 
            { 
                $value = mysql_result($result_fetch, i, 'sku');
                if(strcmp('".$value."', '".$col2."') == 0)
                {               
                    $flag = 1;
                    break;
                }
                else
                {
                    $flag = 0;
                        break;
                }
            }
                if($flag == 1)
                {
                    $query_upadte = "(UPDATE imoprt_prices SET customer_id= '".$col1."', sku ='".$col2."', price= '".$col3."', website= '".$col4."'
                    )";
                    mysql_query($query_upadte);
                        $row++;         
                }
                if($flag == 0)
                {
                    mysql_query("INSERT INTO `imoprt_prices`(`customer_id`,`sku`,`price`,`website`) VALUES('".$col1."','".$col2."','".$col3."','".$col4."')");
                    $row++;   
                } 

    } 
} 
?>

Solution

  • If you have an actual UNIQUE index on your imoprt_prices table, you can use the ON DUPLICATE KEY UPDATE syntax and simplify your code a bit to something similar to; (note, can't test, so see as pseudo code)

    $db_name = "magento";
    $con = mysql_connect("localhost", "magento", "password") or die(mysql_error());
    $seldb = mysql_select_db($db_name, $con);
    
    if (($handle = fopen("localhost/data/htdocs/magento/var/import/Price.csv", "r")) !== FALSE) 
    {     
        while (($data = fgetcsv($handle, 8000, ",")) !== FALSE) 
        {
            $col1 = $pieces[0];  
            $col2 = $pieces[1];
            $col3 = $pieces[2];      
            $col4 = $pieces[3]; 
    
            $query_upadte = "INSERT INTO imoprt_prices (customer_id, sku, price, website) ".
                "VALUES('".$col1."','".$col2."','".$col3."','".$col4."') ".
                "ON DUPLICATE KEY UPDATE customer_id='".$col1."', price='".$col3.
                "',website='".$col4."'";
            mysql_query($query_upadte);
        }
    }
    

    You may also want to either mysql_real_escape_string() or use parameterized queries to make sure there's no ' in your inserted values though. That is always a danger with building sql strings like this.

    Simple demo here.