[英]My SQL Select Query and IF statement will not return any results
I'm hoping someone could help me. 我希望有人可以帮助我。
I am completely new to PHP and I am finding that my SELECT
query and/or IF
statement are not retrieving any result from my database. 我对PHP完全陌生,发现我的
SELECT
查询和/或IF
语句未从数据库中检索任何结果。
The aim of this code is to retrieve a css skin from the database that can be applied to every page of my website. 这段代码的目的是从数据库中检索一个CSS皮肤,该皮肤可以应用于我网站的每个页面。
Here is the code: 这是代码:
$dbc - mysqli_connect($servername, $username, $password, $databasename) or die ('Error connecting to MYSQL server');
$skin = $_POST['skin'];
$query = "SELECT skin FROM skins WHERE id = '".$_SESSION['userid']."'";
$result = mysqli_query($dbc, $query) or die ('Error quering database');
if ($result) {
if ($skin=='blue') {
echo "<link rel='stylesheet' type='text/css' href='blue.css' />";
}
else if ($skin == "pink1") {
echo "<link rel='stylesheet' type='text/css' href='pink1.css' />";
}
else {
echo 'You do not have a skin preference';
}
}
When the code runs, it comes back echoing You do not have a skin preference
. 代码运行时,它回显,回显
You do not have a skin preference
。
My thinking is that it has not connected to the database/table correctly; 我的想法是它没有正确连接到数据库/表; so, it is not retrieving any result.
因此,它不会检索任何结果。
I know for a fact that it should pick up $skin=='blue'
from the database. 我知道一个事实,那就是它应该从数据库中提取
$skin=='blue'
。
Any help or suggestion would be much appreciated. 任何帮助或建议将不胜感激。
Thanks. 谢谢。
mysqli_query()
will only return false in case of an error, which would then trigger your or die()
clause. mysqli_query()
仅在发生错误的情况下返回false,这将触发您的or die()
子句。
A query which returns no data is NOT an error. 不返回任何数据的查询不是错误。 It's still a valid result set, which just happens to be empty.
它仍然是一个有效的结果集,恰好是空的。 You need to check
mysqli_num_rows()
to see if 0 or non-zero rows were returned instead. 您需要检查
mysqli_num_rows()
以查看是否返回了0或非零行。
eg. 例如。
if (mysqli_num_rows($result) == 0) {
... there is no preference set
} else {
... there's at least one skin preference
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.