旧MYSQL查询到MYSQLI查询函数

时间:2014-09-30 18:10:29

标签: php mysql mysqli

我有很多需要更改为MYSQLI的函数。

有人可以帮忙进行适当的重新编码吗?修复此问题将使此问题能够更改其余功能。谢谢。

错误以**开头,以**结束,后面跟着问题行。

connection.php

<?php

mysqli_connect('localhost', 'root', '', 'info') or die($connect_error);


 ?>

general.php

<?php

function sanitize($data) {
    mysqli_real_escape_string($connect,$data);
    }

function protect_page() {
    if (logged_in() === false) {
    header('Location:../../redirect.php');  
    }   
}

function logged_in_redirect() {
    if (logged_in() === true) {
    header('Location: ../../dashboard.php');
    exit();
    }
}

?>

users.php

  <?php

function user_exists($username) {
    $username = sanitize($username);
    $query = mysqli_query($connect,"SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'");
    mysqli_num_rows($query), 0) == 1) ? true : false;
}

function logged_in() {
    return (isset($_SESSION['id'])) ? true : false;
}

?>

    mysqli_connect('localhost', 'root', '', 'info') or die($connect_error);


     ?>

login.php ...如果用户存在,我试图回应的地方,应该是让用户退回&#39;偷走&#39;但事实并非如此。顺便说一下,表格还不错,已经检查过了。

<?php
    include 'core/init.php';

if (user_exists('stole') === false) {
    echo 'exists';
}
die ();


if (empty($_POST) === false) {
    $username = $_POST['username'];
    $password = $_POST['password'];

    if(empty($username) === true || empty($password) === true) {
        $errors [] = 'Please enter a valid username and password';
    } else if (user_exists($username) === false) {
        $errors [] = 'We can\'t find that username. Have you registered? '; 


    }
}    

?>

的init.php

1 个答案:

答案 0 :(得分:2)

您的脚本中存在一些问题:

  1. $connect_error未定义。 (由@ Fred-ii相关 - )

  2. 您正在使用会话$_SESSION['id'],但没有使用session_start();的迹象。在使用会话的所有文件中都需要它。 (由@ Fred-ii相关 - )

  3. 您没有将资源添加到全局变量,您创建的方法永远无法访问mysqli_connect('localhost', 'root', '', 'info')

  4. 了解变量范围:

    http://php.net/manual/en/language.variables.scope.php

    阅读会议:

    试试这个:

    <?php
    
    $connect = mysqli_connect('localhost', 'root', '', 'info') or die($connect_error);
    
    
     ?>
    

    general.php

    <?php
    
    function sanitize($data) {
        global $connect;
        mysqli_real_escape_string($connect,$data);
        }
    
    function protect_page() {
        if (logged_in() === false) {
        header('Location:../../redirect.php');  
        }   
    }
    
    function logged_in_redirect() {
        if (logged_in() === true) {
        header('Location: ../../dashboard.php');
        exit();
        }
    }
    
    ?>
    

    users.php

      <?php
    
        $connect = mysqli_connect('localhost', 'root', '', 'info') or die($connect_error);
    
    function user_exists($username) {
        global $connect;
        $username = sanitize($username);
        $query = mysqli_query($connect,"SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'");
        mysqli_num_rows($query), 0) == 1) ? true : false;
    }
    
    function logged_in() {
        return (isset($_SESSION['id'])) ? true : false;
    }
    ?>