简体   繁体   中英

How do I connect to an online MySQL database using PHP?

I am testing a website offline with XAMPP. My PHP code connects and works with my local MySQL database (ie 'localhost' or '127.0.0.1'). I wish to conduct tests using an online MySQL database. I am trying to use PHPMyAdmin Demo Server but I get an error when trying to connect to the server. I changed the 'DB_HOST' to '192.168.30.23' (which seems to be the demo server ip address). Am I missing something?

Here is a snippet of my code:

<?php

define('DB_NAME', 'ears');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', '192.168.30.23');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$link) {
die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db(DB_NAME, $link);

if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}

?>

Thank you in advance.

The phpMyAdmin MySQL demo server is not available to test your script. Google for "public access MySQL server" to maybe find one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM