返回mysqli对象而不是结果

时间:2013-06-12 05:39:05

标签: php codeigniter

我正在运行

$this->db->query("SELECT `id` FROM $table WHERE $table.id ='$product_id'");

应该返回92但是它返回下面的原因?

  

对象(CI_DB_mysqli_result)#150(8){[“conn_id”] =>对象(mysqli的)#16   (18){[“affected_rows”] => int(1)[“client_info”] => string(6)“5.5.30”   [ “client_version”] => int(50530)[“connect_errno”] => INT(0)   [ “connect_error”] => NULL [“errno”] => int(0)[“error”] => string(0)“”   [ “场计数”] => int(1)[“host_info”] => string(25)“Localhost via   UNIX套接字“[”info“] => NULL [”insert_id“] => int(0)[”server_info“] =>   string(10)“5.5.31-cll”[“server_version”] => int(50531)[“stat”] =>   string(150)“正常运行时间:106781主题:14个问题:30097132慢   查询:13打开:1937675刷新表:1打开表:400查询   每秒平均值:281.858“[”sqlstate“] =>字符串(5)”00000“   [ “PROTOCOL_VERSION”] => int(10)[“thread_id”] => INT(373292)   [ “WARNING_COUNT”] => int(0)} [“result_id”] =>对象(mysqli_result)#161   (5){[“current_field”] => int(0)[“field_count”] => INT(1)   [ “长度”] => NULL [“num_rows”] => int(1)[“type”] => int(0)}   [ “result_array”] => array(0){} [“result_object”] =>数组(0){}   [ “custom_result_object”] => array(0){} [“current_row”] => INT(0)   [ “NUM_ROWS”] => NULL [“row_data”] => NULL}

3 个答案:

答案 0 :(得分:4)

它正在返回mysqli_ object.So尝试获取结果,如

$query = $this->db->query("SELECT `id` FROM $table WHERE $table.id ='$product_id'");
$result = $query->result(); 
foreach($result as $row)
{
     echo "Id is ".$row['id']."<br>";
}

你可以使用mysqli_ *函数代替不推荐使用的mysql_ *函数

答案 1 :(得分:2)

它正在返回一个mysqli_result对象,就像manual所说的那样。

要获得实际id,您需要在对象上调用fetch_assoc()(或类似)。

if ($result = $this->db->query("SELECT id FROM $table WHERE $table.id ='$product_id'")) {

    /* fetch associative array */
    while ($row = $result->fetch_assoc()) {
        printf ("Fetched ID: %s\n", $row["id"]);
    }

    /* free result set */
    $result->free();
}

答案 2 :(得分:0)

基本上是声明:

$mysqli = new mysqli("localhost","rinonymous","03318987165oo","rinonymous");

if ($mysqli->connect_errno) {
    print_r($mysqli->connect_error);
    exit();
}

$site_title = "Rinonymous";
$page_title = "";
$page_body = "";

#Page Setup

$query_page_info = "select * from pages where id = 1";

foreach ($mysqli->query($query_page_info) as $row) {
    print_r($mysqli->query($query_page_info));
    #query method returns an associate array 

    print_r($row);
    $page_title = $row['title'];
    $page_body = $row['body'];

}

返回一个对象,可用于提取结果集或表并分配给变量...因此您需要创建变量结果集指定为

{{1}}