【问题标题】:Variable $connection is undefined. Impossible, what is happening there?变量 $connection 未定义。不可能,那里发生了什么?
【发布时间】:2015-11-01 13:00:56
【问题描述】:

目标:我正在创建一个php登录脚本。

问题:我似乎无法让我的包含识别 $connection 变量,即使它应该在 connection.php 中明确定义。结果我的变量的值是空的/NULL。

我尝试了什么:我从 mysql 开始,但很快发现这是错误的方法,并将我的代码转换为 mysqli。我检查了我拥有的所有 $connection 变量中的拼写错误。我确保路径是正确的。作为最后的手段,我进行了谷歌搜索,但没有找到答案或任何有用的提示。

问题:我的变量没有被定义的原因是什么?

错误信息:

所有这些消息都与这个单一变量有关,由于某种原因没有被定义:

Php 注意:未定义变量:C:\xampp\htdocs\aspie\Php\Core\Common.php 中的连接第 4 行

Php 警告:mysqli_real_escape_string() 期望参数 1 为 mysqli,在第 4 行的 C:\xampp\htdocs\aspie\Php\Core\Common.php 中给出 null

Php 注意:未定义变量:C:\xampp\htdocs\aspie\Php\Core\Functions\Members.php 中的连接第 4 行

Php 警告:mysqli_query() 期望参数 1 为 mysqli,在第 4 行的 C:\xampp\htdocs\aspie\Php\Core\Functions\Members.php 中给出 null

Php 警告:mysqli_num_rows() 期望参数 1 为 mysqli_result,在第 5 行的 C:\xampp\htdocs\aspie\Php\Core\Functions\Members.php 中给出 null

// 初始化器

<?php
session_start();
// error_reporting(NULL);

include 'Connection.php';
include 'Common.php';
include 'Functions/Members.php';

?>

**// 错误消息**

<?php
$connection_error = 'Our website is experiencing technical issues, please come back later.';
$wrong_login = 'Password and name are wrong.';
$member_registered = 'Access has been denied. You do not seem to be a registered user.';
?>

**// 连接**

<?php
include 'Errors.php';

$connection = mysqli_connect('localhost', 'root', '', 'project') or exit ($connection_error);
?>

**// 普通**

<?php
error_reporting();
function sanitize($connection, $data) {
    global $connection;
    global $data;
    return mysqli_real_escape_string($connection, $data);
}
?>

**// 成员**

<?php
function member_registered($connection, $name) {
    $name = sanitize($connection, $name);
    $query = mysqli_query($connection, "SELECT COUNT(`id`) FROM `members` WHERE `name` = '$name'");
    return (mysqli_num_rows($query) == 1) ? true : false;
}

?>

**// 登录 **

<?php
include 'Php/Core/Initializer.php';

    if (member_registered($connection, 'ee')) {
        echo "exists";
    }
    die("eee");
    echo error_reporting();
if (empty($_POST) == false) {
    $name = $_POST['name'];
    $password = $_POST['name'];

    if (empty($name) OR empty($password)) {
        echo $wrong_login;
    }

    else if (member_registered($connection, $name) == false) {
        echo $member_registered;
    }
}
?>

更新:

现在我得到的是:existseee;

即使设置正确,条件现在也不起作用。用户名不存在,所以它不应该回显“存在:

if (member_registered($connection, 'ee')) {
    echo "exists";
}
die("eee");

【问题讨论】:

  • 变量已定义,但在函数范围内看不到
  • $connection 在函数范围内不可用。将其作为参数传递function member_registered($connection,$name) {,然后将连接添加到调用if (member_registered($connection,'ee')) {
  • 函数中的所有变量都在不同的范围内。您需要将变量作为参数传递或在函数中将其设置为全局function ...() { global $connection; [...more code]

标签: php


【解决方案1】:

在您的情况下,您无法访问 sanitize 函数中的 $connection 变量。

尝试将$connection 变量传递到您的函数中,如下所示:

function sanitize($data, $connection) {
    return mysqli_real_escape_string($connection, $data);
}

在此处了解更多信息:http://php.net/manual/en/language.variables.scope.php

更新

在 **// MEMBERS ** 中,您还需要包含新参数:

function member_registered($name, $connection) {
    $name = sanitize($name, $connection);
    $query = mysqli_query($connection, "SELECT COUNT(`id`) FROM `members` WHERE `name` = '$name'");
    return (mysqli_num_rows($query) == 1) ? true : false;
}

因此,您的 **// LOGIN ** 也应该更新:

include 'Php/Core/Initializer.php';

    if (member_registered('ee', $connection)) {
        echo "exists";
    }
    die("eee");
    echo error_reporting();
if (empty($_POST) == false) {
    $name = $_POST['name'];
    $password = $_POST['name'];

    if (empty($name) OR empty($password)) {
        echo $wrong_login;
    }

    else if (member_registered($name, $connection) == false) {
        echo $member_registered;
    }
}

并确保$connection 变量在您需要的任何地方都可以访问。

更新 #2

回答您的更新:

应该是这样的:

if (member_registered('ee', $connection)) {
    echo "exists";
}

不是这样的:

if (member_registered($connection, 'ee')) {
    echo "exists";
}

$connection 是第二个参数。

【讨论】:

  • 我刚刚更新了我的代码,但仍然出现错误。当我编辑我的问题时,你可以看到我的更新
  • 我用你的修复更新了我的代码。问题是我的条件现在不起作用,它检查用户是否存在。您可以在我编辑的问题中的“更新”下阅读它。不过,一切都设置正确。
  • 请检查我的更新#2
  • 我改变了,因为 mysqli_query() 期望参数 1 是 mysqli
  • 这不应该影响mysqli_query() 方法的参数。如果要更改member_registered 方法的参数顺序,则需要在使用member_registered() 的所有位置进行更改。
猜你喜欢
  • 2014-04-30
  • 2012-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 2011-02-23
相关资源
最近更新 更多