gpt4 book ai didi

PHP 函数和 MySQL 连接

转载 作者:行者123 更新时间:2023-11-30 00:56:38 25 4
gpt4 key购买 nike

我有一个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);
}

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;
}

?>

这两个文件都位于/includes 文件夹内。我现在有一个 users.php 文件,我想在其中显示用户列表。这是我尝试执行此操作的代码:

<?php

$users = show_users();
foreach ($users as $key => $value) {
echo $key . " " . $value;
}

?>

我遇到的问题是这样的:

Notice: Undefined variable: connection in /Applications/MAMP/htdocs/blog/includes/functions.php on line 13

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /Applications/MAMP/htdocs/blog/includes/functions.php on line 13

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /Applications/MAMP/htdocs/blog/includes/functions.php on line 15

users.php 文件有 require('includes/functions.php') 和 require('includes/database.php')。但不知何故,这些值没有被传递?。我究竟做错了什么?请帮帮我。我希望这是有道理的。 3的每个函数都会出现 undefined variable 的问题。

最佳答案

$connection 变量传递给您的函数或将其设为全局变量。

例如:

function show_posts($connection, $user_id) {
$posts = array();
$sql = "SELECT body, stamp from posts where user_id = '$user_id' order by stamp desc";
$result = mysqli_query($connection, $sql);
}

或者

function show_posts($user_id) {
global $connection;

$posts = array();
$sql = "SELECT body, stamp from posts where user_id = '$user_id' order by stamp desc";
$result = mysqli_query($connection, $sql);
}

关于PHP 函数和 MySQL 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20494526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com