奇怪的mysql错误,说“期望参数”,但mysql_error函数什么都不返回?我迷路了

时间:2013-03-26 04:55:57

标签: php html mysql die

我遇到了一个奇怪的mysql错误。我得到以下文字:Warning: mysql_num_rows() expects parameter 1 to be resource, null given in /Applications/XAMPP/xamppfiles/htdocs/chatterr/lr_bootstrap/profile.php on line 70虽然代码没有任何问题。我尝试在die("Error in query: ".mysql_error());之上添加if (mysql_num_rows($check_frnd_query) == 1 OR $user_id == $my_id) {,但没有返回任何内容。我是这个问题的愚蠢。我可能正在寻找一些东西。你觉得怎么样?

<?php 
ob_start(); 
include 'core/init.php'; 
include 'includes/overall/overall_header.php'; 

if (isset($_GET['username']) === true && empty($_GET['username']) === false) {
$username       = $_GET['username'];

if (user_exists($username) === true) {
    $my_id          = $session_user_id;
    $user_id        = user_id_from_username($username);
    $profile_data   = user_data($user_id, 'username', 'first_name', 'last_name',     'email', 'profile_img', 'bio');
    $user           = $user_id;
?>


<div class="container-fluid">
  <div class="row-fluid">
    <?php include 'includes/sidebar.php'; ?>
    <div class="span9">
      <div class="hero-unit">
        <p>
        <?php 
        if (empty($profile_data['profile_img']) === false) {

            echo '<img src="', $profile_data['profile_img'],'" alt="',     $profile_data['first_name'] ,'\'s Profile Image" class="profile">'; 
        }
        ?></p>
        <h2><?php echo $profile_data['first_name'],' ', $profile_data['last_name']; ?>    </h2>
        <p><?php echo $profile_data['bio']?></p>
        <p>

        <?php if($user_id === $session_user_id) {
        ?> <a href="settings.php">Edit Profile</a> &nbsp;&nbsp;&nbsp; <?php
        } else {
        if (logged_in() === true) {
        if($user != $my_id){
        $check_frnd_query = mysql_query("SELECT id FROM friends WHERE     (user_one ='$my_id' AND user_two='$user') OR (user_one='$user' AND user_two='$my_id')");

            if (mysql_num_rows($check_frnd_query) == 1) {
                echo "Friends | <a href='actions.php?    action=unfriend&user=$user' class='box'>Unfriend $username</a>";
            } else {
                $from_query = mysql_query("SELECT `id` FROM     `friend_req` WHERE `from`='$user' AND `to`='$my_id'");
                $to_query = mysql_query("SELECT `id` FROM `friend_req`     WHERE `from`='$my_id' AND `to`='$user'");

                if(mysql_num_rows($from_query) == 1) {
                    echo "<a href='actions.php?    action=accept&user=$user' class='box'>Accept</a> | <a href='actions.php?action=ingnore&user=$user' class='box'>Ignore</a>";
                } else if (mysql_num_rows($to_query) == 1) {
                    echo "<a href='actions.php?action=cancel&user=$user' class='box'>Cancel Request</a>";
                } else {
                    echo "<a href='actions.php?    action=send&user=$user' class='box'>Send Friend Request </a>";
                }

            }
        }
        } //logged in
        } ?>

        <!-- If friends:
        <a>My Photos</a> &nbsp;&nbsp;&nbsp;
        <a>My Friends</a> &nbsp;&nbsp;&nbsp;
        <a>Message Me</a>
        </p>
        -->
        <!--<p><a href="#" class="btn btn-primary btn-large">My Profile &raquo;</a></p> --    >
      </div>
      <div class="row-fluid">
      <?php

      if (mysql_num_rows($check_frnd_query) == 1 OR $user_id == $my_id) {
      $res=mysql_query("SELECT * FROM posts WHERE user_id=$user_id ORDER BY     timestamp DESC LIMIT 0, 3");

      while($row=mysql_fetch_assoc($res)) {
        ?>
            <div class="span4">
              <h2><?php echo $profile_data['first_name']?> said...</h2>
              <h8><?php echo $row['timestamp']; ?></h8>
              <p><?php echo $row['content']; ?></p>
              <p><a class="btn" href="#">View Reactions &raquo;</a></p>
            </div><!--/span-->

        <?php
      }
      ?>
      </div><!--/row-->

      <div class="row-fluid">
      <?php
      $res=mysql_query("SELECT * FROM posts WHERE user_id=$user_id ORDER BY timestamp DESC LIMIT 3, 6");

      while($row=mysql_fetch_assoc($res)) {
        ?>
            <div class="span4">
              <h2><?php echo $profile_data['first_name']?> said...</h2>
              <h8><?php echo $row['timestamp']; ?></h8>
              <p><?php echo $row['content']; ?></p>
              <p><a class="btn" href="#">View Reactions &raquo;</a></p>
            </div><!--/span-->

        <?php
      }
      ?>
      </div><!--/row-->


    </div><!--/span-->
  </div><!--/row-->


<?php
} else {
    echo '<h3>Whoops, you must be friends with this user to see their posts.</h3>';
}

} else {
    header('Location: 404.php');
    exit();
}
} else {
header('Location: index.php');
exit();
}

include'includes/overall/overall_footer.php'; 
?>

这就是我所看到的:http://pbrd.co/Zo6sNb

2 个答案:

答案 0 :(得分:0)

您的查询可能失败并返回FALSE而不是mysql_num_rows预期的资源标识符。

在进一步传递之前,请务必检查mysql_query的返回值。

$check_frnd_query = mysql_query("SELECT id FROM friends WHERE     (user_one ='$my_id' AND user_two='$user') OR (user_one='$user' AND user_two='$my_id')");
if (!$check_frnd_query) {
    die('Invalid query: ' . mysql_error());
}

答案 1 :(得分:0)

SELECT * FROM posts WHERE user_id=$user_id ORDER BY     timestamp DESC LIMIT 0, 3

在此查询中,user_id没有引号,它是数字?

SELECT id FROM friends WHERE     (user_one ='$my_id' AND user_two='$user') OR (user_one='$user' AND user_two='$my_id')

在此查询中,user_id类型字段有引号,它是一个字符串?

您是否在第一个查询中缺少引号?