Για τον χρήστη "my_user" με κωδικό mysql "my_password" στην βάση δεδομένων 'testlar-db1' υπάρχουν δυο δυνατότητες σύνδεσης :
1. Κάνοντας χρήση της εντολής mysqli_connect
<?php
$link = mysqli_connect("ONOMA_DIAKOMISTI", "my_user", "my_password", "testlar-db1");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* Create table doesn't return a resultset */
if (mysqli_query($link, "CREATE TEMPORARY TABLE myCity LIKE City") === TRUE) {
printf("Table myCity successfully created.\n");
}
/* Select queries return a resultset */
if ($result = mysqli_query($link, "SELECT Name FROM City LIMIT 10")) {
printf("Select returned %d rows.\n", mysqli_num_rows($result));
/* free result set */
mysqli_free_result($result);
}
/* If we have to retrieve large amount of data we use MYSQLI_USE_RESULT */
if ($result = mysqli_query($link, "SELECT * FROM City", MYSQLI_USE_RESULT)) {
/* Note, that we can't execute any functions which interact with the
server until result set was closed. All calls will return an
'out of sync' error */
if (!mysqli_query($link, "SET @a:='this will not work'")) {
printf("Error: %s\n", mysqli_error($link));
}
mysqli_free_result($result);
}
2. Κάνοντας χρήση του PDO Driver για MySQL:
<?php /*** MySQL διακομιστής ***/ $hostname = "ONOMA_DIAKOMISTI"; /*** MySQL όνομα χρήστη ***/ $username = "my_user"; /*** MySQl κωδικός ***/ $password = "my_password"; /*** Όνομα βάσης δεδομένων ***/ $dbname = "testlar-db1"; try { $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password); /*** Μήνυμα για επιτυχής σύνδεση ***/ echo "Η σύνδεση ήταν επιτυχής"; } catch(PDOException $e) { echo $e->getMessage(); } ?>