从 Mysql 过渡到 mysqli 以获取正在运行的 csv 导入文件
Transition from Mysql to mysqli for a csv import file that was working
我的托管商刚刚贬低了 mysql 个连接以支持 mysqli
这个特定的脚本是一个示例,但在我尝试转换到 Mysqli 之前运行良好。不幸的是,我从另一份工作安排 php 开始编程已经 4 年了,并且已经有一段时间没有看过这个了。所以请温柔一点:-)
我可以连接到数据库,但输出如下所示:
错误:INSERT INTO articles (name, reference) VALUES ('','')
我一定是对 $data 变量做错了什么,但没有任何线索,因为它适用于以前的版本。
感谢任何帮助
<?php
ini_set('auto_detect_line_endings',TRUE);
function clean($str, $default = '') {
if (!isset($str)) $str = $default;
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
$host_name = "some host";
$database = "some database";
$user_name = "someusername";
$password = "some password";
$connect = mysqli_connect($host_name, $user_name, $password, $database);
if (mysqli_connect_errno()){ echo "Failed to connect to MySQL: " . mysqli_connect_error(); } else {echo 'connected to DB<br />';}
$lines = $value1 = $value2 = $data = 0;
if ($_FILES["csv"]["size"] > 0) {
//get the csv file
$file = $_FILES["csv"]["tmp_name"];
$handle = fopen($file,"r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if ($data[0]) {
$sql = "INSERT INTO articles (name, reference) VALUES ('".clean($data[0])."','".clean($data[1])."')";
if (mysqli_query($conn, $sql)) { } else { echo "Error: " . $sql . "<br>" . mysqli_error($conn); }
$lines++;
}
}
header('Location: <?php echo $_SERVER["PHP_SELF"]; ?>?success=1&lines='.$lines.''); die;
}
if ( isset($_GET['success']) )
{
$success = $_GET['success'];
$lines = $_GET['lines'];
} else {
$success = 0;
}
mysqli_close($conn);
if ($success != 0 ) {
echo "<b>Your file has been imported.</b><br>";
echo "Found a total of ".$lines." records in this csv file.<br />";
}
?>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" enctype="multipart/form-data" name="form1" id="form1">
Choose your file: <br />
<input name="csv" type="file" id="csv" />
<input type="submit" name="Submit" value="Submit" />
</form>
问题出在你的clean
函数中,最后一行:
function clean($str, $default = '') {
if (!isset($str)) $str = $default;
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
由于您没有 mysql_*
连接,mysql_real_escape_string
将 return false
转换为空字符串。您应该为 mysqli 使用正确的函数,但准备好的语句会更好,这样您就不必清理任何东西。当然除了定期验证。
请注意,如果您选择使用转义函数,则需要根据需要将数据库连接作为参数发送给您的函数:
function clean($db, $str, $default = '') {
...
return mysqli_real_escape_string($db, $str);
}
现在运行良好...感谢您的帮助
<?php
ini_set('auto_detect_line_endings',TRUE);
function clean($link, $str, $default ='') {
if (!isset($str)) $str = $default;
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysqli_real_escape_string($link, $str);
}
$host_name = "somehost";
$database = "somedb";
$user_name = "someusername";
$password = "somepassword";
$connect = mysqli_connect($host_name, $user_name, $password, $database);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else {}
if (isset($_GET['success'])) {
$success = $_GET['success'];
$lines = $_GET['lines'];
} else {
$success = 0;
}
if ($success != 0 ) {
echo "<b>Your file has been imported.</b><br>";
echo "Found a total of ".$lines." records in this csv file.<br />";
}
$lines = $value1 = $value2 = $data = 0;
if ($_FILES["csv"]["size"] > 0) {
//get the csv file
$file = $_FILES["csv"]["tmp_name"];
$handle = fopen($file,"r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
//print_r($data).'<br />';
if ($data[0]) {
$sql = "INSERT INTO `articles` (`name`, `reference`) VALUES ('".clean($connect, $data[0])."','".clean($connect, $data[1])."')";
//echo $sql.'<br />';
if (mysqli_query($connect, $sql)) { } else { echo "Error: " . $sql . "<br>" . mysqli_error($connect); }
$lines++;
}
}
header('Location: index.php?success=1&lines='.$lines.'');die;
}
?>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" enctype="multipart/form-data" name="form1" id="form1">
Choose your file: <br />
<input name="csv" type="file" id="csv" />
<input type="submit" name="Submit" value="Submit" />
</form>
我的托管商刚刚贬低了 mysql 个连接以支持 mysqli
这个特定的脚本是一个示例,但在我尝试转换到 Mysqli 之前运行良好。不幸的是,我从另一份工作安排 php 开始编程已经 4 年了,并且已经有一段时间没有看过这个了。所以请温柔一点:-)
我可以连接到数据库,但输出如下所示: 错误:INSERT INTO articles (name, reference) VALUES ('','')
我一定是对 $data 变量做错了什么,但没有任何线索,因为它适用于以前的版本。
感谢任何帮助
<?php
ini_set('auto_detect_line_endings',TRUE);
function clean($str, $default = '') {
if (!isset($str)) $str = $default;
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
$host_name = "some host";
$database = "some database";
$user_name = "someusername";
$password = "some password";
$connect = mysqli_connect($host_name, $user_name, $password, $database);
if (mysqli_connect_errno()){ echo "Failed to connect to MySQL: " . mysqli_connect_error(); } else {echo 'connected to DB<br />';}
$lines = $value1 = $value2 = $data = 0;
if ($_FILES["csv"]["size"] > 0) {
//get the csv file
$file = $_FILES["csv"]["tmp_name"];
$handle = fopen($file,"r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if ($data[0]) {
$sql = "INSERT INTO articles (name, reference) VALUES ('".clean($data[0])."','".clean($data[1])."')";
if (mysqli_query($conn, $sql)) { } else { echo "Error: " . $sql . "<br>" . mysqli_error($conn); }
$lines++;
}
}
header('Location: <?php echo $_SERVER["PHP_SELF"]; ?>?success=1&lines='.$lines.''); die;
}
if ( isset($_GET['success']) )
{
$success = $_GET['success'];
$lines = $_GET['lines'];
} else {
$success = 0;
}
mysqli_close($conn);
if ($success != 0 ) {
echo "<b>Your file has been imported.</b><br>";
echo "Found a total of ".$lines." records in this csv file.<br />";
}
?>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" enctype="multipart/form-data" name="form1" id="form1">
Choose your file: <br />
<input name="csv" type="file" id="csv" />
<input type="submit" name="Submit" value="Submit" />
</form>
问题出在你的clean
函数中,最后一行:
function clean($str, $default = '') {
if (!isset($str)) $str = $default;
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
由于您没有 mysql_*
连接,mysql_real_escape_string
将 return false
转换为空字符串。您应该为 mysqli 使用正确的函数,但准备好的语句会更好,这样您就不必清理任何东西。当然除了定期验证。
请注意,如果您选择使用转义函数,则需要根据需要将数据库连接作为参数发送给您的函数:
function clean($db, $str, $default = '') {
...
return mysqli_real_escape_string($db, $str);
}
现在运行良好...感谢您的帮助
<?php
ini_set('auto_detect_line_endings',TRUE);
function clean($link, $str, $default ='') {
if (!isset($str)) $str = $default;
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysqli_real_escape_string($link, $str);
}
$host_name = "somehost";
$database = "somedb";
$user_name = "someusername";
$password = "somepassword";
$connect = mysqli_connect($host_name, $user_name, $password, $database);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else {}
if (isset($_GET['success'])) {
$success = $_GET['success'];
$lines = $_GET['lines'];
} else {
$success = 0;
}
if ($success != 0 ) {
echo "<b>Your file has been imported.</b><br>";
echo "Found a total of ".$lines." records in this csv file.<br />";
}
$lines = $value1 = $value2 = $data = 0;
if ($_FILES["csv"]["size"] > 0) {
//get the csv file
$file = $_FILES["csv"]["tmp_name"];
$handle = fopen($file,"r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
//print_r($data).'<br />';
if ($data[0]) {
$sql = "INSERT INTO `articles` (`name`, `reference`) VALUES ('".clean($connect, $data[0])."','".clean($connect, $data[1])."')";
//echo $sql.'<br />';
if (mysqli_query($connect, $sql)) { } else { echo "Error: " . $sql . "<br>" . mysqli_error($connect); }
$lines++;
}
}
header('Location: index.php?success=1&lines='.$lines.'');die;
}
?>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" enctype="multipart/form-data" name="form1" id="form1">
Choose your file: <br />
<input name="csv" type="file" id="csv" />
<input type="submit" name="Submit" value="Submit" />
</form>