Format using MYSQL Date Retrieved from Database and Display in PHP file

Olive

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

EcoG

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.

edited at
0

Comments

0 comments
Login to comment

Related

In PHP & MySQL Display date in day month Year Format from database

Display date as MONTH YEAR format from Mysql using PHP

How to display Decimal and Date Arrays from MySQL database using php

Display an image from a MySQL database using PHP

PHP MYSQL -> Display dates saved as 'date' type from MYSQL Database

How to fetch images from mysql database directly and display them in json format using php.How to do this?

display data retrieved from database from servlet to jsp file

How to format the retrieved MySQL data in JSON format using php

How can I display data from MYSQL database using HTML table from a PHP file?

How do I display data retrieved from mysql db in a specific order using php

Changing the date format while fetching from database in php mysql

How Can I Display By Date Column From Mysql Database In D-M-Y Format in PHP And Make Search Based on it

Fix the format of the retrieved data from the database using wordpress

how to set text in edit box which is retrieved from database(by using php file)

How to retrieve image file paths from mysql database and display the images using php

Display data in html/js file using NodeJs from mysql database

Not able to format DataTables data that is coming from a mysql database using php

How to display a link from the MySql database in html using jquery or php

How to display images from database mysql using php?

How to display data from database into line graph using php and mysql?

Wrong retrieved date format from SQL Server

How to format the data retrieved from a database

Special characters showing up on content retrieved from mysql using php

How to retrieved data from mysql using php and show it on javascript?

Display values retrieved from database in a table

To display the records retrieved from database into a dialog box

Date from mysql database is not displaying in html table using the date_format() function

Export only certain columns from mySQL database using PHP file

Unable to add data to MYSQL database from JSON file using PHP