我正在尝试使用从给定单词创建字谜的函数结果填充MySQL表。根本没有结果。我什至没有收到错误消息。
<?php
//connect to your database
$conn = mysqli_connect("localhost","dbuser","3423423sfdfsdf","mydb");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$input = $trimmed;
function string_getpermutations($prefix, $characters, &$permutations)
{
if (count($characters) == 1)
$permutations[] = $prefix . array_pop($characters);
else
{
for ($i = 0; $i < count($characters); $i++)
{
$tmp = $characters;
unset($tmp[$i]);
string_getpermutations($prefix . $characters[$i], array_values($tmp), $permutations);
}
}
}
$characters = array();
for ($i = 0; $i < strlen($input); $i++)
$characters[] = $input[$i];
$permutations = array();
string_getpermutations("", $characters, $permutations);
foreach($permutations as $result) {echo $result,'<br>';}
foreach($permutations as $result) {mysqli_query($conn,"INSERT INTO tempanagram (anagram) VALUES ('$result')");}
?>
答案 0 :(得分:1)
您也可以使用mysqli
,但是最好的解决方案是使用一些查询生成器。
添加到PHP和MYSQL的代码错误报告中
<?php
error_reporting(E_ALL);
ini_set('display_errors', true)
mysql_query($sql, $conn) or die('ERROR: '.mysqli_error($conn));
http://php.net/manual/en/mysqli.error.php中的更多信息
您可以使用批量插入来优化代码以进行1次插入查询,如下所示:
<?php
$sql = 'INSERT INTO tempanagram (anagram) VALUES ("'.join('"),("', $permutations).'")';
答案 1 :(得分:0)
我找到了解决方法:
代替foreach($permutations as $result) {mysqli_query("INSERT INTO tempanagram (anagram) VALUES ('$result')");}
我必须写:
foreach($permutations as $result) {mysqli_query($conn,"INSERT INTO tempanagram (anagram) VALUES ('$result')");}
否则,显然不清楚我是指该连接。
所有其他提示都很有价值。非常感谢大家。