Mysql_num_rows()期望参数1为resource, boolean



我一直得到这个错误:

mysql_num_rows()期望参数1是resource, Boolean给出/home/u592632722/public_html/profile.php第80行

我想不出如何解决它。这是我的profile.php代码:
 <?php
    error_reporting (E_ALL ^ E_NOTICE);
    session_start();
    $userid = $_SESSION['userid'];
    $name = $_SESSION['name'];
    $username = $_SESSION['username'];
    $user = $_GET['user'];
    $title = "$user";
    ?>
<?php
    $user = $_GET['user'];
    require("./connect.php");
    $query = mysql_query("SELECT * FROM user WHERE username='$user'");
    $numrows = mysql_num_rows($query);
    echo $numrows;
    if ($numrows == 1){
        $title = "$user";
        $row = mysql_fetch_assoc($query);
        $dbid = $row['id'];
        $user = $row['username'];
        $dbname = $row['name'];
        $dbactive = $row['active'];
        $dbdate = $row['date'];
        echo "<h1 align='center'>$user's profile page</h1>";
        if($dbactive == 1){
            $active = "yes";
        }
        else{
            $active = "no";
        }
        if ($userid && $username){
            $query = mysql_query("SELECT * FROM friends WHERE (user_one='$user' and user_two='$username') or (user_one='$username' and user_two='$user')");
            $numrows = mysql_num_rows($query);
            if ($numrows == 1){
                echo "
                <center>
                    <table style='border:2px solid black; font-size:36px;' bgcolor='red'>
                        <ul>
                            <tr>
                                <td style='border:1px solid black;'>name:</td>
                                <td style='border:1px solid black;'>$dbname</td>
                            </tr>
                            <tr>
                                <td style='border:1px solid black;'>username:</td>
                                <td style='border:1px solid black;'>$user</td>
                            </tr>
                            <tr>
                                <td style='border:1px solid black;'>frends:</td>
                                <td style='border:1px solid black;'>yes</td>
                            </tr>
                            <tr>
                                <td style='border:1px solid black;'>date joined:</td>
                                <td style='border:1px solid black;'>$dbdate</td>
                            </tr>
                            <tr>
                                <td style='border:1px solid black;'>activated:</td>
                                <td style='border:1px solid black;'>$active</td>
                            </tr>
                        </ul>
                    </table>
                </center>";
            }
            else{
                $query = mysql_query("SELECT id FROM friend_req WHERE from='1' and to='1'");
                echo "Mysql query: $query";
                echo "</br>";
                $numromws = mysql_num_rows($query);
                echo "Mysql numrows: $numrows";
                if ($numromws == "1"){
                    echo "
                    <center>
                        <table style='border:2px solid black; font-size:36px;' bgcolor='red'>
                            <ul>
                                <tr>
                                <tr>
                                    <td style='border:1px solid black;'>name:</td>
                                    <td style='border:1px solid black;'>$dbname</td>
                                </tr>
                                <tr>
                                    <td style='border:1px solid black;'>username:</td>
                                    <td style='border:1px solid black;'>$user</td>
                                </tr>
                                <tr>
                                    <td style='border:1px solid black;'>friends:</td>
                                    <td style='border:1px solid black;'>waiting for friend request confirmation</td>
                                </tr>
                                <tr>
                                    <td style='border:1px solid black;'>date joined:</td>
                                    <td style='border:1px solid black;'>$dbdate</td>
                                </tr>
                                <tr>
                                    <td style='border:1px solid black;'>activated:</td>
                                    <td style='border:1px solid black;'>$active</td>
                                </tr>
                            </ul>
                        </table>
                    </center>";
                }
                else{
                    $query = mysql_query("SELECT * FROM friend_req WHERE 'from'='$user' AND 'to'='$username'");
                    $numromws = mysql_num_rows($query);
                    if ($numromws == "1"){
                        echo "
                        <center>
                            <table style='border:2px solid black; font-size:36px;' bgcolor='red'>
                                <ul>
                                    <tr>
                                    <tr>
                                        <td style='border:1px solid black;'>name:</td>
                                        <td style='border:1px solid black;'>$dbname</td>
                                    </tr>
                                    <tr>
                                        <td style='border:1px solid black;'>username:</td>
                                        <td style='border:1px solid black;'>$user</td>
                                    </tr>
                                    <tr>
                                        <td style='border:1px solid black;'>frends:</td>
                                        <td style='border:1px solid black;'><form method='post' action='./$user'><input type='submit' name='acceptfriendreq' value='accept friend request' /></form></td>
                                    </tr>
                                    <tr>
                                        <td style='border:1px solid black;'>date joined:</td>
                                        <td style='border:1px solid black;'>$dbdate</td>
                                    </tr>
                                    <tr>
                                        <td style='border:1px solid black;'>activated:</td>
                                        <td style='border:1px solid black;'>$active</td>
                                    </tr>
                                </ul>
                            </table>
                        </center>";
                    }
                    else{
                        echo "
                        <center>
                            <table style='border:2px solid black; font-size:36px;' bgcolor='red'>
                                <ul>
                                    <tr>
                                        <td style='border:1px solid black;'>name:</td>
                                        <td style='border:1px solid black;'>$dbname</td>
                                    </tr>
                                    <tr>
                                        <td style='border:1px solid black;'>username:</td>
                                        <td style='border:1px solid black;'>$user</td>
                                    </tr>
                                    <tr>
                                        <td style='border:1px solid black;'>frends:</td>
                                        <td style='border:1px solid black;'><form method='post' action='./$user'><input type='submit' name='friendcreate' value='send friend request' /></form></td>
                                    </tr>
                                    <tr>
                                        <td style='border:1px solid black;'>date joined:</td>
                                        <td style='border:1px solid black;'>$dbdate</td>
                                    </tr>
                                    <tr>
                                        <td style='border:1px solid black;'>activated:</td>
                                        <td style='border:1px solid black;'>$active</td>
                                    </tr>
                                </ul>
                            </table>
                        </center>";
                    }
                }
            }
        }
        else{
            echo "
            <center>
                <table style='border:2px solid black; font-size:36px;' bgcolor='red'>
                    <ul>
                        <tr>
                            <td style='border:1px solid black;'>name:</td>
                            <td style='border:1px solid black;'>$dbname</td>
                        </tr>
                        <tr>
                            <td style='border:1px solid black;'>username:</td>
                            <td style='border:1px solid black;'>$user</td>
                        </tr>
                        <tr>
                            <td style='border:1px solid black;'>date joined:</td>
                            <td style='border:1px solid black;'>$dbdate</td>
                        </tr>
                        <tr>
                            <td style='border:1px solid black;'>activated:</td>
                            <td style='border:1px solid black;'>$active</td>
                        </tr>
                    </ul>
                </table>
            </center>";
        }
    }
    elseif ($user == ""){
        $title = "profile page";
        echo "<h1 align='center'>Profile page</h1>";
        if($userid && $username){
            echo "<h1 align='center'>my profile</h1>";
            $query = mysql_query("SELECT * FROM user WHERE username='$username'");
            $title = "$username";
            $row = mysql_fetch_assoc($query);
            $dbid = $row['id'];
            $dbname = $row['name'];
            $dbactive = $row['active'];
            $dbdate = $row['date'];
            if($dbactive == 1){
                $active = "yes";
            }
            else{
                $active = "no";
            }
            echo "
            <center>
                <table style='border:2px solid black; font-size:36px;' bgcolor='red'>
                    <ul>
                        <tr>
                            <td style='border:1px solid black;'>name:</td>
                            <td style='border:1px solid black;'>$dbname</td>
                        </tr>
                        <tr>
                            <td style='border:1px solid black;'>username:</td>
                            <td style='border:1px solid black;'>$username</td>
                        </tr>
                        <tr>
                            <td style='border:1px solid black;'>date joined:</td>
                            <td style='border:1px solid black;'>$dbdate</td>
                        </tr>
                        <tr>
                            <td style='border:1px solid black;'>activated:</td>
                            <td style='border:1px solid black;'>$active</td>
                        </tr>
                    </ul>
                </table>
            </center>";
        }
    }
    else {
        $title = "profile page";
        echo "<h1 align='center'>the user does not exsist</h1>";
    }
    mysql_close();
?>

这是我的connect。php

<?php
mysql_connect("mysql.freehostingnoads.net", "u592632722_mat", "password");
mysql_select_db("u592632722_info");
?>

password替换为我的实际密码。请帮助!

你的代码可能有两个问题。

首先,变量$user可能为空。测试它是否为null

<?php
   if(empty($user)){
    //do not continue;
   }
?>

第二种可能是,你没有连接到mysql。你可以这样修改。

<?php
 $con = mysql_query($host,$user,$password);
 if($con == false)
 //do not continue;
 if(!mysql_select_db($db))
 //do not continue;    
?>
<?php
  $query = mysql_query("SELECT * FROM `user` WHERE `username` = '{$username}'");
  //make sure $username is not empty
 if($query == false)
  //mysql_query returned false, because your query is not correct
?>

建议:mysql*已正式弃用。使用mysql *或PDO* .

相关内容

  • 没有找到相关文章

最新更新