yanti Posted January 5, 2011 Share Posted January 5, 2011 HI everyone, Does anyone can help me to update db from .csv / .txt files into mysql using php script. My code is like below: link = mysql_connect($dbhost, $dbuser, $dbpassword) or die('Could not connect: ' . mysql_error()); mysql_select_db ($dbname); $handle = fopen("/var/www/httpdocs/molog.csv", "r"); // loop content of csv file, using comma as delemiter while (($data = fgetcsv($handle, 1000, ",")) !== false) { $timestamp = $data[1]; $mobile_no = (int)$data[2]; $message_text = ($data[3]); //$price2 = floatval($data[2]); $query = 'SELECT id_pengguna FROM test_maklumat_pengguna'; if (!$result = mysql_query($query)) { continue; } if ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { // entry exists update [b]$query = "UPDATE test_maklumat_pengguna SET no_tel_penjaga='$mobile_no', no_tel_murid='mobile_no' WHERE id=$id";[/b] mysql_query($query); if (mysql_affected_rows() <= 0) { // no rows where affected by update query } } else { // entry don't exists continue or insert... } mysql_free_result($result); } fclose($handle); mysql_close($link); My problem was, i want to update 2 fields which had same data type based on passport no of the customer. My code is wrong i guess. Please help me on this...thank you Link to comment https://forums.phpfreaks.com/topic/223445-update-db-from-csvtxt-files-using-php/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.