dtk31564 2014-07-17 08:02
浏览 30
已采纳

从循环内的表中获取数组

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:"test@email.com";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>";
}
  • 写回答

1条回答 默认 最新

  • doudiecai1572 2014-07-17 08:10
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 使用Java milo连接Kepserver服务端报错?
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了
  • ¥15 微信小程序商城如何实现多商户收款 平台分润抽成