我在PHP中遇到一个警告错误,我正在为数据库创建一个edit.php脚本。当我点击并转到编辑屏幕时,它向我发出错误:警告:mysqli_num_rows()期望参数1为mysqli_result,第34行的/../edit.php中给出布尔值。第34行是代码读if (mysqli_num_rows($sql) > 0)
如果有人能给我一个解决这个错误的方法,我会很感激。
代码如下:
<?php
require_once("includes/connection.inc.php");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Bootstrap -->
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<h1>Edit Record</h1>
<p>Edit the tree!</p>
<?php $id = $_GET["id"]; ?>
<form method="POST" role="form" action="leaferie.php?flag=1&id=<?php echo $id; ?>">
<?php $dbc = dbConnect('read'); ?>
<?php
$sql = mysqli_query($dbc, "SELECT * FROM trees WHERE id = $id");
if (mysqli_num_rows($sql) > 0)
{
$row = mysqli_fetch_array($sql) ?>
<div class="form-group">
<label for="treeName">Tree Name</label>
<input name="tree_name" value="<?php echo $row["tree_name"] ?>" type="text" class="form-control" id="treeName">
</div>
<div class="form-group">
<label for="scientificName">Scientific Name</label>
<input name="scientific_name" value="<?php echo $row["scientific_name"] ?>" type="text" class="form-control" id="scientificName">
</div>
<div class="form-group">
<label for="plantType">Plant Type</label>
<input name="plant_type" value="<?php echo $row["plant_type"] ?>" type="text" class="form-control" id="plantType">
</div>
<div class="form-group">
<label for="leaf">Leaf</label>
<input name="leaf" value="<?php echo $row["leaf"] ?>" type="text" class="form-control" id="leaf">
</div>
<div class="form-group">
<label for="flower">Flower</label>
<input name="flower" value="<?php echo $row["flower"] ?>" type="text" class="form-control" id="flower">
</div>
<div class="form-group">
<label for="fruit">Fruit</label>
<input name="fruit" value="<?php echo $row["fruit"] ?>" type="text" class="form-control" id="fruit">
</div>
<div class="form-group">
<label for="hardiness">Hardiness</label>
<input name="hardiness" value="<?php echo $row["hardiness"] ?>" type="text" class="form-control" id="hardiness">
</div>
<div class="form-group">
<label for="culture">Culture</label>
<input name="culture" value="<?php echo $row["culture"] ?>" type="text" class="form-control" id="culture">
</div>
<div class="form-group">
<label for="size">Size</label>
<input name="size" value="<?php echo $row["size"] ?>" type="text" class="form-control" id="size">
</div>
<div class="form-group">
<label for="uses">Uses</label>
<input name="uses" value="<?php echo $row["uses"] ?>" type="text" class="form-control" id="uses">
</div>
<div class="form-group">
<label for="notes">Notes</label>
<input name="notes" value="<?php echo $row["notes"] ?>" type="text" class="form-control" id="notes">
</div>
<?php
}
?>
<a href="leaferie.php?flag=2">View Records</a>
<button class="btn btn-primary" name="submit" type="submit">Submit</button>
</form>
</body>
答案 0 :(得分:2)
if ($sql == FALSE) {echo mysqli_error(); exit;}
if (mysqli_num_rows($sql) > 0) {//do your thing
}
显然,mysqli没有返回任何有效的问题......所以检查错误,一切都应该清楚。