你好,我只想写自己的函数来从MySQL中获取选项值。我的index.php文件看起来是这样的:致命错误:调用空成员函数query()
<?
include ('config.php'); // here i have database connection details
include ('global-functions.php');
include ('minifier.php');
include ('cache-start.php');
?>
<!DOCTYPE html>
<html lang="jezyk strony z bazy danych">
USTAWIC META! sekcje head<br>
Sprawdzić czy dodac przekierowanie w php z www na bez www !!
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
(...) rest of the file
config.php文件的样子:
<?php
$host = "localhost";
$dbname = "xxxx";
$username = "xxx";
$password = "xxxx";
// Create connection
$conn = new mysqli($host, $username, $password, $dbname);
?>
现在我尝试做功能采取指定的选项,我把在全球的功能,此功能。 php:
<?php
// Get option
function get_option($option_name) {
$sql = "SELECT opcja_value FROM opcje WHERE opcja_name='" . $option_name . "'";
$result = mysqli_fetch_array($conn->query($sql));
return $result[$option_name];
}
?>
现在我要调用函数来获取我的选项的值。所以:
echo get_option('cache');
或做简单的,如果:
if (get_option('cache') == '1') {
// do stuff
} else {
echo 'option disabled, sorry :(';
}
通话功能后,我得到致命错误:
Fatal error: Call to a member function query() on null in /framework/includes/global-functions.php on line 10
哪里是'$ conn'创建,在函数中调用的时候吗? – JustOnUnderMillions
**范围**,___范围,范围。通过'$ conn'作为你函数的参数 – RiggsFolly
好吧,当我添加$ conn函数和调用它时,我没有看到错误,但它不起作用。函数不会从SQL返回任何值。 –