简体   繁体   中英

Showing sql data in HTML table using php

I'm very, very new to HTML, PHP and SQL, so this question might sound less than clever, but the situation is starting to annoy me.

I want to display data from my Mysql-database named "movies" in an HTML-table on my website. The table is named "test". I understand that I need php to process the query from sql to html.

However, and I've read a lot of answers/tutorials both here and elsewhere, I can't get my code to work.

Here's the code:

<?php
$connection = mysql_connect('localhost', 'root', 'password');
mysql_select_db('movies');

$query = "SELECT * FROM test";
$result = mysql_query($query);

echo "<table>";

while($row = mysql_fetch_array($result)){
echo "<tr><td>" . $row['Name'] . "</td></tr>"; 
}

echo "</table>";

mysql_close();

?>

To make it as simple as possible for me, the table contains only two entries with only and ID and a name.

This is what displays when I open the website in html-format in Chrome:

"; while($row = mysql_fetch_array($result)){ echo "" . $row['Name'] . ""; } echo ""; mysql_close(); ?>

I can't figure out where the problem lies, so now I turn to here. If my goal is to display the two entries from my table in a table on my website, where do I need to fix what in my code?

Is it simply because I can't open the file anymore in Chrome, when I have php-code included? If so, what program would I need to open the file in?

Thanks for your time and hopefully a little help!

Try using mysqli_query($connection, $query) instead of mysql_query($query) and mysqli_fetch_array($result) instead of mysql_fetch_array($result) .

Notice the $connection variable which is necessary with the new mysqli .

You also need to use the new mysqli_connect. For example, try,

$connection = mysqli_connect( 'localhost, 'root', 'password', 'movies');

I assume 'test' is a table in the 'movie' database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM