php中的MySQL查询问题

时间:2014-09-27 16:59:12

标签: php mysql codeigniter

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

我想问一下$users_id我的错误是什么? 如果我通过My​​SQL执行查询,我会得到所请求的信息但是如果我运行脚本php,它不会返回任何值吗?那是为什么?

3 个答案:

答案 0 :(得分:0)

在查询之后,您需要先获取结果:

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

答案 1 :(得分:0)

您的代码不清楚,确定发生的事情有很多可能性。 尝试对你的$ users_id变量进行重新整理,看看它是什么。

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

如果那是一个资源,那么你需要获取结果,看看@Ghost说的那样。

答案 2 :(得分:0)

query() - 方法返回一个对象。获得所需值有多种可能性,例如:通过这个:

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

有关参考,请参阅CodeIgniter Guide