PHP mysqli_fetch_array()错误

时间:2014-08-18 20:20:48

标签: php html mysql mysqli

我尝试在我的一个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>

2 个答案:

答案 0 :(得分:1)

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

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

如果您不愿意对查询进行任何类型的错误处理,那么您已被告知此事:

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

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

它就像&#34;我不需要系安全带。我不会开车进入树林,所以我会很好。#34;。在你的尸体从醉酒司机的汽车的引擎盖上刮掉之后,我确信这对你的亲属来说是一个令人安慰的想法。

答案 1 :(得分:0)

试试这个:

$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))