mysqli_query()期望参数1为mysqli,null中给出null

时间:2013-10-01 20:38:33

标签: php mysqli

我的错误消息

  

警告:mysqli_query()期望参数1为mysqli,null给定   在第30行的/home/orikina/public_html/peacce/php/peacce.sys.php

     

警告:mysqli_num_rows()期望参数1为mysqli_result,   /home/orikina/public_html/peacce/php/peacce.sys.php中给出的null   第31行

我的代码:

$usernameCheck  = mysqli_query($myCon, "SELECT * FROM `users` WHERE `username` = 'username'");
            if (mysqli_num_rows($usernameCheck) > 0)
            {
                mysqli_query($myCon, "INSERT INTO users (username, password, email)
                    VALUES ('$username', '$passwordReal', '$email')")
                    or die();
                $usernameCheck  = mysqli_query($myCon, "SELECT * FROM users WHERE `username` = 'username'");
                if (mysqli_num_rows($usernameCheck) > 0) 
                {
                    header("location: ../register.php?data=registerSuccess");
                }
                else
                {
                    header("location: ../register.php?data=registerError&&refine=3");
                }
            }

1 个答案:

答案 0 :(得分:1)

您尚未初始化$myCon或连接时出错。您应该在连接失败时添加错误报告,或者像这样创建mysqli连接:

<?php
$myCon = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if ($myCon->connect_errno) {
    printf("Connect failed: %s\n", $myCon->connect_error);
    exit();
}

$usernameCheck  = mysqli_query($myCon, "SELECT * FROM `users` WHERE `username` = 'username'");
....

关于创建mysqli连接的PHP文档:http://www.php.net/manual/en/mysqli.quickstart.connections.php