0
votes

I am running

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

that should return 92 but its returning the below why?

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 Threads: 14 Questions: 30097132 Slow queries: 13 Opens: 1937675 Flush tables: 1 Open tables: 400 Queries per second avg: 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}

3

3 Answers

6
votes

It is returning mysqli_ object.So Try to get the result like

$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>";
}

And it is appreciable that you are using mysqli_* functions instead of deprecated mysql_* functions

3
votes

It's returning a mysqli_result object, exactly as the manual says it does.

To get the actual id you need to call fetch_assoc() (or similar) on the object.

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
votes

Basically the statement:

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

returns an object that can be used to extract the result set or table and assigned to a variable... so you need to create a variable and assign the result set to it as:

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

}