Du kan prøve at bruge enten MySQLi
eller PDO
og deres forberedte erklæring for at være mere sikker.
Hvis du har til hensigt kun at bruge MySQL, så brug nedenstående kode til at initialisere din databaseforbindelse.
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
For reference se http://php.net/manual/en/function. mysql-connect.php
Alternativt kan du bruge MySQLi
<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
For reference se http://php.net/manual/en/mysqli.construct. php
Hvis du overvejer at bruge PDO, så prøv
<?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();
}
?>
For reference se http://php.net/manual/en/pdo.connections. php