如果有数据,第一部分效果很好。当没有$ res时,else部分不回显。
IO尝试过使用这个但是! $ row停止并且不会回显内部的任何内容。所以我把它改写成下面的PHP脚本,这更直接。我现在担心sql注入因为我正在清理id以及验证用户实际登录到系统。
$stmt = $conn->prepare('SELECT * FROM table WHERE ID=?');
$stmt->bindParam(1, $_GET['id'], PDO::PARAM_INT);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if( ! $row)
{
die('nothing found');
}else {
enter code here
}
Thjs是我的整个php页面。
$ db = new PDO(连接字符串。)
<?php
error_reporting(0);
include_once("php_includes/db_conx.php");
if($_GET['id'] != ''){
$id = preg_replace('#[^0-9]#', '', $_GET['id']);
$sql="SELECT nextdue, alert, completed, page_id, page_type FROM medical where id=$id limit 1";
$alertdiv = "";
if ($res = $db->query($sql)) {
$alertdiv = "";
foreach( $db->query($sql) as $data ) {
//if alert is set to Y create div to show the type and stuff.
$timestamp = $data[0];
if ($data[1]=='y'){
if($timestamp > date("Y-m-d")) {
$alertdiv .= "This alert is in the Future.<br>";
}
if($timestamp < date("Y-m-d")) {
$alertdiv .= "This alert is pastdue.<br>";
}
if($timestamp == date("Y-m-d")) {
$alertdiv .= "This alert is due Today.<br>";
}
$alertdiv .= "<table><tr><td width='50'>Method</td><td>Description</td><td>Destination</td><td>Completed</td><td>Date Due</td><td>Action</td></tr><br>";
$completed = $data[2];
$page_id = $data[3];
$page_type = $data[4];
//check completed
//get page details
switch ($page_type) {
case "d":
$alertdiv .= "<tr id='selectedmethodtr' value='d0'>";
$alertdiv .= "<td width='10'>Default</td><td>Dashboard</td><td>Dashboard</td>";
break;
case "e":
$alertdiv .= "<tr id='selectedmethodtr' value='e$data[3]'>";
$sql1 = "SELECT description, email from page_email where page_email_id=$page_id";
foreach( $db->query($sql1) as $data1 ) {
$alertdiv .= "<td width='50'>E-Mail</td><td>$data1[0]</td><td>$data1[1]</td>";
}
break;
case "p":
$alertdiv .= "<tr id='selectedmethodtr' value='p$data[3]'>";
$sql1 = "SELECT description, phone, carriervalue from page_phone where page_phone_id=$page_id";
foreach( $db->query($sql1) as $data1 ) {
$alertdiv .= "<td width='50' >E-Mail</td><td>$data1[0]</td><td>$data1[1]</td>";
}
break;
}
switch ($completed) {
case "0":
$alertdiv .= "<td>No</td>";
break;
case "1":
$alertdiv .= "<td>Yes</td>";
break;
}
$alertdiv .= "<td>$timestamp</td>";
$alertdiv .= "<td><a onClick=deleteAlert($id) id='deleteAlert'><i class='fa fa-trash-o fa-lg'></i></a></td></tr>";
}
}//end if data[1]
$alertdiv .= "</table>";
echo $alertdiv;
}else {
$alertdiv .= "Alert Type is set Default Dashboard! <br>";
$alertdiv .= "<table id='selectedmethodtable'>";
$alertdiv .= "<tr>";
$alertdiv .= "<td>Description</td>";
$alertdiv .= "<td >Method</td><option id='selectedmethodtr' value='d0' ></option>";
$alertdiv .= "</tr>";
$alertdiv .= "<tr ><td>DashBoard</td><td>Default</td></tr>";
$alertdiv .= "</table>";
echo $alertdiv;
}
}
?>
答案 0 :(得分:1)
您的评论(“找到的结果”)是错误的。
我假设$db->query
是mysqli's query method。
失败时返回FALSE。对于成功的SELECT,SHOW,DESCRIBE或EXPLAIN查询,mysqli_query()将返回一个mysqli_result对象。对于其他成功的查询,mysqli_query()将返回TRUE。
如果查询成功运行,则该值为true,即使该查询未找到匹配的行。
如果运行查询的错误,您将收到错误的结果。
要测试是否没有结果,您应该使用num_rows
。
if ($res = $db->query($sql)) { // Query ran without errors
if ($res->num_rows == 0) {
// There were no results found
} else {
while($row = $res->fetch_assoc($result)) {
// Do stuff with the data in the `$row`
}
}
} else {
// There was an error running the query
}