【问题标题】:SELECT function to array creating copy of dataSELECT函数到数组创建数据副本
【发布时间】:2014-03-18 13:33:24
【问题描述】:

我从数据库中选择两条数据并将该数据放入一个数组中。我的代码是;

<?php
session_start();
include 'connection.php';
$inspectit = array();
$update1 = $_POST['inspect'];
$query1 = "SELECT title, reason FROM daysoff WHERE DATE(start) = '$update1' AND gighold = 1";
$day = mysql_query($query1); 
while($requesting = mysql_fetch_array($day)) {                           
$inspectit = $requesting;
}
$_SESSION['inspect'] = $inspectit; 
 var_dump($inspectit);
 //header('Location: '. $_SERVER['HTTP_REFERER']) ;
?>

当这个查询返回并转储变量时,它会像这样显示每条数据两次;

array(4) { [0]=> string(5) "Brady" ["title"]=> string(5) "Brady" [1]=> string(7) "hfhjgfj" ["reason"]=> string(7) "hfhjgfj" }

为什么会这样?

【问题讨论】:

  • 注意每个重复值的键。一个是数字,另一个是列名。

标签: php sql arrays select


【解决方案1】:

使用mysql_fetch_assoc 而不是mysql_fetch_array

因为 mysql_fetch_array 将返回 id 和关联的标题与 id。

【讨论】:

    【解决方案2】:

    The manual 很清楚mysql_fetch_array

    Fetch a result row as an associative array, a numeric array, or both
    

    双打存在,因此您可以通过名称或索引来引用列。

    【讨论】:

      猜你喜欢
      • 2011-01-27
      • 1970-01-01
      • 2013-12-06
      • 1970-01-01
      • 2012-05-16
      • 1970-01-01
      • 2018-11-30
      • 1970-01-01
      相关资源
      最近更新 更多