Home php How to display data from MySQL with PHP?

How to display data from MySQL with PHP?

Author

Date

Category

There is a news table, in it 5 fields ID, Text, Title, Author, Date . You must display this data on the page. I can’t do it in any way: all the time any errors. I tried to use the mysql_fetch_assoc function, but something, how to apply it, I did not quite understand the same – the same EXPECTS PARAMETER 1 TO BE RESOURCE, BOOLEAN .
Of course, I understand what it means, but how to fix it – no …

$ news = mysql_query ("select * from` news` ");
While ($ row = mysql_fetch_assoc ($ news, mysql_assoc)) {
  Foreach ($ ROW AS $ Col_Value) {
    Print "\ t \ t & lt; TD & GT; $ col_value & lt; / td & gt; \ n";
  }
}

Answer 1, Authority 100%

Actually, everything is very simple, functions for working with MySQL from PHP a lot (see the documentation). Here is a small example of data output:

$ db_host = ''; // Your host
$ db_name = ''; // Your database
$ db_user = ''; // User BD
$ db_pass = ''; // password to database
mysqli_report (mysqli_report_error | mysqli_report_strict); // Turn the error messages
$ mysqli = new mysqli ($ db_host, $ db_user, $ db_pass, $ db_name); // Connect with database server
$ mysqli- & gt; set_charset ("UTF8MB4"); // Set the encoding
$ result = $ mysqli- & gt; Query ('select * from `table_name`'); // Selection Request
while ($ row = $ result- & gt; fetch_assoc ()) // Get all lines in a cycle one
{
  Echo '& Lt; P & GT; Recording id ='. $ Row ['id']. '. Text: '. $ Row [' Text '].' & Lt; / p & gt; '; // Display data
}

Answer 2, Authority 22%

Allow yourself to post an example given above, a bit fixed, using MySQL , which is not recommended to use:

$ sdd_db_host = ''; // Your host
$ sdd_db_name = ''; // Your database
$ sdd_db_user = ''; // User BD
$ sdd_db_pass = ''; // password to database
$ Conn = MySQL_Connect ($ sdd_db_host, $ sdd_db_user, $ sdd_db_pass); // Connect with database server
if (! $ Conn)
{
  Throw New Exception ('Failed to connect to the database! Check the connection settings');
}
if (! mysql_select_db ($ sdd_db_name, $ conn)) // Selection of database
{
  Throw New Exception ("Failed to select a database {$ ssd_db_name}!");
}
$ result = mysql_query ('select * from `table_name`', $ Conn); // Selection Request
if (! $ Result)
{
  Throw New Exception (Sprintf ('failed to execute the database, error code% d, error text:% s', mysql_errno ($ Conn), MySQL_ERROR ($ Conn)));
}
While ($ row = mysql_fetch_array ($ result))
{
  Echo '& Lt; P & GT; Recording id ='. $ Row ['id']. '. Text: '. $ Row [' Text '].' & Lt; / p & gt; '; // Display data
}

Answer 3

$ resul = mysql_query ("select * from` news` Order by`s id` DESC ");
$ ar = mysql_fetch_array ($ resul);
do.
{
Echo "". $ AR [NEWS]. "|" $ AR [DATE]. "";
}
While ($ ar = mysql_fetch_array ($ resul));

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions