php函数Mysql的输出错误


Wrong output from php function Mysql

我的代码如下:

class Database  
{  
    private $db_host;  
    private $db_user;  
    private $db_pass;  
    private $db_name;  
    private $con;
    public function __construct() {
        $this->db_host = "localhost";  
        $this->db_user = "admin";  
        $this->db_pass = 'password';  
        $this->db_name = 'test';    
        $this->con = '';
    }
    public function connect() {
        $db_name = "test";    
        $this->con = mysql_connect($this->db_host, $this->db_user, $this->db_pass);
    }  
    public function select(){
        $q = "SELECT name, city FROM customers;";
        mysql_select_db($this->db_name, $this->con);
        $result = mysql_query($q);
        return mysql_fetch_assoc($result);
    }  
} 

$db = new Database();
$db->connect();
$tempArray = Array();
$rs = $db->select('customers', 'name, suburb');
foreach ($rs as $row)
{
    echo $rs['name'] . "<br>";
}

我的表的数据是

name | city
--------------
Anne | Sydney
Jane | London

实际输出为:

Anne 
Anne

所需输出为:

Anne
Jane

有人能告诉我我做错了什么吗。我好像错过了一些基本的东西。我读了50多篇文章,似乎没有什么能解释我做错了什么。

注意:这是我的代码的缩小版本。我打算使用它来制作一个更通用的对象,从我的数据库中提取信息。

谢谢,

Brett

您需要为每行调用mysql_fetch_assoc。它只返回一行数据,而不是完整的数据集。例如,您可以将其移出循环:

class Database
{  
    /* ... */
    public function select(){
        $q = "SELECT name, city FROM customers;";
        mysql_select_db($this->db_name, $this->con);
        return mysql_query($q);
        /* Remove your line here, returning the query result, not the first row */
    }  
} 
$db = new Database();
$db->connect();
$tempArray = Array();
$result = $db->select('customers', 'name, suburb');
/* Note that I'm now using mysql_fetch_assoc to get each row from the result */
while ($row = mysql_fetch_assoc($result));
    echo $row['name'] . "<br>";
}

您可以在那里使用while循环,因为在检索到最后一行之后,mysql_fetch_assoc将返回FALSE并退出循环。

在代码的这一部分中:

return mysql_fetch_assoc($result);

你只是在返回第一排。我建议你创建一个数组。

public function select(){
    $q = "SELECT name, city FROM customers;";
    mysql_select_db($this->db_name, $this->con);
    $result = mysql_query($q);
    $toReturn = array();
    while($row = mysql_fetch_assoc($result) )
        $toReturn[] = $row;
    return $toReturn;
}

应该是:吗


foreach ($rs as $row)
{
    echo $row['name'] . "<br>";
}

和:


$resArr = array();
while($res = mysql_fetch_assoc($result)) {
  $resArr[] = $res;
}
return $resArr;

您的类的一个健全版本。当然,它缺乏在现实生活中使用的重要部分,但只是在你的草图中:

class Database  
{  
    private $con;
    public function __construct() {
        $this->con = mysql_connect("localhost", "admin", 'password');
        mysql_select_db("test",$this->con);
    }
    public function query($sql,$this->con){
        return mysql_query($sql);
    }  
    public function get_all($sql,$this->con){
        $ret = array();
        $res = mysql_query($sql);
        while  ($row = mysql_fetch_array($row)) {
          $ret[] = $row;
        }
        return $ret;
    }  
} 
$db = new Database();
$rs = $db->get_all("SELECT name, city FROM customers");
foreach ($rs as $row)
{
    echo $rs['name'] . "<br>";
}

似乎有点奇怪,没有人在这里的代码中发现一点gaff。

你这样定义select()

public function select(){
    $q = "SELECT name, city FROM customers;";
    mysql_select_db($this->db_name, $this->con);
    $result = mysql_query($q);
    return mysql_fetch_assoc($result);
}

但是,你可以这样称呼它:

$rs = $db->select('customers', 'name, suburb');

我认为您的意图是能够指定要从数据库中选择的表和字段。如果是的话,你的select函数应该更像这样:

public function select($table, $fields){
    $q = "SELECT $fields FROM $table;";
    mysql_select_db($this->db_name, $this->con);
    return mysql_query($q);
} 

从那里你可以按照@BenLee在他的回答中的例子,因为你需要迭代一个结果集。每个字段都成为关联数组中的一个关键点。

我不建议在生产代码中实际执行这样的字符串插入,但我认为它更接近您的意图。

HTH。