special thanks to Jan Michelfeit & Martin Kruliš
stud_yourlogin
$connection = mysqli_connect($host, $user, $password, $database);
if (!$connection) { /* handle error */ }
// ... work with connection
mysqli_close($connection); // optional; only after you are done with database access
$mysqli = new mysqli($host, $user, $password, $database);
if ($mysqli->connect_error) { /* handle error */ }
// ... work with connection
$mysqli->close();
$dsn = "mysql:host=$host;dbname=$database";
try {
$pdo = new PDO($dsn, $username, $password);
} catch (PDOException $e) {
/* handle error */
});
mysql_
* functions
$query = "SELECT * FROM MyTable";
if ($result = mysqli_query($connection, $query)) { // $result contains mysqli_result object representing the result set or FALSE
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
// access $row["Column1"], $row["Column2"], ...
}
}
$query = "SELECT * FROM MyTable";
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
// access $row["Column1"], $row["Column2"], ...
}
}
// Do not use:
$query = 'SELECT * FROM products WHERE title = "'. $_GET['search'] . '"';
$query_result = mysqli_query($conn, $query);
$searchStr = $_GET['search'];
// Notice quotes around title
$query = 'SELECT * FROM products WHERE title = "'
. mysqli_real_escape_string($conn, $searchStr)
. '"';
$query_result = mysqli_query($conn, $query);
// ! Notice no quotes around ?
$stmt = $mysqli->prepare("INSERT INTO myTable VALUES (?, ?)");
// ! do not use literals, use variables for $aStringVar, $aDoubleVar
$stmt->bind_param('sd', $aStringVar, $aDoubleVar);
$aStringVar = '...'
$aDoubleVar = 0;
$stmt->execute();
$query_result = $stmt->get_result();
ADD-A-NAME BOARD