【发布时间】:2014-08-18 20:20:47
【问题描述】:
尝试在我的一种 php 表单中执行 sql 语句时出现 mysqli_fetch_array() 错误。 它正在做的是在数据库中搜索用户电子邮件并返回结果。我可以通过 dbforge 执行 SQL 语句,它可以工作,但是当它通过 Web 应用程序启动时不会运行..
代码:
<?php
// Start session
session_start();
// Include required functions file
require_once('includes/functions.inc.php');
require_once('includes/config.inc.php');
if (isset($_POST['email'])){
$email = $_POST['email'];
} else $email ="";
var_dump($email);
// Connect to database
$mysqli = @new mysqli(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($mysqli, "SELECT
checkin.user,
SUM(checkin.points) AS point_total,
satellite_members.f_name,
satellite_members.l_name,
satellite_members.email
FROM checkin
INNER JOIN satellite_members
ON satellite_members.email = checkin.user
WHERE checkin.user = $email");
?>
此处的 HTML 元素:
<form action="" method="post">
<div class="form-group">
<label for="Email Address">Email Address</label>
<input type="email" class="form-control" id="email" name="email" style="width:60%; display:inline;" required>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<div class="table-responsive" style="width:100%; ">
<table class="table table-condensed table-bordered" >
<tr class="bg-cotu">
<th style="width:45%;" class="text-center">Member name</th>
<th style="width:20%;" class="text-center">Point Total</th>
</tr>
<?php
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['f_name']." ".$row['l_name'] . "</td>";
echo "<td>" . $row['point_total'] . "</td>";
echo "</tr>";
}
?>
</table>
</div>
【问题讨论】:
-
你混合了 mysql_* 和 mysqli_* API。只使用 mysqli。
-
^--gin & tonic,虽然有些人喜欢混合这些 ;)
-
没有区别。拿出来也没办法解决
-
拿出来然后呢?这个
mysql_real_escape_string($_POST['email'])应该是mysqli_real_escape_string($mysqli,$_POST['email'])并引用$email- 使用error reporting 会发出这些错误的信号。 -
当使用
mysqli时,您应该使用参数化查询和bind_param将用户数据添加到您的查询中。 请勿使用字符串插值来完成此操作,因为您将创建严重的SQL injection bugs。不要手动转义和注入字符串。