I am retrieving a date from my database. Now my assignment calls to format it using MYSQL. I've tried using a similar call as I did to retrieve the data, but I keep getting the error message "Call to a member function fetch_object() on a non-object".
//RETRIEVE DATA FROM DATABASE INCLUDING $blog_date
$db = new mysqli($host,$user,$pw,$database)
or die("Cannot connect to MySQL.");
$command = "select * from ".$table_name." where blog_id=".$blogId;
$result = $db->query($command);
while ($data = $result->fetch_object())
{
$blog_title = $data->blog_title;
$blog_author = $data->blog_author;
$blog_date = $data->date_of_blog;
$blog_entry = $data->blog_entry;
}
//Using MYSQL, I try to format $blog_date using the mysql date_format; however, I keep getting the error message "Call to a member function fetch_object() on a non-object"
$command = "select date_format($blog_date, '%W, %m/%d/%Y') as formatted_date;";
$result = $db->query($command);
while ($data = $result->fetch_object())
{
$blog_date_formatted = $data->formatted_date;
}
$db->close();
I appreciate your help!!!
Olive
The error you are getting means that $result
does not have a valid data from the database.
There is a problem with your SELECT
statement. You need to specify from which database table you want to get the data. Something like this:
$command = "select date_format($blog_date, '%W, %m/%d/%Y') as formatted_date FROM table_name;";
More information on Mysql SELECT Syntax.
You can also add the following code before the while
statement:
if (!$result) printf("Error message: %s\n", $mysqli->error);
This will print for you the error which is reported by the database.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments