Fatal error: Call to a member function bind_param() on boolean in C:\xampp\htdocs\index.php

Fatal error: Call to a member function bind_param() on boolean in C:\xampp\htdocs\index.php

我正在尝试制作一个 url 缩短网站。但是,当我尝试单击提交输入时,我 运行 遇到了这个错误: 致命错误:在 C:\xampp\htdocs\index.php 中调用布尔值的成员函数 bind_param()。 我看到其他人遇到了这个问题,但从未设法解决它。 提前致谢!

<?php

$db = new mysqli("localhost","root","");

if (isset($_POST['shorten'])) {
//echo "Clicked";
$result = $db->prepare("INSERT INTO links VALUES('',?,'test')");
$result->bind_param('s', $_POST['url_to_shorten']);
$result->execute();
echo "Done.";
}

?>
<!doctype html>
<html>
<head>
<title>Orix.ml - free link shortener</title>
</head>
<body>
<center>
<h1>ORIX.ML</h1>
<h2>FREE USB SHORTENING SERVICE</h2>
<form action="/" method="POST">
<input type="text" name="url_to_shorten" value="" placeholder="PASTE YOUR LINK HERE">
<input type="submit" name="shorten" value="GO">
</form>
</center>
</body>
</html>

正如我在评论中所述,您没有选择数据库。

根据要求:

这一行:

$db = new mysqli("localhost","root","");

应该读作:

$db = new mysqli("localhost","root","", "database");

并将 "database" 替换为您的数据库名称。

文档可以在 PHP.net 上的以下地址找到:

从手册中提取的示例以及错误检查:

<?php
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");

if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;

mysqli_close($link);

注意:虽然理论上您仍然可以连接,但您将无法查询任何内容。这就是在开发测试期间错误检查很重要的地方。