Hi. I'm trying to create a function which connects to a database, and then displays rows in it. I'm trying to use a function to connect, but it's not working. Here's my first code:
<?php
define("SERVER", "mysql.example.com");
define("USERNAME", "username");
define("PASSWORD", "********");
define("DATABASE", "db");
function displayFunction() {
$dbc = @mysqli_connect(SERVER, USERNAME, PASSWORD, DATABASE);
$query = "SELECT * FROM table";
$result = @mysqli_query($dbc, $query);
while($row = @mysqli_fetch_array($result)) {
return $row['row'];
}
}
echo displayFunction();
?>
That works. However this code:
<?php
define("SERVER", "mysql.example.com");
define("USERNAME", "username");
define("PASSWORD", "********");
define("DATABASE", "db");
function connect() {
$dbc = @mysqli_connect(SERVER, USERNAME, PASSWORD, DATABASE);
if(!$dbc) {
return false;
}
else {
return true;
}
}
function displayFunction() {
if(!connect()) {
return false;
}
else {
$query = "SELECT * FROM table";
$result = @mysqli_query($dbc, $query);
while($row = @mysqli_fetch_array($result)) {
return $row['row'];
}
}
}
echo displayFunction();
?>
doesn't work.
Can anyone help me?
Thanks