使用php将html连接到mysql - 全新的php

我想了解一些一般性问题。</ p>

对于我一直在闲暇时间的项目,我试图在网站上为每周一次的国际象棋锦标赛展示一个基于阶梯的排名表。 为此,我创建了一个MYSQL数据库来保存每个玩家的赢/输/锦标赛胜利。 据我所知,为了将这些数据显示在基于html的网站上,我需要使用php来连接数据库和网站。</ p>

我对php的理解也很浅 服务器端语言,而不是客户端语言。 基本上对我而言,这意味着php只能通过托管该网站的在线服务器来理解,而不能通过网络浏览器(例如Google Chrome)本身来理解。 因此,这意味着我不能简单地通过在Notepad ++中打开它并选择“在Chrome中运行”来运行.php文件。 这是行不通的 - 我只能通过首先将它们保存到网站目录并在线查看来测试php文件。 这是正确的思考方式吗?</ p>

我的第二个问题更简单 - 它涉及连接我的网站和MYSQL服务器所需的步骤。 这是我的第一次尝试:</ p>


$ db = mysql_connect(“a2412233_ss”,“a2412233_ss”,'My_Password ');

if(!$ db){

?&gt; ;

&lt; html&gt;
&lt; head&gt;
&lt; Title&gt; MySQL表连接&lt; / Title&gt;
&lt; / head&gt;
&lt; body&gt;

&lt; / body&gt;

&lt; / html&gt;
</ code> </ pre>

我希望得到解决的两个问题:</ p> \ n

1)当我从网站上的index.html点击此文件的链接时,php文件会下载而不是像普通网页那样在浏览器窗口中打开。 显然有些东西是错的。</ p>

2)其次,我对这样写的事实感到不舒服,任何人都可以通过查看此页面源来查看我的数据库信息/密码。 所以必须有一个更好的方法来实现这一点,通过查看源代码看不到php。</ p>

谢谢大家的澄清和正确的方向! 慢慢得到这个html / MYSQL / php的东西。</ p>
</ div>



I have a few general questions I would like to have a better understanding on.

For a project I have been spending my free time on, I am attempting to display a ladder-based rankings table for a weekly-based chess tournament on a website. To do this, I have created a MYSQL database to hold each player's wins/losses/tournament wins. I understand that in order to display this data onto a html-based website, I need to use php to connect the database and the site.

I also have a very shallow understanding of php as a server-side language, and not a client-side one. Basically to me, this means that php can only be understood by the online server hosting the site, and not by the web browser (such as Google Chrome) itself. Consequently, this means that I can't simply run a .php file by opening it in Notepad++ and choosing "Run in Chrome". That will not work - I can only test php files by first saving them into the website directory and viewing them online. Is this the correct way of thinking about this?

My second question is more straightforward - and it involves the steps required to connect my website and the MYSQL server. Here is my first attempt:

//Connect to database

$db = mysql_connect("a2412233_ss","a2412233_ss", 'My_Password');

if (! $db){
    die("Database connection failed: " . mysql_error());


    <Title> MySQL Table Connection </Title>


Two problems with this that I would love to get cleared up:

1) When I click the link to this file from my index.html on the website, the php file downloads rather than opening up on the browser window like a normal webpage should. So clearly something is wrong there.

2) Secondly, I am uncomfortable with the fact that written this way, anyone can view my database info/password by viewing this page source. So there has to be a better way of doing this where that php isn't visible by viewing the source.

Thank you all for the clarification and kick in the right direction! Slowly getting this html/MYSQL/php stuff figured out.

duanlei5339 “当我从网站上的index.html点击此文件的链接时,php文件会下载而不是”:听起来好像你没有安装PHP。
大约 6 年之前 回复
dongpa5207 Fred,目前我的index.html里面没有php代码。它仍然需要重命名为.php才能与上面显示的文件正确通信?
大约 6 年之前 回复
doutangxi2144 请不要使用mysql_函数-它们已被弃用。请改用mysqli_或PDO函数。
大约 6 年之前 回复
douqiaoru2583 “当我从网站上的index.html点击此文件的链接时”-使用.php扩展名。
大约 6 年之前 回复


关于你的第一个问题,是的,这是正确的,因为它是服务器端而不是客户端,你只能查看你的php 页面如果代码是通过主机运行的。</ p>

我建议考虑使用XAMPP,这是一个允许您使用本地主机更快速测试代码的软件 ,这还附带一个免费的MySQL数据库(仅供本地使用,但仍然有助于测试)。 但无论如何,你可以查看有关如何设置XAMPP的youtube教程,它并不复杂,并且在测试代码时会节省大量时间,因为它就像编辑代码一样简单,点击保存 按钮,然后您可以立即在浏览器中查看更改。</ p>

此外,我想指出您应该将index.html重命名为index.php,否则您的浏览器赢了 '/ /可能无法识别您的PHP代码。</ p>

关于您的第二个问题,mysql_connect通常是一种较旧的连接数据库的方式,很多人可能建议您避免 它,但我认为以这种方式连接到数据库完全没有坏处,但是,对于这个问题,你应该能够在youtube上找到很多教程来帮助你设置数据库并使用php连接到它 脚本。</ p>

哦,还有最后一件事,为了摆脱你的疑虑,因为PHP是服务器端,这意味着如果有人要'查看源代码',它们就不会是 看看你的PHP代码是什么,他们会看到HTML和CSS代码(或任何其他客户端代码),以及你的数据库的用户名和密码是完全安全的。</ p>

< 编辑:我还建议您查看这本PHP手册,以获取该语言的一些基础知识: http://php.net/manual/en/index.php </ p>
</ div>



In regards to your first question, Yes that is correct, as it is server side and not client side, you can only view your php page if the code is running through a host.

I would recommend looking into getting XAMPP, which is a piece of software which allows you to use a local host to more quickly test out your code, this also comes with a free MySQL database (for local use only, but still serves a good purpose for testing). But anyway, you can look up on youtube tutorials on how to get XAMPP set up, it's not that complicated, and will save you tons of time when testing out your code, as it's then just as simple as editing your code, hitting the save button and then you can view the changes straight away in your browser.

Also I'd like to point out that you should rename your index.html to index.php, otherwise your browser won't/may not recognise your php code.

In regards to your second question, mysql_connect is generally an older way of connecting to the database, and a lot of people may advice you to avoid it, but I suppose it will do no harm at all to connect to a database this way, yet again, for this question you should be able to find many tutorials on youtube to help you set up your database and connect to it using a php script.

Oh and one last thing, to get rid of your doubts, as PHP is server side, it means that if someone was to 'view source code', they will not be able to see any of your PHP code, all they will see if the HTML and CSS code (or any other client side code), and therefore your username and passwords for your database are entirely safe.

EDIT: I'd also recommend looking at this PHP manual to pick up some of the basics of the language: http://php.net/manual/en/index.php

douzhanjia0773 没问题,我很高兴我可以对你有所帮助,如果你需要任何其他帮助然后让我知道,但是是的WAMP应该没问题,我从来没有实际使用过它,但我认为它基本上与 XAMPP。
大约 6 年之前 回复
doulandai0641 非常感谢Alex - 这真的让事情变得清晰起来。 几个星期前,当我第一次开始学习MYSQL时,我正在使用WAMP进行测试 - 这对于测试是否可行?
大约 6 年之前 回复
Csdn user default icon