开发者

PDO: If my host ist localhost:80, what do I have to put in here?

I got this example from开发者_StackOverflow the php site:

<?php
try {
    $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
    foreach($dbh->query('SELECT * from FOO') as $row) {
        print_r($row);
    }
    $dbh = null;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
?>

I use MAMP on the mac, and when I call my local site it looks like this:

http://localhost:80/mysite/index.php

so what exactly would I have to write in

$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);

for host= ? would that be localhost, or would that be localhost:80? or something totally different? My MySQL DB has the port number 3306. Is that relevant? Or is just "localhost" perfectly fine?


What you have now should be fine, assuming MySQL is running on the same machine.

Port 80 is what your web server (apache) is running on. MySQL will be running on a different port. If your MySQL server is running on the default port then you probably don't need to put the port number in, and what you have will work fine. If you installed MySQL to run on a different port though, then you would probably have to put in the port number.

Also, if you have apache running on port 80, you don't even have to put the ":80" in the URL for your site. Since port 80 is the default port for HTTP, the browser will use that automatically.


MySQL server and HTTP server are separate programs running on your machine.

The 80 port in the URL belongs to HTTP server while MySQL server, on the other hand, takes another port, usually 3306.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜