Jump to content

UPDATE db from csv/txt files using php


yanti

Recommended Posts

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.