Jump to content

[SOLVED] Object of class mysqli_result could not be converted to string


gr1zzly

Recommended Posts

Ok so I have a script for paginating results on a blog style site, only I can't get the posts to output properly to the page. The web page includes() the "populate" script which itself then requires_once() the "populate_functions" script file.

 

The populate script calls to the print_page() function which in turn calls to the get_rows() function which access the database and retrieve the posts. But I can't get the results array to return back to the print_page function properly.

 

At first I tried just returning the results object variable ( $r ) which gave me the error shown in the tittle of this post. I have also tried reading $r into another variable e.g.

 

$row = mysqli_fetch_array($r, MYSQLI_ASSOC);
return array(true, $row);

 

as well as trying to read it into an array variable using

 

$row = mysqli_fetch_array($r, MYSQLI_ASSOC);
foreach ( $row AS $rec ) {$data = $rec;};
return array(true, $data);

 

the most I have got for my efforts has been single letters or numbers instead of the full text of post titles, dates and text etc or the error "Invalid argument supplied for foreach() in /php/populate_functions.inc.php on line 125".

 

The actual output function works as follows:

 

list ($chk, $data) = get_rows($dbc, $table, $is_sticky, $limit, $offset); // Retrieve posts.

if ($chk) { // Got posts OK!

echo "<h4>Step 6 : Printing posts where Sticky = {$is_sticky}!</h4>"; ### DEBUG ###
echo "<p>{$data}</p>\n"; ### DEBUG ###

foreach ($data AS $row) {

	if ( $rows_left > 0 ) {

		echo "<div class='{$table}_post is_sticky'>";

		if ($row[is_sticky] == 1) echo "<p><b><i>Sticky</i></b>";

		echo	"<span class='post_date'>Posted: '" . $row[post_dts] . "'</span></p>
		<h4>'" . stripslashes($row[post_title]) . "'</h4>
		<p>'" . stripslashes($row[post_text]) . "'</p>
		<p class='post_foot'>Posted by: '" . $row[post_auth] . "'</p>
		</div><br />\n";

	};

	$rows_left = ($rows_left - 1);

}; // End of FOREACH.

} else { // Get posts failed!
// Print errors?
}; // End of GET POSTS.

 

Any ideas what I'm doing wrong, I am new to php so go easy on me please if it's a simple stupid mistake. Thanking you in advance for your helps

Link to comment
Share on other sites

Ok, so now I've got text to output, but it's just printing the same post over and over!!

 

The $data variable is definitely being set to an array properly, but it seems like it's only storing 1 row from the db with each column in the row being stored as a separate entry.

 

What i need is each whole row being put into $data as 1 entry in an associative manner. So i'm guessing making it a 3 dimensional array where each entry in $data is a sub array for each db row, stored as 'column' => 'value'.

 

1. How do I achieve this?

 

Next I pass $data back with the return() function and iterate through each entry with a foreach statement ( see 1st post ).

 

2. How do I use foreach in conjunction with a 3D array?

 

Help please as I really need to get this working before I can move on with this project.

Link to comment
Share on other sites

Ok I think I've got it working now. Sorry I wasn't fully understanding how to use the results with an array variable. I am now using the following:

 

$r = @mysqli_query($dbc, $q); // OR $errors = mysqli_error($dbc);

if ($r) { // If OK.

$i = 0;

while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC) ) {
	$data[$i] = $row;
	$i++;
};

return array(true, $data);

}

 

Using the extra $i var to reference the $data array allowed me to create an array entry for each row as a sub array containing all the rows cell data.

 

Thanks to all your helps.

 

 

Thanks again for the help on the quoted topic, sorry again for the hijack (and my newbieness). Hope that this solution may help others.

 

>To Moderator - Could you change the title of this topic to something more suitable and search friendly such as "Storing multiple SQL results in an array variable" or something, please?  Thankyou

Link to comment
Share on other sites

Sorry I made a mistake before in addressing the array, the $i is not required -> just use the square brackets to assign a row...

 

while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC) ) {
    $data[] = $row;
};

 

Like so! Then when you come to address the variable later a simple

 

foreach ($data AS $row) {
    // Something to do with the data...
}

 

in this foreach() the $row is the whole row from the database as an associative array (depending on the query and results fetch method of course) and so can be addressed as $row[column_name], where 'column_name' is the name of the column you want the contents of.

 

Hope this helps.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.