//Establish connection by instantiated
$db = new mysqli(
$dbHost ,
$dbName,
$dbUsername,
$dbPassword
);
//Error
if ($db->connect_error){
die("Cannot connect to database: \n"
. $db->connect_error . "\n"
.$db->connect_errno
);
}
//Return the Object
return $db;
}
function fetchAll(mysqli $db){
//Return an array of data
$data = [];
$sql = 'SELECT * FROM city';
$results = $db->query($sql);
if (!$results) {
trigger_error('Invalid query: ' . $db->error);
}
//Loop
if ($results->num_rows > 0){
while($row = $results->fetch_assoc()){
$dat[] = $row;
}
}
return $data;
}
Please format the code you posted, by wrapping it in triple backticks. -> `
Обсуждают сегодня