请检查此代码,如果有任何错误,请告诉我。
// Create connection
$conn = mysqli_connect($servername, $username, $password, $shop_item);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$item_sql = "SELECT * FROM shop-items";
mysqli_query($conn, 'SET CHARACTER SET utf8;');
$result_item = mysqli_query($conn, $item_sql);
echo var_dump($result_item); //returns: bool(false)
if (mysqli_num_rows($result_item) > 0) { // doesn't execute. the error is "mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean";
答案 0 :(得分:4)
正如我所提到的,shop-items
被mysql视为shop minus items
。
如果你的表名真的是shop-items
- 你应该使用反引号来逃避它:
$item_sql = "SELECT * FROM `shop-items`";
为了检查错误,您可以使用mysqli_error()
功能:
$err = mysqli_error($conn);
echo $err;
答案 1 :(得分:1)
使用类似于此的结构可能更好:
$conn = new mysqli('localhost', 'root', 'password', 'your_database');
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql="SELECT * FROM shop-items";
if ($result = $conn->query($sql)) {
// actions in case of success
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
答案 2 :(得分:1)
这可能是破折号的问题。你试过吗
SELECT * FROM `shop-items`
答案 3 :(得分:1)
我会尝试使用以下代码来处理从PHP到您的数据库的所有查询,因为正如之前提到过的,您使用的错误可能来自非语法:
$mysqli = new mysqli("localhost", "user", "password", "DB");
/* Check conection */
if (mysqli_connect_errno()) {
printf("Conection error: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT * FROM shop-items";
if ($stmt= $mysqli->prepare($query)) {
/* execute the query */
$stmt->execute();
/* Bind the results to variables */
$stmt->bind_result($col1, $col2); //as many variables as columns the query will return
/* obtener los valores */
while ($stmt->fetch()) {
//do something with the results
}
/* close the query */
$stmt->close();
}
/* close the DB connection */
$mysqli->close();
?>
希望这会有所帮助!!