An issue with MySQL query in php

$users_id = $this->db->query('SELECT users_id FROM users ORDER BY daily_points desc limit 1');
$text = 'Congratulations! You are the winner for today!';
$this->game->send_message( '0', $users_id, $text, 'yes' );

I'd like to ask what's my mistake here with the $users_id ? If I execute the query via MySQL I get the requested info but however if I run the script php, it doesn't return any value for that? Why is that?

Answers


After the query you need to fetch the results first:

$result = $this->db->query('
    SELECT users_id FROM users 
    ORDER BY daily_points desc limit 1')
->result_array(); // you need to fetch it either result()/result_array()

$text = 'Congratulations! You are the winner for today!';
$this->game->send_message( '0', $result[0]['users_id'], $text, 'yes' );

Your code is not clear, there is a lot of posibilities to determine what is happening. Try debbuging your $users_id variable to see what is into it.

$users_id = $this->db->query('SELECT users_id FROM users ORDER BY daily_points desc limit 1');
var_dump($users_id);
$text = 'Congratulations! You are the winner for today!';
$this->game->send_message( '0', $users_id, $text, 'yes' );

If that is a resource then you will need to fetch the result to see that as @Ghost said.


The query()-method returns an object. There are multiple possibilities to get the wanted value, e.g. by this:

$users_id = $this->db->query('
    SELECT users_id FROM users ORDER BY daily_points desc limit 1
')->row()->users_id;

For a reference, see the CodeIgniter Guide


Need Your Help

MessageSecurityException, UsernamePasswordValidator, and HttpWebRequest Problem

c# .net wcf

is there a better way to return back the standard "Invalid username/password" message to clients that connect to a WCF service requiring credentials? I have the following code, which causes an exce...

MVC Beta Attribute (need help)

asp.net-mvc handleerror asp.net-mvc-beta1

I've tried this both with and without the 'ExceptionType' parameter. I have an Error.aspx page in both the Views/Shared folder and the Views/thisController folder. But everytime I run this I get a

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.