1

I'm struggling to pull data using PHP from an array stored as LONGTEXT in a MySQL table.

I'm creating a notification script that pulls from two separate tables ('events' and 'bookings'). Amongst other columns, the 'bookings' table is structured like so, with a LONGTEXT array stored in the 'booking_meta' column:

+------+------+------------+------------------------+
| booking_id  | event_id   | booking_meta           |
+------+------+------------+------------------------+
|           1 |        12  |  (example array below) |
|           2 |        12  |                        |
|           3 |        12  |                        |
|           4 |        12  |                        |
|           5 |        13  |                        |
|           6 |        13  |                        |
|           7 |        13  |                        |
+------+------+------------+------------------------+    

Example array:

a:1:{s:12:"registration";a:5:{s:10:"user_email";s:14:"[email protected]";s:9:"user_name";s:9:"Full Name";s:10:"first_name";s:4:"Full";s:9:"last_name";s:4:"Name";s:10:"dbem_phone";s:8:"12345678";}} 

Essentially, i'd like to pull the 'user_name' and 'dbem_phone' from the array in 'booking_meta' and echo it in a list of all bookings in the next 15 minutes. Everything in the following query works, but i have no idea how to parse this array on top of what's already going on:

$bookingresult = mysqli_query($con,"SELECT * FROM events, bookings
WHERE event_start_date='$date' AND event_start_time BETWEEN '$currenttime' and '$currenttime15' AND events.event_id = bookings.event_id");

echo "Bookings for events in the next 15 minutes: <br>";

while($row = mysqli_fetch_array($bookingresult)) {
    echo $row['booking_id'] . " " . $row['event_name'];
    $phonedata = mysqli_fetch_array($row['booking_meta']);
    echo $phonedata['phone'];
    echo "<br>";
}
2

1 Answer 1

1

The data stored in the booking_meta field looks like a serialized PHP array. An array is passed to serialize to produce the value of booking_meta.
To turn it back into an array, pass the string to unserialize.

In short, and as I said in my comment:

$phonedata = mysqli_fetch_array($row['booking_meta']);
//should be
$phonedata = unserialize($row['booking_meta']);
echo $phonedata['registration']['dbem_phone'];

Try var_dump($phonedata); to get an idea of how the data is actually structured (it's a 2D array).

PS: You are using $row as an associative array, which is fine. But you're fetching the row both as an associative as well as a numerically indexed array. Which is the default behaviour of mysqli_fethc_array, consider switching to mysqli_fetch_assoc

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Elias - have done. However still nothing being printed when i echo $phonedata['dbem_phone']; - is there anything more i need to do with the array now that it's unserialised? Appreciate the help.
@Tom: Updated my answer... basically: it's a multi-dimensional array
Solved - was reading array wrong. Needed: echo $meta['registration']['dbem_phone']

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.