Search the Community
Showing results for tags 'connectivity'.
-
I am trying to create a script (from a tutorial I try to adapt to my own needs) and I am running into a few problems. Here I go... I have an /includes folder which contains the following: database.php and functions.php with the following content: database.php <?php // Database connectivity stuff $host = "localhost"; // Hostname for the database. Usually localhost $username = "root"; // Username used to connect to the database $password = "root"; // Password for the username used to connect to the database $database = "blog"; // The database used // Connect to the database using mysqli_connect $connection = mysqli_connect($host, $username, $password, $database); // Check the connection for errors if (mysqli_connect_errno($connection)) { // Stop the whole page from loading if errors occur die("<br />Could not connect to the database. Please check the settings and try again.") . mysqli_connect_error() . mysqli_connect_errno(); } ?> functions.php <?php // Functions file for the system function show_posts($user_id) { $posts = array(); $sql = "SELECT body, stamp from posts where user_id = '$user_id' order by stamp desc"; $result = mysqli_query($connection, $sql); while ($data = mysqli_fetch_assoc($result)) { $posts = array( 'stamp' => $data->stamp, 'user_id' => $user_id, 'body' => $data->body ); } return $posts; } function show_users() { $users = array(); $sql = "SELECT id, username FROM users WHERE status = 'active' ORDER BY username"; $result = mysqli_query($connection, $sql); while ($data = mysqli_fetch_array($result)) { $users[$data->id] = $data->username; } return $users; } function following($user_id) { $users = array(); $sql = "SELECT DISTINCT user_id FROM following WHERE follower_id = $user_id"; $result = mysqli_query($connection, $sql); while ($data = mysqli_fetch_assoc($result)) { array_push($users, $data->user_id); } return $users; } ?> And here's the code that I run to display the users in users.php <?php $users = show_users(); foreach ($users as $key => $value) { echo $key . " " . $value; } ?> That throws me the following errors: Notice: Undefined variable: connection in /Applications/MAMP/htdocs/blog/includes/functions.php on line 22 Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /Applications/MAMP/htdocs/blog/includes/functions.php on line 22 Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /Applications/MAMP/htdocs/blog/includes/functions.php on line 24 I've tried to pass the $connection value to the show_users(); function with no results... any help would be appreciated. I am able to display the users from the database if I introduce the following code in users.php, but I want to try and keep things as clear as possible, without cluttering the files unnecessary. <?php $users = array(); $query = "SELECT id, username FROM users WHERE status = 'active' ORDER BY username"; $result = mysqli_query($connection, $query); if ($result) { while ($member = mysqli_fetch_assoc($result)) { echo strip_tags($member['username']) . "<br />"; } } else { echo "There are no posts to display. Why not add a new one?"; } mysqli_free_result($result); mysqli_close($connection); ?> PS: The users.php file also has require('includes/database.php'); and require('includes/functions.php'). I guess I'm trying to recreate the last bit of code into a working function... and I can't seem to do it. Any help is appreciated. I hope it makes an sense... -- Andrei