error in my PHP code -


i'm getting error in code. if put wrong user, shows

user ok

if put correct user, shows

user ok

i don’t know error in code please take @ code , let me know went wrong.

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 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 file information   $query = "select *  users username = '".$dblink-  >escape_string($username)."'";    $result = $dblink->query($query);   $company = false;   if($result) {   echo "user ok"."</br>";   //now result information   $row = $result->fetch_object();  //will store record in $row   //access need   if($row) {     $company = $row->company;  //variable name should match field name in  database     echo $company; //see if value stored in database   }   } else {    echo "worng user";   }    mysqli_select_db($dblink,"balhaf");     // query list of existing files   $sql = "select id, name, mime, size, created $company";   $result = $dblink->query($sql);   // check if successfull   if($result) {     // make sure there files in there     if($result->num_rows == 0) {       echo '<p>there no files in database</p>';     } else {       // print top of 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 result      $result->free();      }      else      {      echo 'error! sql query failed:';      echo "<pre>{$dblink->error}</pre>";      }      // close mysql connection      $dblink->close();      ?> 

change following from:

if($result) { 

to:

if( $result && mysqli_num_rows($result) > 0 ) { 

$result still object, $result not null, false, or 0.

edit

i neglected add $result first time. if query fails, $result == false. mysqli_num_rows($result) throw warning:

warning: mysqli_num_rows() expects parameter 1 mysqli_result, boolean given

...if mysqli_result object. so, first checking $result not false, prevent error occuring.

note: undone's answer correct first time, if after mine :p


Comments

Popular posts from this blog

design - Custom Styling Qt Quick Controls -

Unable to remove the www from url on https using .htaccess -