php mysqli在查询后返回空行

时间:2014-06-09 06:46:35

标签: php mysql php-5.4

我使用了这个查询:

SELECT * FROM sillaru_users AS users 
JOIN sillaru_users_data AS users_data 
ON users.id = users_data.user_id 
WHERE name = 'user_package_id' AND users.id = 2

在phpadmin中确定,但在代码中它返回false,可能是什么原因?

更新

public function __construct($data)
{
    foreach($data as $key => $value)
    {
        $this->$key = $value;
    }

    $this->conn = mysqli_connect($this->host, $this->username, $this->password, $this->db);

    if (mysqli_connect_errno())
    {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    $this->conn->set_charset("utf8");
}

public function query($query)
{

    $this->result = $this->conn->query($query);
    return $this;
}


public function row()
{
    if($this->result)
    {
        $row = mysqli_fetch_assoc($this->result);
        mysqli_free_result($this->result);
    }
    else
    {
        $row = false;
    }
    return $row;
}

HERE是db类的源代码,我这样称呼它:

    $node = $this->db->query("SELECT * FROM ".Config::$prefix."users AS users JOIN ".Config::$prefix,"users_data AS users_data ON users.id = users_data.user_id WHERE name = 'user_package_id' AND users.id = {$id}")->row();

var_dump($node); //boolean false

这里有什么想法吗?

我的配置:

<?php

class Config
{

    public static $charset = 'UTF-8';
    public static $prefix = 'sillaru_';
    public static $maxUsersPerLevel = 3;

    public static $db = array(

        'localhost' => 'localhost',
        'prefix' => 'sillaru_',
        'db' => 'sillaru',
        'username' => 'root',
        'password' => ''
    );

    public static $currentApplication = 'sillaru';



}

我的配置文件

2 个答案:

答案 0 :(得分:3)

错误似乎在下面的行附近.Config :: $ prefix, - 其中逗号必须用点分隔。

$node = $this->db->query("SELECT * FROM ".Config::$prefix."users AS users JOIN ".Config::$prefix,"users_data AS users_data ON users.id = users_data.user_id WHERE name = 'user_package_id' AND users.id = {$id}")->row();

var_dump($node);

替换为以下行

 $node = $this->db->query("SELECT * FROM ".Config::$prefix."users AS users JOIN ".Config::$prefix."users_data AS users_data ON users.id = users_data.user_id WHERE name = 'user_package_id' AND users.id = {$id}")->row();

答案 1 :(得分:0)

使用别名。*代替*

 SELECT users.* FROM sillaru_users AS users 
 JOIN sillaru_users_data AS users_data 
 ON users.id = users_data.user_id 
 WHERE name = 'user_package_id' AND users.id = 2