duan010167787 2014-02-11 12:19
浏览 44
已采纳

错误:mysqli_query,mysqli,mysqli_fetch_assoc,mysqli_result,mysqli_num_rows和imo wierd undefined变量

Hello im trying to repair my site that I use to try out stuff and learn php better in my spare time. So ye my host changed so now I have to use mysqli instead of mysql and im having alot more problems then I think I should have with it.

Thanks in advance for any help!

Errors:

  • Notice: Undefined variable: con in functions.php on line 11
  • Warning: mysqli_query() expects parameter 1 to be mysqli, null given in functions.php on line 11
  • Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in functions.php on line 13
  • Notice: Undefined variable: con in functions.php on line 41
  • Warning: mysqli_query() expects parameter 1 to be mysqli, null given in functions.php on line 41
  • Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in functions.php on line 41

profile.php:

<?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

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

functions.php:

<?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;
    }
}
?>
  • 写回答

2条回答 默认 最新

  • dongyu2047 2014-02-11 12:29
    关注

    You could try to put

    global $con;
    

    after the "function getUsersData($id) {" codepart

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题