Php 对成员函数query()的调用

Php 对成员函数query()的调用,php,mysqli,Php,Mysqli,我正忙于编写投票脚本,但当我想投票时遇到问题,我收到一个错误: 致命错误:对第7行中的非对象调用成员函数查询 <?php include '../config.php'; function getAllVotes($id) { $votes = array(); if($q_13 = "SELECT * FROM post WHERE id = $id"); if($r_26 = $db->query($q_13)); ?> 这是我的config.ph

我正忙于编写投票脚本,但当我想投票时遇到问题,我收到一个错误:

致命错误:对第7行中的非对象调用成员函数查询

<?php
include '../config.php';
function getAllVotes($id)
{
    $votes = array();
    if($q_13 = "SELECT * FROM post WHERE id = $id");
    if($r_26 = $db->query($q_13));
?>
这是我的config.php

<?php

$db = new mysqli('localhost', 'root', 'mysql', 'me');

if($db->connect_errno) {
    die('Sorry, we are having some problems.');
}

?>

这是一个范围问题,您需要将$db作为参数传递给getAllVotes

像这样

function getAllVotes(mysqli $db, $id) //<---- Pass $db here
{

@hakre可能重复谢谢,我还有这个函数:函数getEffectiveVotesmysqli$db,$id,并收到这个错误:解析错误:语法错误,意外的T_变量,这是一行:$effectiveVote=getEffectiveVotesmysqli$db,$id;调用函数时,只传递变量,不要在前面添加mysqli。它仅用于函数定义,不用于函数调用-$effectiveVote=getEffectiveVotes$db,$id;。看:@hakre谢谢你,它起作用了!这对我来说是新的,谢谢你让我学到了新的东西!祝你今天愉快,不客气。学习这些工具并明智地使用它们。如果您不了解某个细节,请在PHP手册中找到它。它通常很有见地,如果你找不到它,问问它。