简体   繁体   English

php函数Mysql输出错误

[英]Wrong output from php function Mysql

My code is as follows: 我的代码如下:

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>";
}

And my table's data is 我表的数据是

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

The actual output is: 实际输出为:

Anne 
Anne

The desired output is: 所需的输出是:

Anne
Jane

Can someone tell me what I am doing wrong. 有人可以告诉我我在做什么错。 It seems like I have missed something basic. 似乎我错过了一些基本的知识。 I have read over 50 articles and nothing seems to explain what I am doing wrong. 我已经阅读了50多篇文章,似乎没有什么可以解释我做错了什么。

Note: This is a scaled down version of my code. 注意:这是我的代码的缩小版本。 I intend to use this to make a more general object that pulls information from my database. 我打算使用它来创建一个更通用的对象,该对象从数据库中获取信息。

Thanks, 谢谢,

Brett 布雷特

You need to call mysql_fetch_assoc for each row. 您需要为每一行调用mysql_fetch_assoc It only returns one row of data, not the full set. 它仅返回一行数据,而不返回全部数据。 For example, you could move it out into the loop: 例如,您可以将其移出循环:

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>";
}

You can use a while loop there because after the last row has been retrieved, mysql_fetch_assoc will return FALSE and exit the loop. 您可以在其中使用while循环,因为在检索到最后一行之后, mysql_fetch_assoc将返回FALSE并退出循环。

In this section of your code: 在代码的这一部分中:

return mysql_fetch_assoc($result);

You are merely returning the first row. 您仅返回第一行。 I suggest you to create an array. 我建议您创建一个数组。

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;
}

Should'nt it be : 不应该是:


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

And: 和:


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

A sane version of your class. 您班级的理智版本。 It lacks vital parts of course, to be used in the real life, but just out of your sketch: 当然,它缺少重要的部分,无法在现实生活中使用,只是出于您的想法:

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>";
}

Seems kind of odd that no one has picked up on a bit of a gaff in the code here. 似乎有些奇怪,在这里的代码中没有人发现任何问题。

You define select() like so: 您可以这样定义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);
}

But, then you call it like this: 但是,您可以这样称呼它:

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

I assume your intention was to be able to specify the table and the fields to select from the database. 我认为您的意图是能够指定表和字段以从数据库中选择。 If it was, your select function should look more like this: 如果是这样,则您的select函数应如下所示:

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

    return mysql_query($q);
} 

From there you would follow @BenLee's example in his answer, since you need to iterate over a resultset. 从那里开始,您将按照@BenLee的示例进行回答,因为您需要遍历结果集。 Each field becomes a key in an associative array. 每个字段都成为关联数组中的键。

I wouldn't recommend actually doing string insertion like this in production code, but I think it's closer to what you intended. 我不建议实际在生产代码中像这样进行字符串插入,但我认为它更接近您的意图。

HTH. HTH。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM