从同一用户的数据库中拉出所有行

时间:2012-12-28 15:45:51

标签: php mysql arrays mysqli

我已经复制并粘贴了所有可能的方法,建议如何为一个用户拉出所有行,然后回显到另一个页面,但无法让它回显任何东西。代码不会给我错误只是空白我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行内容,我想全力以赴,请协助!

2 个答案:

答案 0 :(得分:0)

$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();
}

答案 1 :(得分:0)

检查您的代码是否存在可能的点故障,以下是一些注意事项。 如果你学习了为什么不看看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();
?>