duanpiyao2734 2014-01-31 04:59
浏览 18
已采纳

Mysqli num row返回不起作用

I'm wondering why it is not working for mysqli eventhou mysql_num_row is working.

if (mysql_num_rows($rows) > 0) {
    echo "<p>That name has been taken </p>";
}

That is mysql. But, Im trying convert it to mysqli.

 if (mysqli_num_rows($rows) > 0) {
        echo "<p>That name has been taken </p>";
    }

It supposed to be displayed on the screen but it's not. And there is nothing error message displayed. Or am I missing something? Any ideas?

  • 写回答

2条回答 默认 最新

  • doushao1948 2014-01-31 05:22
    关注

    First, a quick tutorial on some of the differences between mysql_* and mysqli_* functions.

    In mysql_* you would have 3 parameters for your DB connection, then have a seperate line for your DB selection.

    For example:

    $db = mysql_connect("host","username", "password");
    $db_selected = mysql_select_db('db_name', $db);
    if (!$db_selected) {
        die ('Can\'t use this : ' . mysql_error());
    }
    

    Your query would come first, followed by your DB connection.

    For example:

    mysql_query($query,$db);
    

    But in mysqli_* things have changed including the parameters location. You now put all 4 parameters, for example (if you haven't done so yet):

    $db = new mysqli("host","username", "password", "db_name");
    
    if (mysqli_connect_errno()) {
    printf("Connect failed: %s
    ", mysqli_connect_error());
    exit();
    }
    

    then the DB connection would come first, followed by the query instead of the other way around:

    mysqli_query($db,$query);
    

    A sample query:

    $email = mysqli_real_escape_string($db,$_POST['email']);
    $query = mysqli_query($db, "SELECT * FROM table_name WHERE email='".$email."'");
    
    if(mysqli_num_rows($query) > 0){
    
        echo "email already exists";
    }else{
        $sql="INSERT INTO table_name (email) VALUES ('$email')";
        if (!mysqli_query($db,$sql))
        {
            die('Error: ' . mysqli_error($db));
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?