首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误: mysqli_query、mysqli、mysqli_fetch_assoc、mysqli_result、mysqli_num_rows和imo未定义变量

错误: mysqli_query、mysqli、mysqli_fetch_assoc、mysqli_result、mysqli_num_rows和imo未定义变量
EN

Stack Overflow用户
提问于 2014-02-11 12:19:09
回答 2查看 983关注 0票数 0

你好,我试着修复我的网站,我用来尝试的东西,并更好地学习php在我的业余时间。所以我的主机改变了,所以现在我不得不使用mysqli,而不是mysql,我有很多问题,所以我想我应该用它。

提前感谢您的帮助!

错误:

  • 注意:未定义变量:第11行functions.php中的con
  • 警告: mysqli_query()期望参数1为mysqli,在第11行的functions.php中为null
  • 警告: mysqli_fetch_assoc()期望参数1为mysqli_result,在第13行的functions.php中为null
  • 注意:未定义变量:第41行functions.php中的con
  • 警告: mysqli_query()期望参数1为mysqli,在第41行的functions.php中为null
  • 警告: mysqli_num_rows()期望参数1为mysqli_result,在第41行的functions.php中为null

profile.php:

代码语言:javascript
复制
<?php

require("functions.php");
$profileUsersData = getUsersData(mysqli_real_escape_string($con, $_GET['id']));

if(userExists(mysqli_real_escape_string($con, $_GET['id'])))
{
echo '<div class="page-header"><h2>'.$profileUsersData['firstname']." ".$profileUsersData['lastname']."'s Profile</h2></div>";
?>
<div class="row" style="margin:0 auto;">
    <div class="col-lg-6">
        <div class="well">
            <?php echo '<font size=4>About '.$profileUsersData['firstname'].':</font>'; ?>
            <br />
            <?php echo $profileUsersData['about']; ?>
            <br />
            <br />
            Country: <?php echo $profileUsersData['country']; ?>
            <br />
            City: <?php echo $profileUsersData['city']; ?>
            <br />
            Age:
            <?php
                $difference = time() - $profileUsersData['birthdate'];
                $age = floor($difference / 31556926);
                echo $age.'<font class="text-danger"> Still not working correctly</font>';
            ?>
            <br /><p></p>
            <?php
            if($profileUsersData['username']==$_SESSION['username'])
            {
                echo '<a href="index.php?page=editprofile"><button type="button" class="btn btn-success">Edit profile</button></a>';
            }
            ?>
        </div>
    </div>
</div>
<?php 
}
else echo '<div class="page-header"><h2>Profile</h2></div><p><font class="text-danger"><h1>Invalid ID: '.h($_GET['id']).'</h1></font></p><br /><br /><br /><br />';
?>

dbc.php

代码语言:javascript
复制
<?php
$con = new mysqli("host","user","password","db");
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
?>

functions.php:

代码语言:javascript
复制
<?php

require("dbc.php");

if (!function_exists('getUsersData')) {
    function getUsersData($id) {
        $array = array();
        $q = mysqli_query($con, "SELECT * FROM `users` WHERE `id`=".$id);

        while($r = mysqli_fetch_assoc($q)) {
            $array['id'] = $r['id'];
            $array['username'] = $r['username'];
            $array['password'] = $r['password'];
            $array['firstname'] = $r['firstname'];
            $array['lastname'] = $r['lastname'];
            $array['profileext'] = $r['profileext'];
            $array['about'] = $r['about'];
            $array['birthdate'] = $r['birthdate'];
            $array['country'] = $r['country'];
            $array['city'] = $r['city'];
        }
        return $array;
    }
}
if (!function_exists('getId')) {
    function getId($username)
    {
        $q = mysqli_query($con, "SELECT `id` FROM `users` WHERE `username`='".$username."'");
        while($r = mysqli_fetch_assoc($q))
        {
            return $r['id'];
        }
    }
}
if (!function_exists('userExists')) {
    function userExists($id)
    {
        $numrows = mysqli_num_rows(mysqli_query($con, "SELECT `id` FROM `users` WHERE `id`=".$id));
        if($numrows==1)
            return true;
        else
            return false;
    }
}
if (!function_exists('form_row_class')) {
    function form_row_class($name) {
        global $errors;
        return $errors[$name] ? " has-error" : "";
    }
}
if (!function_exists('error_for')) {
    function error_for($name) {
        global $errors;
        if($errors[$name]) {
            return "<div class='text-danger'>" . $errors[$name] . "</div>";
        }
    }
}
if (!function_exists('h')) {
    function h($string){
        return htmlspecialchars($string);
    }
}
if (!function_exists('updateProfile')) {
    function updateProfile($id,$firstName,$lastName,$about,$country,$city)
    {
        if(mysqli_query($con, "UPDATE `users` SET `firstname`='".$firstName."', `lastname`='".$lastName."', `about`='".$about."', `birthdate`=".$birthdate.", `country`='".$country."', `city`='".$city."' WHERE `id`=".$id))
            return true;
        else
            return false;
    }
}
?>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-02-11 12:29:49

你可以试着

代码语言:javascript
复制
global $con;

在“函数getUsersData($id) {”之后

票数 1
EN

Stack Overflow用户

发布于 2014-02-11 12:28:47

在您的functions.php中,$con没有定义,因此它是一个空值。您正在从父文件中使用$con,因此必须编写parent::$con

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21701673

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档