我有一个小脚本从csv文件上传数据到mysql数据库,我想检查csv文件中的值列表。
这是用于检查来自csv的值是否存在于db:
中的CSV文件code,alert_quantity
12345,10
这是我的项目PHP文件:
<?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;
}
?>
答案 0 :(得分:0)
我不知道你在这做什么,但它不会起作用:
$sql = "SELECT COUNT(*) AS count_no FROM products WHERE code = ?";
$count = $sql;
if($count > 0){
return true;
这相当于:
if ("SELECT COUNT(*)..." > 0)
永远不会是真的。