【问题标题】:Pulling All Rows From Database for the Same User为同一用户从数据库中提取所有行
【发布时间】:2012-12-28 15:45:51
【问题描述】:

我已经复制并粘贴了所有可能的方法,建议如何为一个用户提取所有行,然后回显到另一个页面,但无法让它回显任何内容。代码没有给我错误,只是在我 include_once PHP 页面的地方空白。

<?php
$userid  = 10;  

/****************************************************************
* Open connection to the MySQL database
****************************************************************/

// Create new mysqli object representing a connection to the MySQL data
$mysqli = new mysqli("localhost", "username", "password", "db");

// Test if a connection error occurred
if ($mysqli->connect_errno !=0)  {
    exit;
}

/**************************************************************
* Run an SQL statement
**************************************************************/

// Create an INSERT query  
$query = "SELECT * FROM table WHERE(userid='".$userid."')";
$result = $mysqli->query($query);
if ($result = $mysqli->query($query)) {
    while ($rows = $result->fetch_assoc()) {
        $rows[] = $row;
    }
    foreach($rows as $row) {
        $category = $row['category'];
        $amount   = $row['amount'];
        echo $category;
    }
    $result->close();
}
//  Attempt to close connection
$closed = $mysqli->close();
?>

该用户有10行内容,我想全力以赴,请协助!

【问题讨论】:

  • 为什么要运行两次查询,并运行两个单独的循环?您可以结合 while+foreach 循环,只需使用 echo $rows['category']

标签: php mysql arrays mysqli


【解决方案1】:
$query = "SELECT * FROM table WHERE(userid='".$userid."')";

应该是

$query = "SELECT * FROM table WHERE userid='".$userid."'";

这里也有错误

while ($rows = $result->fetch_assoc()) {

     $rows[] = $row;
    }

$row 未定义,因此您应该将 $rows 分配给左侧的数组。将左侧数组命名为其他名称,例如

if ($result = $mysqli->query($query)) {
    while ($rows = $result->fetch_assoc()) {
        $values[] = $rows;
    }
    foreach($values as $row) {
        $category = $row['category'];
        $amount   = $row['amount'];
        echo $category;
    }
    $result->close();
}

【讨论】:

【解决方案2】:

检查您的代码是否存在可能的点故障,以下是一些注释。 如果您学习了为什么不看看 PHP PDO 类,它可以很好地替代多个数据库驱动程序。

<?php

  $userid  = 10;  


  /****************************************************************
   * Open connection to the MySQL database
   ****************************************************************/

   // Create new mysqli object representing a connection to the MySQL data
   $mysqli = new mysqli("localhost", "username", "password", "db");


   // Test if a connection error occurred
   if ( $mysqli->connect_errno != 0 )
    exit( 'error on connect' );

     /**************************************************************
    * Run an SQL statement
    **************************************************************/

    // Create an INSERT query  

    // avoid temporary variables
    //$query = "SELECT * FROM table WHERE(userid='".$userid."')";
    // reader will know taht is a query by function name
    //WARNING you should scape data coming from form
    //expecting numeric data, so convert it
    $result = $mysqli->query( "SELECT * FROM table WHERE(userid='".intval( $userid )."')" );

    // error here
    //if ($result = $mysqli->query($query)) {
    //
    if ( !($result = $mysqli->query($result)) )
      exit( 'query error' );
    // error here also changed variable name
    //while ($rows = $result->fetch_assoc())
    // $rows[] = $row;
    $rows = array();
    while ( $tmp = $result->fetch_assoc() )
     $rows[] = $tmp;

    foreach($rows as $row)
    {
     // unnecessary code also, duplicating variable
     //$category  = $row['category'];
     //$amount    = $row['amount'];
     //echo $category;
      echo $row['category'];
    } 

     $result->close();
    }


//  Attempt to close connection
$closed = $mysqli->close();
?>

【讨论】:

    猜你喜欢
    • 2019-09-28
    • 2017-11-04
    • 1970-01-01
    • 1970-01-01
    • 2016-09-14
    • 2016-06-28
    • 2011-08-18
    • 2022-06-13
    • 1970-01-01
    相关资源
    最近更新 更多