2012-12-28 77 views
-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行內容,我想全力以赴,請協助!

+0

你爲什麼要運行查詢的兩倍,並運行兩個獨立的循環?你可以結合while + foreach循環,並簡單地使用echo $ rows ['category']'。 –

回答

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

應該

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

而且這裏有個錯誤

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

    $rows[] = $row; 
    } 

$行沒有定義,所以你應該分配$行到陣列上左。將左邊的數組命名爲

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

nope。根本不重要! –

+0

@jw .:是的,[確實很重要](http://sqlfiddle.com/#!2/1f75a/2) –

+0

@juergend看到這個http://sqlfiddle.com/#!2/879e0/12 –

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(); 
?> 
相關問題