[英]I am trying to run my website on an online host but i keep getting an error
Every time i am trying to run the following PHP code on 000Webhost, i keep getting this error -- mysqli_num_rows() expects parameter 1 to be mysqli_result. 每当我尝试在000Webhost上运行以下PHP代码时,我都会不断收到此错误-mysqli_num_rows()期望参数1为mysqli_result。
The same code had been run successfully without errors on my localhost, XAMPP, i have looked through many examples and only found out that this error is caused by an error in the query, but as mentioned, the query works perfectly on my localhost. 相同的代码已在我的本地主机XAMPP上成功运行且没有错误,我浏览了许多示例,才发现此错误是由查询中的错误引起的,但是如上所述,查询在我的本地主机上运行良好。
The error is indicated in the code. 错误在代码中指示。
Any help would be appreciated. 任何帮助,将不胜感激。
<?php
session_start();
//decalre variables
$DeviceID ="";
$productID ="";
//connect to database
$db = mysqli_connect('localhost','id5655845_grocerywatch1234','123456','id5655845_grocerywatch1234');
//validate product id and device id are avaliable
if(isset($_POST['validate_user'])){
$DeviceID = mysqli_real_escape_string($db,$_POST['DeviceID']);
$productID = mysqli_real_escape_string($db,$_POST['productID']);
$query = "SELECT * FROM configuration WHERE DeviceID='$DeviceID' AND productID='$productID'";
$result1 = mysqli_query($db,$query);
echo $query;
//error indicated on the following line.
if(mysqli_num_rows($result1) == 1){
$_SESSION['DeviceID'] = $DeviceID;
$_SESSION['success'] = "You are now logged in";
header('location: register.php');
}
else{
echo "Device Not registered";
echo "Product Doesnt Exist";
}
}
I think your query is likely failing. 我认为您的查询可能失败。 The return value for
mysqli_query
is False
on failure, otherwise it is mysqli_result
. 失败时
mysqli_query
的返回值为False
,否则为mysqli_result
。 See docs here 在这里查看文档
Fix by properly formatting string: 通过正确格式化字符串来解决:
...
$query = "SELECT * FROM configuration WHERE DeviceID='".$DeviceID."' AND productID='".$productID."'";
$result1 = mysqli_query($db,$query);
echo $query;
if ($result1 == false){
echo "Error has occurred!";
}
elseif (mysqli_num_rows($result1) == 1){
$_SESSION['DeviceID'] = $DeviceID;
$_SESSION['success'] = "You are now logged in";
header('Location: register.php');
}
else{
echo "Device Not registered";
echo "Product Doesnt Exist";
}
The query either returned no rows or is erroneus, thus FALSE is returned. 该查询没有返回任何行,或者是错误的,因此返回FALSE。 Change it to
更改为
if (!$dbc || mysqli_num_rows($dbc) == 0)
Return Values
返回值
Returns TRUE on success or FALSE on failure.
成功返回TRUE,失败返回FALSE。 For SELECT, SHOW, DESCRIBE or EXPLAIN mysqli_query() will return a result object.
对于SELECT,SHOW,DESCRIBE或EXPLAIN,mysqli_query()将返回结果对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.