【问题标题】:PHP mysqli_fetch_array() errorPHP mysqli_fetch_array() 错误
【发布时间】: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。不要手动转义和注入字符串。

标签: php html mysql mysqli


【解决方案1】:

您尚未引用您的 $email 值。 real_escape 函数为您执行此操作。

WHERE checkin.user = '$email'");
                     ^------^---- you still need these

如果您费心对查询进行任何类型的错误处理,您就会被告知:

$result = mysqli_query($mysqli, $query) or die(mysqli_error());
                                       ^^^^^^^^^^^^^^^^^^^^^^^

永远不要假设查询总是会成功。即使您的 SQL 在语法上是完美的,也有无数其他原因导致它失败。始终假设失败,检查失败,并将成功视为惊喜。

这就像“我不需要系安全带。我不会开车撞到树上,所以我会没事的”。在你的尸体被从醉酒司机的汽车引擎盖上刮下后,我相信这会让你的亲人感到欣慰。

【讨论】:

  • “在你的尸体被从醉酒司机的汽车引擎盖上刮下后,我相信这对你的亲戚来说会是一个安慰。” - 清点周围的尸体这里是真实的;不是虚拟的。
  • 切换到占位符值会使引用变得无关紧要。
【解决方案2】:

试试这个:

$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'") or die(mysqli_error());

这是必须的

 while($row = mysqli_fetch_array($result))

【讨论】:

    猜你喜欢
    • 2011-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多