doupaoshu8334 2014-08-11 17:55
浏览 67

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

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:

<?php
//Connect to database

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

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

?>

<html>
    <head>
    <Title> MySQL Table Connection </Title>
    </head>
    <body>  
    </body>

</html>

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.

  • 写回答

1条回答 默认 最新

  • doumo6356 2014-08-11 18:09
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试