0

我在跑步

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

那应该返回92但它返回下面为什么?

object(CI_DB_mysqli_result)#150 (8) { ["conn_id"]=> object(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) "Uptime: 106781 线程: 14 问题: 30097132 慢查询: 13 打开: 1937675 刷新表: 1 打开表:平均每秒 400 次查询:281.858" ["sqlstate"]=> string(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) ["lengths"] => NULL ["num_rows"]=> int(1) ["type"]=> int(0) } ["result_array"]=> array(0) { } ["result_object"]=> array(0) { } ["custom_result_object"]=> array(0) { } ["current_row"]=> int(0) ["num_rows"]=> NULL ["row_data"]=> NULL}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) ["lengths"]=> NULL ["num_rows"]=> int(1) ["type"]=> int(0) } ["result_array "]=> array(0) { } ["result_object"]=> array(0) { } ["custom_result_object"]=> array(0) { } ["current_row"]=> int(0) ["num_rows "]=> NULL ["row_data"]=> NULL}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) ["lengths"]=> NULL ["num_rows"]=> int(1) ["type"]=> int(0) } ["result_array "]=> array(0) { } ["result_object"]=> array(0) { } ["custom_result_object"]=> array(0) { } ["current_row"]=> int(0) ["num_rows "]=> NULL ["row_data"]=> NULL}NULL ["num_rows"]=> int(1) ["type"]=> int(0) } ["result_array"]=> array(0) { } ["result_object"]=> array(0) { } ["custom_result_object"]=> array(0) { } ["current_row"]=> int(0) ["num_rows"]=> NULL ["row_data"]=> NULL}NULL ["num_rows"]=> int(1) ["type"]=> int(0) } ["result_array"]=> array(0) { } ["result_object"]=> array(0) { } ["custom_result_object"]=> array(0) { } ["current_row"]=> int(0) ["num_rows"]=> NULL ["row_data"]=> NULL}

4

3 回答 3

6

它正在返回 mysqli_ 对象。所以尝试得到类似的结果

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

于 2013-06-12T05:46:40.690 回答
3

它返回一个mysqli_result对象,正如手册所说的那样。

要获得实际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();
}
于 2013-06-12T05:46:47.280 回答
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'];

}
于 2017-07-31T12:22:03.273 回答