PHP代码中的错误

PHP代码中的错误,php,Php,我的代码出错了。如果我输入了一个错误的用户,它就会显示出来 用户ok 如果我输入正确的用户,它也会显示 用户ok 我不知道我的代码中的错误在哪里,所以请看一下我的代码,让我知道哪里出错了 Login.php <html> <body> <form action="list_work.php" method="post"> username: <input type="text" name="username">

我的代码出错了。如果我输入了一个错误的用户,它就会显示出来

用户ok

如果我输入正确的用户,它也会显示

用户ok

我不知道我的代码中的错误在哪里,所以请看一下我的代码,让我知道哪里出错了

Login.php

<html>
  <body>
    <form action="list_work.php" method="post">
      username: <input type="text" name="username">
      password: <input type="text" name="password">
      <input type="submit">
    </form>
  </body>
</html>
List_work.php

<?php
  $username = $_POST["username"];
  $password = $_POST["password"];

  // Connect to the database
  $dbLink = new mysqli('localhost', 'sqldata', 'sqldata', 'balhaf');
  if(mysqli_connect_errno()) {
    die("MySQL connection failed: ". mysqli_connect_error());
  }

  mysqli_select_db($dbLink,"balhaf2");

  // Fetch the file information
  $query = "select *  from users WHERE username = '".$dbLink-  >escape_string($username)."'";

  $result = $dbLink->query($query);
  $company = false;
  if($result) {
  echo "user ok"."</br>";
  //Now get the result information
  $row = $result->fetch_object();  //will store the record in $row

 //Access what you need
  if($row) {
    $company = $row->company;  //variable name should match the field name in your  database
    echo $company; //See if you get the value stored in the database
  }
  } else {
   echo "worng user";
  }

  mysqli_select_db($dbLink,"balhaf");


  // Query for a list of all existing files
  $sql = "SELECT id, name, mime, size, created FROM $company";
  $result = $dbLink->query($sql);
  // Check if it was successfull
  if($result) {
    // Make sure there are some files in there
    if($result->num_rows == 0) {
      echo '<p>There are no files in the database</p>';
    } else {
      // Print the top of a table
      echo '<table border="1" align="center">
          <H2 align="center"> Report Table</H>

            <tr>
                <td><b>Name</b></td>
                <td><b>Mime</b></td>
                <td><b>Size (bytes)</b></td>
                <td><b>Created</b></td>
                <td><b>&nbsp;</b></td>
            </tr>';

    // Print each file
    while($row = $result->fetch_assoc()) {
        echo "
            <tr>
                <td>{$row['name']}</td>
                <td>{$row['mime']}</td>
                <td>{$row['size']}</td>
                <td>{$row['created']}</td>
                <td><a style='text-decoration:none;' href='get_file_work.php?id= {$row['id']}&company=$company'>Download</a></td>
            </tr>";
       }

       // Close table
       echo '</table>';
      }

     // Free the result
     $result->free();
     }
     else
     {
     echo 'Error! SQL query failed:';
     echo "<pre>{$dbLink->error}</pre>";
     }
     // Close the mysql connection
     $dbLink->close();
     ?>

将以下内容更改为:

if( $result && mysqli_num_rows($result) > 0 ) {
if($result  &&  $result->num_rows) {
致:

$result仍然是一个对象,因此$result不为NULL、FALSE或0

编辑

第一次我忘了加$result本身。如果查询失败,则$result==FALSE。mysqli_num_rows$result将抛出警告: 警告:mysqli_num_rows期望参数1为mysqli_结果,给定布尔值

…如果它不是mysqli_结果对象。因此,通过首先检查$result是否为false,我们可以防止错误的发生


注意:undone的答案第一次是正确的,即使是在我的答案之后:p

您需要检查返回的行数: 更改:

致:


备注:$result=$dbLink->query$query;当SQL语句出现问题时,失败时返回FALSE。除此之外,它将返回一个与if语句中的true相同的对象。

发布错误消息是否会对您更有帮助。。?由于缺少信息,这些类型的问题会产生大量的否决票。没有错误消息,如果我提供了错误的用户,则显示用户ok,如果我输入了正确的用户,则显示用户ok。这是我收到的错误消息,请仔细阅读我的帖子。
if($result ){
if($result  &&  $result->num_rows) {