Fatal error: Call to a member function bind_param(

2019-08-06 15:32发布

问题:

I am trying to make a url shortening website. However, I am running into this error when I try clicking the submit input: Fatal error: Call to a member function bind_param() on boolean in C:\xampp\htdocs\index.php. I've seen others having this issue but never managed to fix it. Thanks in advance!

<?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>

回答1:

As I stated in comments, you didn't choose a database.

As requested:

This line:

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

Should have read as:

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

and replacing "database" with the name of your database.

The documentation can be found at the following address on PHP.net:

  • http://php.net/manual/en/function.mysqli-connect.php

Example pulled from the manual along with error checking:

<?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);

Note: Alhough you can still theoretically connect, you just wouldn't be able to query anything though. That's where error checking is important during development testing.



标签: php bindparam