mySQL和Php网站之间的连接如何工作?

mySQL和Php网站之间的连接如何工作?

问题描述:

I have successfully setup a localhost (Individual Installs) with Apache 2.2, MySQL and PHP, as well as setup a couple virtual-hosts. Currently, I am trying to teach myself command line in MySQL. The part I am lost on is setting up the connection between the database that I create and the virtual-host website I am working on. In my mind I would want to save the database for a specific website in the same folder that I am creating the website. However, I could be way off base. Could some body explain how this piece of the puzzle and how it works. I tried to Google it and all I get is information regarding Work Bench and phpMyAdmin.

我已成功设置了Apache 2.2,MySQL和PHP的localhost(个人安装),并设置了一对 虚拟主机。 目前,我正在尝试自学MySQL中的命令行。 我迷失的部分是在我创建的数据库和我正在处理的虚拟主机网站之间建立连接。 在我看来,我想在我创建网站的同一文件夹中保存特定网站的数据库。 但是,我可能会离开基地。 有些人可以解释一下这个难题是如何以及如何运作的。 我试图谷歌它,我得到的是有关Work Bench和phpMyAdmin的信息。 p> div>

The MySQL server runs as a service (or daemon) on your server - so the folder (or directory) you put it in is set by the system configuration and has nothing to do with your document root for your web site.

In PHP, you will "connect" to the MySQL service using PDO or something similar and then send requests to the MySQL server using PHP commands.

When you are running the MySQL command line interpreter, you can also test those same commands by typing them yourself. This is helpful when you're debugging your PHP program so you can see the data that your program will get yourself in the command line interface.

I hope this helps put you in the right picture.

I tried to Google it and all I get is information regarding Work Bench and myPhpAdmin

this is the first entry on the first page when I google php mysql, it covers everything from installation up to code example.

Very generally:

You need to check with your hosting company if they offer you locally hosted MySQL server access. Using cpanel/phpMyAdmin, or whatever tools they have available for you, you can then login and create multiple databases as you please - each database maybe corresponding to one of your site folders. Once you have created your account and your databases, you can use php commands from your website code to connect to/manipulate whichever database you want.

EDIT: I missed where you said you already have a MySQL localhost setup. Now you need to login to your SQL server and create one or more databases and one or more user accounts for those databases. Create an admin account with all the privileges. Then you can use php in your webpage to connect to a database, using something like this:

$con = mysql_connect("localhost", "your_MySQL_username", "your_MySQL_password") or die ("Unable to connect to MySQL Server " . mysql_error()); 
if(is_resource($con))
    $db = mysql_select_db($MySQL_database, $con) or die( "Unable to select database " . mysql_error());

See the manual for more ways to query the database and handle the results: http://php.net/manual/en/ref.mysql.php