[英]What's wrong with this simple php code querying a database and displaying results?
This is simply additional functionality for an assignment where I have created a water level monitor device. 这只是我创建了水位监控器设备的任务的附加功能。 All I want the web page to do is display a couple of SQL results. 我要网页执行的所有操作只是显示几个SQL结果。 However, none of the PHP in the page seems to run at all. 但是,页面中的PHP似乎都没有运行。 I have tested the SQL statements and they work fine so it is definitely my PHP. 我已经测试了SQL语句,它们工作正常,因此绝对是我的PHP。 Any help would be appreciated, cheers! 任何帮助将不胜感激,加油!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='cs' lang='cs'>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv='Content-Language' content='cs' />
<link rel="stylesheet" href="styl.css" type="text/css" />
<title>Flood Detector</title>
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
$con=mysqli_connect("");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
</head>
<body>
<div id="header">
<h2><a href="#" title="home">Flood Detector</a></h2>
<ul id="menu-top">
<li><a href="index.html">Home</a></li>
<li><a href="topten.html">Records</a></li>
</ul>
</div>
<div id="contain">
<div id="left">
<h1>Welcome</h1>
<p> Your water levels are currently <?php
$sql = "SELECT BODY FROM sms ORDER BY ID DESC LIMIT 1";
$result = mysqli_query($con, $sql)
or die(mysqli_error($con));
while($row = mysqli_fetch_array($result)){
print_r($row);
?></p>
<br>
<br>
<p> Your water has reached dangerous levels <?php
if ($dangerresult = mysqli_query($con, "SELECT * FROM sms WHERE BODY = ' warning'")) {
$count = mysqli_num_rows($dangerresult);
echo $count;
}
?>
times
</p>
</div>
<div class="cleaner"></div>
</div>
</body>
</html>
Firstly, you need to pass DB connection to your queries. 首先,您需要将数据库连接传递给查询。
You also have a space in ' warning'
so that may cause problems; 您还会在' warning'
留一个空格,以免引起问题; remove it 'warning'
if you're not getting results with my answer/code below. 如果您无法通过下面的答案/代码获得结果, 请将其 'warning'
删除 。
$sql = "SELECT BODY FROM sms ORDER BY ID DESC LIMIT 1";
$result = mysqli_query($con,$sql);
echo $result;
Sidenote: I don't know why you're doing echo $result;
旁注:我不知道您为什么要执行echo $result;
so you may need to elaborate on that. 因此您可能需要详细说明。
Or try something like: 或者尝试类似的方法:
while($row = mysqli_fetch_array($result))
{
echo "Your water levels are currently " . $row['BODY'];
echo "<br>";
}
and $mysqli_query
that is a function not a variable. 和$mysqli_query
是不是变量的函数。
In regards to mysqli_num_rows()
, try using it this way, since you want to check if num_rows()
does return a value as per the word you're looking for, being "warning". 关于mysqli_num_rows()
,请尝试以这种方式使用它,因为您要检查num_rows()
是否确实根据您要查找的单词返回一个值,即“警告”。
$dangerresult = mysqli_query($con, "SELECT * FROM sms WHERE BODY = ' warning'");
$count = mysqli_num_rows($dangerresult);
if($count > 0){
echo $count;
}
else{
echo "Empty.";
}
Edit: 编辑:
Or try: 或尝试:
$dangerresult = mysqli_query($con, "SELECT * FROM sms WHERE BODY = 'warning'");
$count = mysqli_num_rows($dangerresult);
if($count > 0){
echo $count;
}
else{
echo "Empty.";
}
while($row = mysqli_fetch_array($dangerresult))
{
echo "Your water levels are currently " . $row['BODY'];
echo "<br>";
}
or die(mysqli_error($con))
to all the mysqli_query()
and error reporting. 还要向所有mysqli_query()
添加or die(mysqli_error($con))
mysqli_query()
和错误报告。 Plus, in regards to SQL injection, use mysqli
with prepared statements , or PDO with prepared statements , they're much safer . 另外,对于SQL注入, 将mysqli
与预备语句一起使用 ,或者将PDO与预备语句一起使用 ,则它们要安全得多 。
You did not use mysqli_select_db and mysqli_fetch_array function to fetch data. 您没有使用mysqli_select_db和mysqli_fetch_array函数来获取数据。 Please try this : 请尝试这个:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='cs' lang='cs'>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv='Content-Language' content='cs' />
<link rel="stylesheet" href="styl.css" type="text/css" />
<title>Flood Detector</title>
<?php
$con=mysqli_connect("*****************","*********","********","***********");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
?>
</head>
<body>
<div id="header">
<h2><a href="#" title="home">Flood Detector</a></h2>
<ul id="menu-top">
<li><a href="index.html">Home</a></li>
<li><a href="topten.html">Records</a></li>
</ul>
</div>
<div id="contain">
<div id="left">
<h1>Welcome</h1>
<p> Your water levels are currently <?php
$sql = "SELECT BODY FROM sms ORDER BY ID DESC LIMIT 1";
$result = mysqli_query($sql);
//echo $result;
while($row = mysqli_fetch_array($result)){
print_r($row);
}
?></p>
<br>
<br>
<p> Your water has reached dangerous levels <?php
if ($dangerresult = mysqli_query("SELECT * FROM sms WHERE BODY = ' warning'")) {
$count = mysqli_num_rows($dangerresult);
echo $count;
}
?>
times
</p>
</div>
<div class="cleaner"></div>
</div>
</body>
</html>
There's many problems with that code. 该代码有很多问题。
First, you need to pass your $con
arround, the mysqli_query
function needs to know what connection you're referring to, check the manual page for mysqli_query() . 首先,您需要传递$con
arround, mysqli_query
函数需要知道您所指的连接,请查看mysqli_query()的手册页 。
Then there's also the line if ($dangerresult = $mysqli_query(...
, where you're referring to mysqli_query
as a variable. 然后还有if ($dangerresult = $mysqli_query(...
,在这里您将mysqli_query
称为变量。
Also, 也,
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
Is lacking a closing bracket, so the rest of the script is only executed in case of error (and it's a syntax error too). 缺少右括号,因此脚本的其余部分仅在出现错误的情况下才执行(这也是语法错误)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.