我希望您能提供帮助,我正在为公司创建网址缩短工具,但我遇到了错误:Call to a member function bind_param() on a non-object
,非常感谢您的回复。< / p>
<?php
$data_base = new mysqli ("localhost","exab_linus","HZYhVCz8bxm7NmU9","exab_lmg");
function generateRandomString($length = 4) {
$key = 'abcdefghijklmnopqrstuvwxyz1234567890';
$keyLength = strlen($key);
$string = '';
for ($i = 0; $i < $length; $i++) {
$string .= $key[rand(0, $keyLength - 1)];
}
return $string;
}
if (isset($_GET['title'])) {
$resolve = $data_base->prepare("SELECT * FROM links WHERE title=?");
$resolve->bind_param("s", $_GET['title']);
$resolve->execute();
$goto = $resolve->get_result()->fetch_array();
$goto1 = $goto[1];
header("Location: $goto1");
}
if (isset($_POST['submit'])) {
$short_url = generateRandomString();
if (!preg_match("/^(http|https):/", $_POST['long_url'])) {
$_POST['long_url'] = 'http://'.$_POST['long_url'];
}
$link = $_POST['long_url'];
$resolve = $data_base->prepare("INSERT INTO links (long_url, title) VALUES (?, ?);");
$resolve->bind_param("ss",$link, $short);
$resolve->execute();
?>
提前致谢, 扎克
答案 0 :(得分:0)
您不能在未经检查的情况下使用prepare
结果
如果发生任何错误,它可以返回false
。
if ($resolve = $data_base->prepare("INSERT INTO links VALUES('',?,?)")) {
$resolve->bind_param("ss",$link, $short); // Problematic frame
$resolve->execute();
} else {
// You can get error message here
printf("Error: %s.<br/>\n", $resolve->error); // assumed this is mysqli
$resolve->close();
}
可能是你得到sql语法错误,关于你的插入查询应该是这样的:
INSERT INTO links (long_url, title) VALUES (?, ?);