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

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

报告相同问题?

悬赏问题

  • ¥15 请提供一个符合要求的网页链接。
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码