Page 1 of 1

I need help converting a MySQL object to a string

PostPosted: 05. July 2016 02:48
by Chiriel
XAMPP version is 7.0.4
Installation file type Application (exe)
Full name is xampp-win32-7.0.4-0-VC14-installer

My Operating System is Windows 10
64 bit Operating System/x64 processor
Home Edition
*Not sure where to find the service pack level

The installation path is C:\xampp\new_directory


I'm currently trying to make a navigation menu from one of my columns in my mysql table.

Okay, so in my PHP class Database file, I have this function:

public function get_food_type(){
return $this->query("SELECT food_type FROM menu");
}


And in the other file I have the following code:

<?php

require_once('Includes/wzDB.php');

$content = "<div id=\"menu\">"
. "<nav id=\"food\">"
. "<ul>"

. $type=wzDB::getInstance()->get_food_type();
print($type);
if($type->num_rows>0){
while($row = $type->fetch_assoc()){
echo "<li>" . htmlspecialchars($row['food_type']) . "</li>";
}
}


"</ul>"
. "</nav>"
. "</div>";


But when I try to run the file, I get the following error message

Catchable fatal error: Object of class mysqli_result could not be converted to string in C:\xampp\new_directory\htdocs\Wu_Zen2\menu.php on line 8

How do I fix this error?

Re: I need help converting a MySQL object to a string

PostPosted: 05. July 2016 13:39
by Nobbie
This is horrible Code, remove the concatenation in front of the line "$type=wzDB::getInstance()->get_food_type()" and insert a semicolon instead in the end of the line before. What did you think when you coded that??

Wrong:

Code: Select all
$content = "<div id=\"menu\">"
. "<nav id=\"food\">"
. "<ul>"
. $type=wzDB::getInstance()->get_food_type();


vs. Correct:

Code: Select all
$content = "<div id=\"menu\">"
. "<nav id=\"food\">"
. "<ul>";
$type=wzDB::getInstance()->get_food_type();


But I think you will receive some more errors, as the lonesome HTML strings in the bottom of the file are also totally wrong. That is not valid PHP syntax.

You should get some basic tutorials about PHP and read them carefully and work out some examples. That code is a pain.