如何在数组中获取数据库结果?

时间:2011-05-22 11:40:31

标签: php arrays oop

我想用我数据库中的数据创建这个数组..

| Day | Comment | OtherComment |
|-----|---------|--------------|
|  1  |  hallo  | hallohallo   |
|-----|---------|--------------|
|  2  |  hey    | heyhey       |
|-----|---------|--------------|
|  3  |  hello  | hellohello   |
|_____|_________|______________|

我尝试了很多东西,但可以得到我想要的结果,这是我的最新代码:

$sql = "select DiaryOpmerkingen, DiaryDoctorcomment from tblDiary 
                WHERE fk_UserId = ".$p_iUserid."
                AND DiaryDay = '".$this->Day."';";
        $rResult = mysqli_query($link, $sql);
        return $rResult;

        $dim = array();

        while ( $row = mysql_fetch_assoc($result) )
        $dim[$row['DiaryOpmerkingen']][$row['DiaryDoctorcomment']] = $row;

4 个答案:

答案 0 :(得分:2)

使用MySQLi

好的,因为我们计划使用MySQLi来做这件事,我想向您介绍使用mysqli的面向对象方式,因此您不必在调用中继续传递数据库链接。我们需要改变我们的连接方式:

$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');

if ($mysqli->connect_error) {
    // So the user an error instead of exiting like this
    // in your production code!
    echo "Connect Error ({$mysqli->connect_errno}) {$mysqli->connect_error}";
    exit;
}

正如注释内联中所述,您应该向他们展示一个更友好的错误页面,而不是退出并向用户显示非常难看的单行错误。接下来我们将使用一种称为预准备语句的东西来确保我们的数据被清理:

$sql = "SELECT DiaryOpmerkingen, DiaryDoctorcomment FROM tblDiary 
                WHERE fk_UserId = ?
                AND DiaryDay = ?";

$stmt = $mysqli->prepare($sql);
if(!$stmt) {
  // Let the user know the query failed!
}

对于我们这些习惯于使用内联变量的标准查询的人来说,这有点陌生:

$sql = "SELECT DiaryOpmerkingen, DiaryDoctorcomment FROM tblDiary 
                WHERE fk_UserId = ?
                AND DiaryDay = ?";

基本上,问号充当占位符,我们将填充我们想要的实际值。请注意,对于字符串值,您无需在值周围加上引号:

$sql = "SELECT DiaryOpmerkingen, DiaryDoctorcomment FROM tblDiary 
                WHERE fk_UserId = ?
                AND DiaryDay = '?'"; <-- This is wrong!!

接下来是准备好的陈述的核心:

$stmt->bind_param('is', $p_iUserid, $this->Day);

在这里,我们告诉MySQLi我们想要替换问号占位符。 bind_param的第一个参数表示我们要替换的数据类型。这允许MySQLi执行健全性检查。

在这种情况下,i表示整数值,s表示字符串值。然后我们按照它们在查询中出现的顺序列出我们的值。 $p_iUserid替换第一个?$this->Day替换第二个?。现在我们执行此语句,以便获取实际数据:

$stmt->execute();

下一部分是一个非常有趣的功能:

$stmt->bind_result($diaryOpmerkingen, $diaryDoctorcomment);

这看起来很复杂,但实际上在处理查询时会更容易。这个函数的作用是创建变量$diaryOpmerkingen$diaryDoctorcommen在循环结果时用实际的列数据填充它们:

$dim = array();
while ($stmt->fetch()) {
  $dim[$diaryOpmerkingen][$diaryDoctorcomment] = array($diaryOpmerkingen, $diaryDoctorcomment);
}

注意我们如何不必使用关联数组,而是可以使用更清晰的变量名?最后,由于使用预处理语句,您可以继续交换不同的值,我们需要在完成后释放我们准备好的语句:

$stmt->close();

最后,我们关闭了主数据库连接:

$mysqli->close();

以下是完整的代码清单供参考:

$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');

if ($mysqli->connect_error) {
    // So the user an error instead of exiting like this
    // in your production code!
    echo "Connect Error ({$mysqli->connect_errno}) {$mysqli->connect_error}";
    exit;
}

$sql = "SELECT DiaryOpmerkingen, DiaryDoctorcomment FROM tblDiary 
                WHERE fk_UserId = ?
                AND DiaryDay = ?";

$stmt = $mysqli->prepare($sql);
if(!$stmt) {
  // Let the user know the query failed!
}

$stmt->bind_param('is', $p_iUserid, $this->Day);
$stmt->execute();
$stmt->bind_result($diaryOpmerkingen, $diaryDoctorcomment);

$dim = array();
while ($stmt->fetch()) {
  $dim[$diaryOpmerkingen][$diaryDoctorcomment] = array($diaryOpmerkingen, $diaryDoctorcomment);
}

$stmt->close();

$mysqli->close();

使用标准MySQL

$sql = "select DiaryOpmerkingen, DiaryDoctorcomment from tblDiary 
                WHERE fk_UserId = ".$p_iUserid."
                AND DiaryDay = '".$this->Day."';";

仅选择您需要的列。很好。但是,由于我们已经使用双引号,因此我们不需要连接运算符。同样最好从列和值中一致地分解SQL关键字:

$sql = "SELECT DiaryOpmerkingen, DiaryDoctorcomment FROM tblDiary 
                WHERE fk_UserId = $p_iUserid
                AND DiaryDay = '{$this->Day}'";

冠:

$rResult = mysqli_query($link, $sql);

您正在混淆$link and $sql parameters,还混合了mysqlimysql系列函数。除非您在其他地方有另一个连接,否则您只能使用$sql作为唯一参数:

$rResult = mysql_query($sql);

现在你要返回结果:

    return $rResult;

但是通过执行下一段代码不会被调用,所以我们可以摆脱它。最后,我们遍历结果:

    $dim = array();

    while ( $row = mysql_fetch_assoc($rResult) )
    {
      $dim[$row['DiaryOpmerkingen']][$row['DiaryDoctorcomment']] = $row;
    }

这是最终的代码:

// Make sure $this->Day is sanitized if it's user input data
$day = mysql_real_escape_string($this->Day);
$sql = "SELECT DiaryOpmerkingen, DiaryDoctorcomment FROM tblDiary 
                WHERE fk_UserId = $p_iUserid
                AND DiaryDay = '$day'";

$rResult = mysql_query($sql);
if(!$rResult) {
  //Do something with the error
}

$dim = array();
while ( $row = mysql_fetch_assoc($rResult) )
{
  $dim[$row['DiaryOpmerkingen']][$row['DiaryDoctorcomment']] = $row;
}    

答案 1 :(得分:1)

查看此页面,了解一些sql函数5 useful PHP functions for MySQL data fetching

答案 2 :(得分:0)

尝试:
while($ r = mysql_fetch_assoc($ result))

$ dim [$ r ['day']] = [“DiaryOpmerkingen”=&gt; $ r ['DiaryOpmerkingen']],'DiaryDoctorcomment'=&gt; [$ R [ 'DiaryDoctorcomment']];

答案 3 :(得分:0)

$i=0;
 while ( $row = mysql_fetch_assoc($result) )
  {
   extract($row);
$arr[$i]=$DiaryOpmerkingen;
$i++;

$arr[$i]=$DiaryDoctorcomment;

$i++;

}

请尝试此代码