mysqli_query() expects at least 2 parameters
时间: 2024-05-04 19:18:24 浏览: 101
PHP的mysqli_query参数MYSQLI_STORE_RESULT和MYSQLI_USE_RESULT的区别
5星 · 资源好评率100%
The mysqli_query() function in PHP is used to execute a query on a MySQL database. It requires at least two parameters to work: the first parameter is the database connection object, which is returned by the mysqli_connect() function, and the second parameter is the SQL query to be executed.
Here's an example of how to use mysqli_query() function with two parameters:
```
// Create database connection
$conn = mysqli_connect("localhost", "username", "password", "database_name");
// Check if connection is successful
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Execute SQL query
$sql = "SELECT * FROM users";
$result = mysqli_query($conn, $sql);
// Check if query execution is successful
if ($result) {
// Do something with the query result
} else {
echo "Error executing query: " . mysqli_error($conn);
}
// Close database connection
mysqli_close($conn);
```
In this example, we first create a database connection object using the mysqli_connect() function. We then use this connection object as the first parameter in the mysqli_query() function along with the SQL query as the second parameter. The function returns a result object, which we can use to check if the query execution was successful.
It's important to note that the mysqli_query() function can take additional optional parameters for prepared statements and other advanced features, but the minimum requirement is two parameters as described above.
阅读全文