2013-06-12 40 views
0

我正在mysqli的对象返回而不是结果

$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)“” [“field_count”] => int(1)[“host_info”] => string(25)“Localhost via UNIX socket”[“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”] => object(mysqli_result)#161 (5){[“current_field”] => int(0)[“field_count”]] => int(1) [“result_array”] => array(0)=> int(1) [“length”] => NULL [“num_rows”] => int {“result_object”] => array(0){} [“custom_result_object”] => array(0){} [“current_row”] => int(0) [“num_rows”] => NULL [ “row_data”] => NULL}

回答

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_ *函数

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(); 
} 
0

基本上声明:

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

返回可用于提取结果的对象设置或表并分配给一个变量...所以你需要创建一个变量分配结果集以它为:

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

} 
相关问题