-1

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

4

2 回答 2

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();
}
于 2012-12-28T15:47:15.610 回答
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();
?>
于 2012-12-28T16:00:59.903 回答