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条)

报告相同问题?

悬赏问题

  • ¥20 怎么在stm32门禁成品上增加记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 解riccati方程组