r/PHPhelp • u/Adventurous-Bat-6332 • May 16 '23
Cannot get all matching values when MySQL query is run in PHP
Hello everyone
This is my first post here and the post concerns getting matching values using a MySQL query run in a PHP script.
I have two PHP scripts, one is a page template and the other collects SQL query functions, and a MySQL database named bookings.
The page template simply calls a function named find_booking_by_id:
isset($_GET['id']) ? $id = $_GET['id'] : $id = "";
$bookings = find_cbooking_by_id($id);var_dump($bookings);
The function is defined in the other php file as follows:
function find_booking_by_id($id) {global $db;$sql = "SELECT * FROM bookings ";$sql .= 'WHERE product_id="' . db_escape($db, $id) . '" ';$result = mysqli_query($db, $sql);confirm_result_set($result);$booking = mysqli_fetch_assoc($result);mysqli_free_result($result);return $booking;}
The booking database contains a field named product_id that has a number of records with the same product_id values, which I would like to get by running the SELECT query as written above.
The problem I'm faced with is although the same query does return all of the records with the matching product_id, it only returns the first record with the matching product_id when it is called from these PHP scripts. Calling var_dump($result) after $result = mysqli_query($db, $sql); and var_dump($booking) after $booking = mysqli_fetch_assoc($result); also returns the first matching record.
It would be appreciated if anyone would help me work out the correct PHP script to return all the matching records.
Thanks for reading this,
Ead
4
u/lovesrayray2018 May 16 '23
This is as expected. mysqli_fetch_assoc fetches the next row of a result set. Just 1 row.
If you want all matching rows to be returned, you want to use mysqli_fetch_all which returns a two-dimensional array of all result rows as an associative array. You can then loop through the results row by row using a foreach
1
u/Adventurous-Bat-6332 May 16 '23
Thanks! I didn't know how to implement mysqli_fetch_all(), so I iterated the fech_assoc() property instead and became able to fetch any properties of the matching records:
function find_cbooking_by_id($id) {global $db;$sql = "SELECT * FROM bookings ";$sql .= 'WHERE product_id="' . db_escape($db, $id) . '" ';$result = mysqli_query($db, $sql);confirm_result_set($result);$bookings = array();while ($booking = $result->fetch_assoc()) {$bookings[] = $booking;}return $bookings;}-1
u/32gbsd May 16 '23
dont use fetch all. unless you know exactly how many rows are going to be returned.
0
May 16 '23
[deleted]
0
u/lovesrayray2018 May 16 '23 edited May 16 '23
confirm_result_set($result);
Use try catch blocks to track errors and display them.
This function above, you are calling it before the mysqli_fetch_all($result), what is it doing?
Edit, just saw ur function is defined with name of
find_booking_by_id($id)Your calling this function however as
$bookings = find_cbooking_by_id($id)Do you see that extra c in the fn name?
7
5
u/j-c-s-roberts May 16 '23
I use PDO, and so have no experience with this style. However, I do notice you have used the function fetch_assoc. In PDO a similar thing only fetches the first row. I would think there is a function called fetch_all that would be better for you, and would fetch all rows.
Regardless, I would highly recommend PDO. It is so much easier and safer than using mysqli.
A great tutorial can be found here: https://www.phpdelusions.net/pdo
2
u/RaiseHistorical1780 May 17 '23
You can use mysqli_fetch_assoc in a while loop: while($x = mysqli_fetch_assoc($result)){
};
6
u/RandyHoward May 16 '23
For readability, I'd change this:
To this:
You can also do this, which I tend to prefer these days:
All three lines are equivalent.