0

Explanation of the issue is:

  • Query sql returns 6 rows of data (of 14 fields each) when run in phpMyAdmin
  • On the php page mysqli_num_rows returns: 6
  • print_r($sql_res) returns: mysqli_result Object ( [current_field] => 0 [field_count] => 14 [lengths] => [num_rows] => 6 [type] => 0 )
  • var_dump($sql_res) returns: object(mysqli_result)#1 (5) { ["current_field"]=> int(0) ["field_count"]=> int(14) ["lengths"]=> NULL ["num_rows"]=> int(6) ["type"]=> int(0)

There is data in the db, but is not being parsed into the table structure since no data containing field values are returned. Any help or pointers would highly be appreciated. DB connection is OK, my other pages are working, I'm using session variables in query ($selected and $selfair), my php code that gives the headache is:

<?php
$sql = "SELECT har_id, har_fair_id, har_kat_id, har_tarih, har_co_id, har_desc, har_docno, har_kur, har_debit, har_credit, har_used, har_invoiced, har_cancelled, har_schid FROM hareketler WHERE har_co_id =". $selected." AND har_fair_id =". $selfair;
echo $sql."<br><br>";
//sql returns correct statement
$sql_res = mysqli_query($con, $sql);
print_r($sql_res)."<br><br>";
echo "<br><br>".mysqli_num_rows($sql_res);
// row count is 6 but no other data is fetched from db!
echo "<br><br>".var_dump($sql_res);
while($row = mysqli_fetch_array($sql_res,MYSQLI_ASSOC));{
// detail rows are NOT fetched!
    if($row<>''){
    echo '<tbody>';
        echo '<tr>';
            echo '<td class="katdata">'.$row['har_id'].'</td>';
            echo '<td class="katdata">'.$row['har_fair_id'].'</td>';
            echo '<td class="katdata">'.$row['har_kat_id'].'</td>';
            echo '<td class="katdata">'.$row['har_tarih'].'</td>';
            echo '<td class="katdata">'.$row['har_co_id'].'</td>';
            echo '<td class="katdata">'.$row['har_desc'].'</td>';
            echo '<td class="katdata">'.$row['har_docno'].'</td>';
            echo '<td class="katdata">'.$row['har_kur'].'</td>';
            echo '<td class="katdata">'.$row['har_debit'].'</td>';
            echo '<td class="katdata">'.$row['har_credit'].'</td>';
            echo '<td class="katdata">'.$row['har_used'].'</td>';
            echo '<td class="katdata">'.$row['har_invoiced'].'</td>';
            echo '<td class="katdata">'.$row['har_cancelled'].'</td>';
            echo '<td class="katdata">'.$row['har_schid'].'</td>';
        echo '</tr>';
    echo '</tbody>';
    }
    else {
        echo "<br><br>No data returned!";
        // sql is correct but no detail data is fetched!
    }
}
?>
6
  • 3
    Welcome. That ; in your while statement does work? Commented Sep 27, 2018 at 10:13
  • 1
    Your code is prone to SQL injections..(stackoverflow.com/questions/601300/what-is-sql-injection) you should prevent them ( stackoverflow.com/questions/60174/… ) Commented Sep 27, 2018 at 10:14
  • @kerbholz Thanks a million! I guess I really need better glasses! I'm also sorry for occupying time here for a stupid typo... Thanks again. Commented Sep 27, 2018 at 10:23
  • @Raymond Nijland Thanks Raymond, I will read those articles. Commented Sep 27, 2018 at 10:26
  • "I guess I really need better glasses!" Or a decent IDE! Or error_reporting! ;) Glad it works. Commented Sep 27, 2018 at 10:27

2 Answers 2

1

You have a typo:

while($row = mysqli_fetch_array($sql_res,MYSQLI_ASSOC));{

should be

while($row = mysqli_fetch_array($sql_res,MYSQLI_ASSOC)){
Sign up to request clarification or add additional context in comments.

Comments

0

You have to make sure that there is a single quote(') around the values you are comparing. For instance

SELECT * FROM table WHERE column_name = 'value';

will work, but

SELECT * FROM table WHERE column_name = value;

will not work.

So make sure your output is:

SELECT har_id, har_fair_id, har_kat_id, har_tarih, har_co_id, har_desc, har_docno, 
har_kur, har_debit, har_credit, har_used, har_invoiced, har_cancelled, har_schid 
FROM hareketler WHERE har_co_id ='selected' AND har_fair_id ='selfair';

To achieve this, change your sql to

$sql = "SELECT har_id, har_fair_id, har_kat_id, har_tarih, har_co_id, har_desc, 
har_docno, har_kur, har_debit, har_credit, har_used, har_invoiced, har_cancelled, 
har_schid FROM hareketler WHERE har_co_id ='". $selected."' AND har_fair_id ='". 
$selfair."';";

This Should Solve your problem, that, and there is a misplaced semicolon in your while loop. while($row = mysqli_fetch_array($sql_res,MYSQLI_ASSOC));{. You need to fix that Thank You, hope this was helpful.

2 Comments

Whilst that's all true, concatenating a value into a SQL string is not a good idea as it opens you up to SQL injection attacks. Better approaches here: stackoverflow.com/questions/60174/…
I use PDO objects, its much safer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.