mysqli code example
Example 1: query php
<?php
$mysqli = new mysqli("localhost", "my_username", "my_password", "my_database");
if($mysqli->connect_error) {
echo "<b>Failed to connect to MySQL: </b>" . $mysqli->connect_error;
}
$selectQuery = $mysqli->prepare("SELECT * FROM my_table");
$selectQuery->execute();
$selectQueryResult = $selectQuery->get_result();
while($selectQueryRow = $selectQueryResult->fetch_array()) {
echo $selectQueryRow['my_column'];
}
$selectUsername = $mysqli->prepare("SELECT * FROM users");
$selectUsername->execute();
$selectUsernameResult = $selectUsername->get_result();
while($selectUsernameRow = $selectUsernameResult->fetch_array()) {
echo $selectUsernameRow['username'];
}
Example 2: mysqli database
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "test");
if (mysqli_connect_errno()) {
printf("Échec de la connexion : %s\n", mysqli_connect_error());
exit();
}
if ($result = $mysqli->query("SELECT DATABASE()")) {
$row = $result->fetch_row();
printf("La base de données courante est %s.\n", $row[0]);
$result->close();
}
$mysqli->select_db("world");
if ($result = $mysqli->query("SELECT DATABASE()")) {
$row = $result->fetch_row();
printf("La base de données courante est %s.\n", $row[0]);
$result->close();
}
$mysqli->close();
?>
Example 3: MYSQL
GRANT CREATE ON *.* TO 'testuser'@'localhost';