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条)

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路