【问题标题】:SQL Query to JavaScript Array对 JavaScript 数组的 SQL 查询
【发布时间】:2015-01-10 19:57:09
【问题描述】:

我正在尝试使用 blueimp 制作视频库。 https://github.com/blueimp 不幸的是,我对 MySQL 并不熟练,我想知道如何将带有 youtube URL 的查询返回到 JavaScript 数组中。 示例:

blueimp.Gallery([ { 标题:'辛特尔', href: 'https://archive.org/download/Sintel/sintel-2048-surround_512kb.mp4', 类型:'视频/mp4', 海报:'https://i.imgur.com/MUSw4Zu.jpg' }, ...

已经谢谢了,希望你能帮上忙。

【问题讨论】:

  • 你使用什么语言来执行 MySQL 查询?

标签: javascript mysql sql


【解决方案1】:

您应该在数组上使用 PHP 函数 json_encode() 将其转换为 javascript 对象。

你应该做类似what was done here

$sth = mysqli_query("SELECT ...");
$rows = array();
while($r = mysqli_fetch_assoc($sth)) {
    $rows[] = $r;
}
print json_encode($rows);

您可能需要做一些工作才能将数组组装成您想要的格式。在上述代码 IE 的 while 循环中执行此操作:

$sth = mysqli_query("SELECT ...");
$rows = array();
while($r = mysqli_fetch_assoc($sth)) {
    $rows[]['title'] = $r['title'];
    $rows[]['href'] = $r['href'];
}
print json_encode($rows);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 2016-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    相关资源
    最近更新 更多