php没有从数据库中获取图像位置

时间:2014-02-04 08:21:39

标签: php mysql mysqli

在改变几个方法的过程中很快就会在php函数/方法中弃用并且遇到一点麻烦。

当我使用mysql_query

时,此代码有效
function user_id_from_email($email)
{
    $email = sanitize($email);
    return mysql_result(mysql_query("SELECT `user_id` FROM `users` WHERE `email` = '$email'"), 0, 'user_id');   
}

function login($email, $passwd)
{
    $user_id = user_id_from_email($email);

    $email = sanitize($email);
    $passwd = sha1($passwd);

    return (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `email` = '$email' AND `passwd` = '$passwd'"), 0) == 1) ? $user_id : false;

}

这是重写的代码

function user_id_from_email($email)
{
    $email = sanitize($email);
    $db = new mysqli('127.0.0.1','admin','sompass','login');
        if($db->connect_errno){
            $connect_error = 'Sorry, we are experiencing connection problems.'; 
            die ($connect_error);
        }
    $query = "SELECT `user_id` FROM `users` WHERE `email` = '$email'";
    if($result = mysqli_query($db, $query)){
        while ($email = mysqli_fetch_array($result)){
            return $email;
        }
    }
}

function login($email, $passwd)
{
    $user_id = user_id_from_email($email);

    $email = sanitize($email);
    $passwd = sha1($passwd);

    $db = new mysqli('127.0.0.1','admin','sompass','login');
        if($db->connect_errno){
            $connect_error = 'Sorry, we are experiencing connection problems.'; 
            die ($connect_error);
        }
    $query = "SELECT `user_id` FROM `users` WHERE `email` = '$email' AND `passwd` = '$passwd'";
    if($result = mysqli_query($db, $query)){
        while ($data = mysqli_fetch_array($email, $passwd)){
            return $data;
        }
    }
}
很明显,我无法正确转换查询,并且在返回结果时无法找到维护方法的user_id属性的正确语法。另外,请让我预测,在分析代码时我会显得缺乏经验,因为我是:)。只是一个抬头。使用修改后的方法我没有收到任何错误信息,我有--- ini_set('display_errors','On'); error_reporting(E_ALL); ---在我的init文件的顶部。我遇到的问题是图像现在不上传或其路径不再正确定向到其位置。先感谢您。

我使用http://us2.php.net/manual/en/mysqli-result.fetch-assoc.php

的参考资料

1 个答案:

答案 0 :(得分:0)

问题在这里

 while ($data = mysqli_fetch_array($email, $passwd)){
        return $data;
    }

你需要将$ result传递给mysqli_fetch_array - 你应该得到一个错误。另外我建议你习惯使用面向对象的样式调用,并删除while循环。像...

$query = "SELECT `user_id` 
              FROM `users`
               WHERE `email` = '$email' AND `passwd` = '$passwd'";
$result = $db->query($query);
return $result->num_rows > 0 ? $user_id : false;