0

I have this table and I would like to select some of the data into a form, but I my query is not working. can anyone figure out what is wrong with my query?

enter image description here

Above the table, I only wanted to show all voucher in voucher_3 and user_id = 1 that has been verified as '1'.

and there is an error when I echo out:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in D:\Application\Apps\xampp\htdocs\newtmk\account.php on line 302

here is my coding:

   <tbody>

                            <?php
                                $get_member = "SELECT * 
                                FROM transaction_record_tpg
                                WHERE user_id = '1' 
                                AND voucher = '1'
                                AND (voucher_3 = 'RM 3 Voucher' 
                                OR voucher_3 = 'RM 5 Voucher'
                                OR voucher_3 = 'RM 10 Voucher')
                                ";
                                $run_customer = mysqli_query($conn,$get_member);

                                $i = 0;

                                while($row_orders = mysqli_fetch_array($run_customer)){

                                $id = $row_orders['trans_id'];

                                $description = $row_orders['v_script'];

                                $date = $row_orders['collected_datetime'];

                                $outlet = $row_orders['voucher_3'];

                                $voucher = $row_orders['voucher_code'];

                                $i++;
                            ?>
                        <tr><!-- tr Starts -->

                            <td><?php echo $i; ?></td>
                            <td><?php echo $outlet; ?></td>
                            <td><?php echo $voucher; ?></td>
                            <td><?php echo $description; ?></td>
                            <td><?php echo $date; ?></td>
                        </tr><!-- tr Ends -->
                            <?php } ?>
                        
                    </tbody>
Dharman
  • 30,962
  • 25
  • 85
  • 135
louis
  • 57
  • 7
  • You need parentheses around your `OR` terms i.e. `(voucher_3 = 'RM 3 Voucher' OR voucher_3 = 'RM 5 Voucher' OR voucher_3 = 'RM 10 Voucher')` – Nick Oct 23 '18 at 08:20
  • If you want to get all the voucher_3 rows that are not empty, you can use WHERE user_id=1 AND verified=1 AND voucher_3 IS NOT NULL – Alberto Oct 23 '18 at 08:21

3 Answers3

2

Try below using bracket for OR condition

SELECT * FROM transaction_record_tpg 
WHERE user_id = '1' 
AND voucher = '1' 
AND (voucher_3 = 'RM 3 Voucher' 
OR voucher_3 = 'RM 5 Voucher' 
OR voucher_3 = 'RM 10 Voucher')

OR You can try using in operator like below

SELECT * FROM transaction_record_tpg 
WHERE user_id = '1' 
AND voucher = '1' 
AND voucher_3 in ('RM 3 Voucher','RM 5 Voucher','RM 10 Voucher')
Fahmi
  • 37,315
  • 5
  • 22
  • 31
1

Although your requirement is not clear I guess that the following should do the trick for you:

$get_member = "SELECT * FROM transaction_record_tpg 
WHERE user_id = '1' 
AND voucher = '1' 
AND (voucher_3 = 'RM 3 Voucher' 
OR voucher_3 = 'RM 5 Voucher' 
OR voucher_3 = 'RM 10 Voucher')";

Since you want voucher_3 to be one of the 'RM 3 Voucher', 'RM 5 Voucher', 'RM 10 Voucher', you'd need to enclose the OR statements in parentheses.

You can also check IN funcion which is usually preferred over multiple OR statements as it increases readability.

Giorgos Myrianthous
  • 36,235
  • 20
  • 134
  • 156
0

Supposing that user_id and verified are INT columns and you need to check for the verified column to be set as 1 and all the voucher_3 rows that are not empty, you can try the following:

$get_member = "SELECT * FROM transaction_record_tpg 
WHERE user_id = 1 
AND verified = 1 
AND voucher_3 IS NOT NULL
Alberto
  • 674
  • 13
  • 25