Php 在Wookmark中使用MySQL并从数据库创建数组

Php 在Wookmark中使用MySQL并从数据库创建数组,php,mysqli,Php,Mysqli,我使用Wookmark插件,希望能够使用我的数据库输出内容。当前格式为PHP数组,如下所示: $data = array( array( 'id' => "1", 'title' => "First image", 'url' => "http://www.example.org/1", 'width' => "560", 'height' => "560", 'image' => "", 'prev

我使用Wookmark插件,希望能够使用我的数据库输出内容。当前格式为PHP数组,如下所示:

$data = array(
  array(
    'id' => "1",
    'title' => "First image",
    'url' => "http://www.example.org/1",
    'width' => "560",
    'height' => "560",
    'image' => "",
    'preview' => ""
  ),
  array(
    'id' => "2",
    'title' => "Second image",
    'url' => "http://www.example.org/1",
    'width' => "560",
    'height' => "560",
    'image' => "",
    'preview' => ""
  )
);
我已经尝试了一些代码并进行了测试,但在这方面没有太多经验。我试图在Stackoverflow上搜索,但没有成功。无论如何,这是我尝试使用MySQLI的结果,但它可能是完全错误的

$sth = mysqli_query($con, "SELECT * from entries");
$column = array();
while($row = mysqli_fetch_array($sth)){
    $column[] = $row[$key];
}

有什么解决方案吗?

您可以将图像数据保存在MySQL数据库中,并使用Wookmark jQuery插件以Wookmark样式显示在HTML页面上。可以通过使用Wookmark JSON API下载JSON数据来构建示例数据库

导入到数据库 从Wookmark下载和导入图像数据的快速PHP脚本

<?PHP
//### Import Wookmark's popular images to database ###//
$imgs = json_decode(file_get_contents('http://www.wookmark.com/api/json/popular'), true);

$SQL = "INSERT INTO images VALUES(:id, :title, :referer, :url, :width, :height, :image, :preview)";
$db = new PDO("mysql:host=localhost;dbname=imgbase", "username", "password");
$stmnt = $db->prepare($SQL);

foreach($imgs as $img){
    $stmnt->execute($img);
}

exit("Done!");
?>
以Wookmark风格显示图像 使用。对于本例,除了jQuery之外,只需要以下文件:

css/main.css libs/jquery.imagesloaded.js jquery.wookmark.min.js 示例api/index.html 编辑示例api/index.html文件以从http://www.wookmark.com/api/json/popular 指向popular.php页面的URL


当您导航到index.html时,您将看到以与wookmark.com相同的样式排列的图像

您想实现什么?是否要在当前存储为PHP数组的数据库中存储数据?数据库中是否已有数据并希望通过HTML显示?还有别的吗?
<?PHP
//### Import Wookmark's popular images to database ###//
$imgs = json_decode(file_get_contents('http://www.wookmark.com/api/json/popular'), true);

$SQL = "INSERT INTO images VALUES(:id, :title, :referer, :url, :width, :height, :image, :preview)";
$db = new PDO("mysql:host=localhost;dbname=imgbase", "username", "password");
$stmnt = $db->prepare($SQL);

foreach($imgs as $img){
    $stmnt->execute($img);
}

exit("Done!");
?>
<?PHP
$per_page = 10;
$page = 1;

if(isset($_GET['per_page'])){
    $per_page = (int) $_GET['per_page'];
    if($per_page < 10) $per_page = 10;
    if($per_page > 50) $per_page = 50;
}

if(isset($_GET['page'])){
    $page = (int) $_GET['page'];
    if($page < 1) $page = 1;
}

$SQL = "SELECT * FROM images LIMIT ".(($page-1)*$per_page).",".$per_page;
$db = new PDO("mysql:host=localhost;dbname=imgbase", "username", "password");
$imgdata = "(".json_encode($db->query($SQL)->fetchAll(PDO::FETCH_ASSOC)).")";
header("Content-Type: application/json");
echo isset($_GET['callback'])?$_GET['callback'].$imgdata:$imgdata;
?>