See below how to create a variable in PHP that we decided to call "link". It is the active MySQLi connection that we will use in our queries from within our PHP scripts:
$DBHost = '127.0.0.1:3306'; // local connection on port 3306 (if you have not changed the default port). You could specify a remote MySQL server too (server running on something other than your linux box).
# $DBHost = 'localhost'; // This is essentially the same thing and it will use the default port 3306 unless otherwise specified however I have found the proceeding to be supported on various different web server setups by default whereas "localhost" has to be configured in during server setup.
$DBUser = 'mysql username'; // The user must have the appropriate privileges assigned and assigned to the database below, best not to use a root account
$DBPass = 'mysql user password';
$DBName = 'database name';
$link = mysqli_connect($DBHost, $DBUser, $DBPass);
if (!$link)
{
// Do error hand off for: Unable to connect to the database server.
exit;
}
if (!mysqli_set_charset($link, 'utf8'))
{
// Do error hand off for: Unable to set database connection encoding.
exit;
}
if (!mysqli_select_db($link, $DBName))
{
// Do error hand off for: Unable to locate this users database.
exit;
}
// You could test some more stuff if you're paranoid but that is a solid connection.
Example Query:
$stmt = $link->prepare("
SELECT id, name
FROM users
WHERE email = ?
AND tel = ?
");
if (!$stmt)
{
// Do error hand off for: 'Error '.$link->errno.' : '.$link->error.'';
exit();
}
if (!$stmt->bind_param("si", $thisUsersPredefinedEmail, $thisUsersPredefinedTelephone))
{
// Do error hand off for: 'Error '.$link->errno.' : '.$link->error.'';
exit();
}
if (!$stmt->execute())
{
// Do error hand off for: 'Error '.$link->errno.' : '.$link->error.'';
exit();
}
$stmt->bind_result($userId, $userName);
$stmt->fetch();
$stmt->close();
Assuming that you are locally accessing the MySQL server via the lamp on your linux system (localhost) that would work for you too.
But really... Read the manual:
http://www.php.net/manual/en/book.mysqli.php