views:

124

answers:

5

Hello, I'm currently trying to extract data from a table, and am using this:

 $online = mysqli_fetch_field(mysqli_query($db, 
      "SELECT `online` FROM `tbl_system` WHERE `property` = 'extranet'"));

However, it's not working as echoing $online give "Array".

Here's a var_dump from $online

object(stdClass)#3 (11) { 
               ["name"]=> string(6) "online" 
               ["orgname"]=> string(6) "online" 
               ["table"]=> string(10) "tbl_system" 
               ["orgtable"]=> string(10) "tbl_system" 
               ["def"]=> string(0) "" 
               ["max_length"]=> int(1) 
               ["length"]=> int(11) 
               ["charsetnr"]=> int(63) 
               ["flags"]=> int(36865) 
               ["type"]=> int(3) 
               ["decimals"]=> int(0) }
+3  A: 

You want:

$query = mysqli_query($db, "SELECT online FROM tbl_system WHERE property = 'extranet'");
$row = mysqli_fetch_array($query);
$online = $row[0];

mysqli_fetch_field() is for retrieving column definitions, not data, and it's working perfectly well: giving you back a column definition object.

mysqli_fetch_array(), mysqli_fetch_assoc(), and mysqli_fetch_object() are for data retrieval.

chaos
+1  A: 

Unless I'm mistaken, there is no way to fetch just one column's data directly. You will need to fetch the whole row, even though it's just one column, and then manually take the value from the resulting array.

edit: chaos's answer looks pretty much like what I meant.

Jani Hartikainen
+1  A: 

The function you're looking for is not mysqli_fetch_field(). It extracts the information about the field but not the field data itself.

Try this instead:

$row = mysqli_fetch_assoc(mysqli_query($db, "SELECT `online` FROM `tbl_system` WHERE `property` = 'extranet'"));
echo $row['online'];
kizzx2
+3  A: 

Try one of the following functions:

  • mysqli_fetch_array — Fetch a result row as an associative, a numeric array, or both

    $row = mysqli_fetch_array($result);
    echo $row[0]; // or
    echo $row['online'];
    
  • mysqli_fetch_assoc — Fetch a result row as an associative array

    $row = mysqli_fetch_assoc($result);
    echo $row['online'];
    
  • mysqli_fetch_object — Returns the current row of a result set as an object

    $row = mysqli_fetch_object($result);
    echo $row->online;
    
  • mysqli_fetch_row — Get a result row as an enumerated array

    $row = mysqli_fetch_row($result);
    echo $row[0];
    

With $result being:

$result = mysqli_query($db, "SELECT `online` FROM `tbl_system` WHERE `property` = 'extranet'");
Gumbo
+1  A: 

It's a little bit offtopic, but I recommend you to use higher level framework for working with database, like http://adodb.sourceforge.net/ for example

nightcoder
Thanks :) It looks useful and I certainly will consider it!
Shamil