简体   繁体   中英

Connect to an external Mysql database

I've a problem connecting on my free database. I created a free mysql database on freesqldatabase.com in order to try if my php script does his job. The main purpose is to insert data from an html form (hosted in a different host let's say HOST A) into a table in the database (in HOST B). I tried many times but it says that could not connect on database.

Here's the error:

Warning: mysqli_connect(): (HY000/2003): 
Can't connect to MySQL server on 'sql2.freesqldatabase.com' (110) 
in [HOST A]/core/try.php on line 6 ERROR: Could not connect. 
Can't connect to MySQL server on 'sql2.freesqldatabase.com' (110)

So here's my PHP script:

    <?php

$link = mysqli_connect("sql2.freesqldatabase.com", "user", "password", "database_name");

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Attempt insert query execution
$sql = "INSERT INTO persons (person_id, first_name, last_name, email_address) VALUES (1, 'Peter', 'Parker', 'peterparker@mail.com')";
if(mysqli_query($link, $sql)){
    echo "Records added successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

// Close connection
mysqli_close($link);
?>

And this is my simple html form:

<form action="core/try.php" method="post" />

<p>Nome persona: <input type="text" name="person_id" /></p>
<p>nome <input type="text" name="first_name" /></p>

<p>cognome <input type="text" name="last_name" /></p>
<p>email <input type="text" name="email_address" /></p>

    <input type="submit" value="Submit" />

</form>

What have I done wrong?!

From the connection settings and code that you've provided it seems that the table persons does not exist or is missing. I can only see the table tabella . I think you have to create the table persons first. Connecting to the database itself with the code that you've provided works. This is the error that I'm getting:

ERROR: Could not able to execute INSERT INTO persons 
(person_id, first_name, last_name, email_address) 
VALUES (1, 'Peter', 'Parker', 'peterparker@mail.com'). 
Table 'sql2107307.persons' doesn't exist

您没有使用创建 sqldatabase 时提供的密码和用户名,请从您的电子邮件中查看详细信息

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