问题
I want to insert some data from a parsed JSON to a table, but when I do it, it doesn't work, it returns 0 rows, what am I missing? I'm new yet with this "mysqli". I have more than 25000 rows to insert to the table.
$mysqli = mysqli_connect('localhost', 'root', '', '');
$allData = $dataSource->getAllData();
foreach ($allData as $key => $value) {
$query = 'INSERT INTO `table`(`data_id`, `name`) VALUES (' . $value['data_id'] . ', ' . $value['name'] . ')';
$result = mysqli_query($mysqli, $query);
}
回答1:
Seems like you should set single quotes around your data values. Also adding a mysqli_error check for your mysqli_query line so you can actually see what is happening:
$allData = $dataSource->getAllData();
foreach ($allData as $key => $value) {
$query = "INSERT INTO `table`(`data_id`, `name`) VALUES ('" . $value['data_id'] . "', '" . $value['name'] . "')";
$result = mysqli_query($mysqli, $query) or die(mysqli_error($mysqli));
}
Or better yet, use mysqli_stmt_bind_param like this. Allow MySQLi to deal with the whole query data structuring instead of having to worry about single quote placement. Also, added a check for mysqli_connect_error on your mysqli_connect line:
// Connecting, selecting database
$mysqli = mysqli_connect('localhost', 'root', '', '') or die(mysqli_connect_error());
$allData = $dataSource->getAllData();
foreach ($allData as $key => $value) {
// Set the query.
$query = "INSERT INTO `table`(`data_id`, `name`) VALUES (?, ?)";
// Bind the params.
// mysqli_stmt_bind_param($query, 'ss', $value['data_id'], $value['name']);
mysqli_stmt_bind_param($query, 'is', $value['data_id'], $value['name']);
// Run the query.
$result = mysqli_query($mysqli, $query) or die(mysqli_error($mysqli));
}
Note that I have a commented line for the mysqli_stmt_bind_param since it’s not clear to me if your $value['data_id']
is a number or a string. The mysqli_stmt_bind_param($query, 'is',…
means the first value is an integer (i
) and the next value is a string (s
). Feel free to adjust to best fit your actual data types.
回答2:
You are most likely encountering this problem, because of missing quotes in SQL. All literal strings must be quoted when part of SQL statement. PHP would happily let you know about SQL syntax errors if you would only have enabled them. See mysqli_fetch_assoc() expects parameter / Call to a member function bind_param() errors. How to get the actual mysql error and fix it?. To summarize just add this line before mysqli_connect()
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
However, there is much better way to insert the data using mysqli. You should use prepared statements. Using parameter binding is not only safer (no SQL injections), but it also a little bit faster when you add 25000 rows.
Your fixed code with error reporting and prepared statements would look like this:
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'root', '', 'dbname');
$mysqli->set_charset('utf8mb4');
$allData = $dataSource->getAllData();
$stmt = $mysqli->prepare('INSERT INTO `table`(`data_id`, `name`) VALUES (?,?)');
foreach ($allData as $key => $value) {
$stmt->bind_param('ss', $value['data_id'], $value['name']);
$stmt->execute();
}
回答3:
An even more automatic solution would be:
$link = mysqli_connect('localhost', 'root', 'password', 'mydatabase') or die(mysqli_connect_error());
foreach ($groups as $key => $values) {
$valuesArray = array();
// figure out if we have to encapsulate in ''
foreach($values as $key => $value){
switch(gettype($value)){
case 'boolean':
case 'integer':
case 'double': // includes float
$valuesArray[] = $value;
break;
case 'string':
$valuesArray[] = '\''.$value.'\'';
break;
default:
die('not possible');
break;
}
}
$query = 'INSERT INTO mydatabase.mytable(' . implode(',',array_keys($values)) . ') VALUES (' . implode(',',$valuesArray) . ')';
// echo $query; // debug
$result = mysqli_query($link, $query) or die(mysqli_error());
}
This solution is plain mysqli again, for external input use prepared statements or pdo or the like!
来源:https://stackoverflow.com/questions/24333301/insert-into-mysql-from-array-with-mysqli