如何将html页面连接到mysql数据库? [英] How to connect html pages to mysql database?

查看:3910
本文介绍了如何将html页面连接到mysql数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何将 html 页面连接到mysql数据库?什么是代码?



我也想知道如何在使用HTML页面时将新数据添加到MySQL数据库以及如何发布数据?

解决方案

HTML是标记语言,基本上它们是< html> < body> ,它用于使用 css 的问题,以及作为一个整体。所有这些发生在客户系统或用户将浏览网站。

现在,连接到数据库,发生在另一个级别上。它发生在服务器上,这是托管网站的地方。

因此,为了连接到数据库并执行各种与数据相关的操作,您必须使用服务器端脚本,如 php jsp asp.net 等问题。



现在,让我们看看使用MYSQLi PHP扩展的一段连接。

  $ db = mysqli_connect('hostname , '用户名', '口令', '数据库名'); 

这个单行代码足以让您入门,可以混合这样的代码,并结合HTML标签来创建一个HTML页面,这是显示基于数据的页面。例如:

 <?php 
$ db = mysqli_connect('hostname','username','password ','数据库名称');
?>
< html>
< body>
<?php
$ query =SELECT * FROM`mytable`;;
$ result = mysqli_query($ db,$ query);
while($ row = mysqli_fetch_assoc($ result)){
//在页面上显示您的数据
}
?>
< / body>
< / html>

为了将新数据插入数据库,可以使用 phpMyAdmin 或写入 INSERT 查询并执行它们。


I want to know how to connect html pages to mysql database? What are the codes?

I also want to know how to add new data to MySQL database when I'm using HTML page and also how to publish the data?

解决方案

HTML are markup languages, basically they are set of tags like <html>, <body>, which is used to present a website using , and as a whole. All these, happen in the clients system or the user you will be browsing the website.

Now, Connecting to a database, happens on whole another level. It happens on server, which is where the website is hosted.

So, in order to connect to the database and perform various data related actions, you have to use server-side scripts, like , , etc.

Now, lets see a snippet of connection using MYSQLi Extension of PHP

$db = mysqli_connect('hostname','username','password','databasename');

This single line code, is enough to get you started, you can mix such code, combined with HTML tags to create a HTML page, which is show data based pages. For example:

<?php
    $db = mysqli_connect('hostname','username','password','databasename');
?>
<html>
    <body>
          <?php
                $query = "SELECT * FROM `mytable`;";
                $result = mysqli_query($db, $query);
                while($row = mysqli_fetch_assoc($result)) {
                      // Display your datas on the page
                }
          ?>
    </body>
</html>

In order to insert new data into the database, you can use phpMyAdmin or write a INSERT query and execute them.

这篇关于如何将html页面连接到mysql数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆