0

I've used this query:

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

in phpadmin its ok, but in the code it returns false, what can be the reason?

UPDATE

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 is the source of the db class, I call it like this:

    $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

here it is any ideas?

My Config:

<?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';



}

my config file

29
  • Do you ever check for an error result from the query? If so, where and what does it say? If not, I'd try that (and make sure that name is not a column name in both tables. Commented Jun 9, 2014 at 6:49
  • @JeremyMiller I've tried var dump, and there are null values of all the properties, can't figure out whats going on because the query is valid(checked in the phpmyadmin sql prompt) the connection is stable Commented Jun 9, 2014 at 6:50
  • Could you show us the tables structures? Commented Jun 9, 2014 at 6:50
  • I think that during editing of this question, the PHP part was removed by accident. Commented Jun 9, 2014 at 6:50
  • 1
    @JeremyMiller He's implemented his own DB class, the code is all in the question. Commented Jun 9, 2014 at 6:59

2 Answers 2

3

error seems to be here on the line below near .Config::$prefix , -- where comma have to separated with dot.

$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);

replace by below line

 $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();
Sign up to request clarification or add additional context in comments.

3 Comments

the users table doesn't have a name field, it has login field, the name field belongs to users_data there no confusion
Ah, you found the line with the comma incorrect and just after the OP who edits their post changing things so that they can then just say, "I repeat" comments on your post, you edit it to replace the missing comma. Well done.
@Abhishek Don't just show the code, explain what you changed and why.
0

Use alias.* in place of *

 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

1 Comment

That will reduce the number of columns shown, but won't change the number of rows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.