执行查询后取值

take value after execute query

我是 Codeigniter 及其开发的新手。我已经成功地测试了一个查询。但是当我尝试获取 voteid 值时出现错误请帮助我。

$query=$this->db->query("SELECT voteid FROM Questions WHERE questionid = '$questionid'");

        echo $query->voteid; //im getting errors here.

Var_dump 查询值

object(CI_DB_mysql_result)#18 (8) {
  ["conn_id"]=>
  resource(30) of type (mysql link persistent)
  ["result_id"]=>
  resource(39) of type (mysql result)
  ["result_array"]=>
  array(0) {
  }
  ["result_object"]=>
  array(0) {
  }
  ["custom_result_object"]=>
  array(0) {
  }
  ["current_row"]=>
  int(0)
  ["num_rows"]=>
  int(1)
  ["row_data"]=>
  NULL

错误

Severity: Notice

Message: Undefined property: CI_DB_mysql_result::$voteid

Filename: models/questions_model.php

您的查询 returns 没有,因为它不正确。尝试: "SELECT voteid FROM Questions WHERE questionid = " 。 $questionid(我假设$questionid是整型变量)

试试这种写查询的方式:

$this->db->select( 'voteid' );
$this->db->from  ( 'Questions' );
$this->db->where ( 'questionid', $questionid );

$result = $this->db->get( );

if ( $result->num_rows( ) > 0 ) 
{ 
    $row = $result->row( );
    $row->voteid;
} 

试试这个:

$query=$this->db->query( "SELECT voteid FROM Questions WHERE questionid = $questionid" );
$row = $query->row(); 
return $row->voteid;

使用“$questionid”意味着您的变量是一个字符串,但在本例中它是一个整数。

$query=$this->db->query("SELECT * FROM Questions WHERE questionid = '$id'");

echo $query->row()->voteid;

试试这个应该有用