你好,我只想寫自己的函數來從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返回任何值。 –