0

I need to get an object $resultInput to be viewed as an array (like $resultInput commented out). However when I use the function object to array I get an error Resource id #5. What is the issue?

require('dbc.php');

mysql_select_db($db);
$resultInput = mysql_query("SHOW COLUMNS FROM about WHERE Field NOT IN ('id', 'created', 'date_modified', 'last_modified', 'update', 'type', 'bodytext') AND Field NOT REGEXP '_image'"); // selects only the columns I want

//$resultInput = array('page_header', 'sub_header', 'content', 'content_short');

function objectToArray($object){
    if(!is_object($object) && !is_array($object)){
        return $object;
    }
    if(is_object($object)){
        $object = get_object_vars( $object );
    }
    return array_map( 'objectToArray', $object );
}

// convert object to array
$array = objectToArray( $resultInput );

//show the array
print_r( $array );
2

2 Answers 2

5

You are getting this error because mysql_query() returns a resource, not an object. To get an object, you would need to use one of the mysql functions to retrieve the rowset as an object (i.e. mysql_fetch_object()). However, you don't need the objectToArray function, as you can just use mysql_fetch_array to fetch the rowset as an array, like so:

$result = mysql_query("...");
$resultInput = mysql_fetch_array( $result);

Also, I'm sure somebody will comment about using the now-deprecated mysql functions. But, just to be thorough, you should be looking into using either mysqli or PDO.

So you got this as output:

array(12) {
    [0] = > string(11)"page_header"
      ["Field"] = > string(11)"page_header" 
    [1] = > string(11)"varchar(50)" 
      ["Type"] = > string(11)"varchar(50)" 
    [2] = > string(3)"YES" 
      ["Null"] = > string(3)"YES" 
    [3] = > string(0)"" 
      ["Key"] = > string(0)"" 
    [4] = > NULL
      ["Default"] = > NULL
    [5] = > string(0)"" 
      ["Extra"] = > string(0)""
}

Which is describing the page_header column. To just display the column names for all columns, you would do:

$result = mysql_query("..."); $columns = array();
while( $row = mysql_fetch_array( $result) {
    $columns[] = $row['Field'];
}

var_dump( $columns);
Sign up to request clarification or add additional context in comments.

3 Comments

I dont want to fetch the rowset I want to get the column names as defined in SHOW COLUMNS from. Which I realize is mysql_fetch_assoc
... The column names will be returned in the rowset. Do var_dump( $resultInput); and you'll see them...
Interesting... forgive me for I am a novice. But how does one get just the names of the columns into an array instead of this: array(12) { [0]=> string(11) "page_header" ["Field"]=> string(11) "page_header" [1]=> string(11) "varchar(50)" ["Type"]=> string(11) "varchar(50)" [2]=> string(3) "YES" ["Null"]=> string(3) "YES" [3]=> string(0) "" ["Key"]=> string(0) "" [4]=> NULL ["Default"]=> NULL [5]=> string(0) "" ["Extra"]=> string(0) "" } I dont need all this varchar and key information
0

As of your code now, you are not returning a result from your MySQL server. For that, you need to pass your mysql_query result to a mysql_fetch_* function.

For your use, you can do mysql_fetch_assoc($result);, which will retain column/value association.

Now, if you were using MySQLi and had mysqli_fetch_object(), you can use the below to turn the object to array, and vice versa.

You can do the following:

Typecasting:

$array = new array();
$object = (object) $array;

or

$object = new stdClass();
$array = (array) $object;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.