mysqli错误 - 杀了我

时间:2015-09-13 09:48:16

标签: php mysql mysqli

这几行代码给了我x错误,我正在为自己的问题而自杀。有新鲜眼睛的人可以看着它并指出我回答。

错误:mysqli_query()期望参数1为mysqli。

有错误的行:3,4,9,10

function mysqli_query_excute( $sql )
{
    global $link;

    $result = mysqli_query( $link, $sql );

    if(  ! $result )
    {
        die( printf( "Error: %s\n", mysqli_error( $link ) ) );
    }

    return $result->fetch_object();
}

这就是整个事情:

<?php

    global $link;
    $link = mysqli_connect( "localhost", "xxx", "xxx", "xxx" )
    or die("Couldnt connect");


function mysqli_query_excute( $sql )
{
    global $link;

    $result = mysqli_query( $link, $sql );

    if(  ! $result )
    {
        die( printf( "Error: %s\n", mysqli_error( $link ) ) );
    }

    return $result->fetch_object();
}

    /*
    * get the user data from database by email and password
    **/
    function get_user_by_email_and_password( $email, $password )
    {
        return mysqli_query_excute( "SELECT * FROM users WHERE email = '$email' AND password = '$password'" );
    }

    /*
    * get the user data from database by provider name and provider user id
    **/
    function get_user_by_provider_and_id( $provider_name, $provider_user_id )
    {
        return mysqli_query_excute( "SELECT * FROM users WHERE hybridauth_provider_name = '$provider_name' AND hybridauth_provider_uid = '$provider_user_id'" );
    }

    /*
    * get the user data from database by provider name and provider user id
    **/
    function create_new_hybridauth_user( $email, $first_name, $last_name, $provider_name, $provider_user_id )
    {
        // let generate a random password for the user
        $password = md5( str_shuffle( "0123456789abcdefghijklmnoABCDEFGHIJ" ) );

        mysqli_query_excute(
            "INSERT INTO users
            (
                email,
                password,
                first_name,
                last_name,
                hybridauth_provider_name,
                hybridauth_provider_uid,
                created_at
            )
            VALUES
            (
                '$email',
                '$password',
                '$first_name',
                '$last_name',
                $provider_name,
                $provider_user_id,
                NOW()
            )"
        );
    }
?>

1 个答案:

答案 0 :(得分:-1)

这个错误也让我很伤心。用清晰的英语说它$ link不是mysqli实例,应该如此。

所以,你必须先连接到mysqli。