gpt4 book ai didi

PHP PDO - 我该怎么做

转载 作者:行者123 更新时间:2023-11-29 12:07:39 27 4
gpt4 key购买 nike

我需要一种方法来执行 4 个查询,或者使我的 $views 变量能够从获取最高观看次数切换到第二高观看次数等,我需要它们格式类似于数组,因此 $views[0]; 是第一高的观看次数,$views[4]; 是第四高的观看次数。

代码:

//Load Database Details
require_once 'functions/db/dbconfig.php';

//Database Connect - View - High - 1
$db = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

//Query and Fetch
$res = $db->prepare("SELECT * FROM tvinfo ORDER BY views LIMIT 0, 1;");
$res->setFetchMode(PDO::FETCH_ASSOC);
$res->execute();
$Result = $res->fetchAll();

//Result Variables
foreach ($Result as $r) {
$name = $r['name'];
$rating = $r['rating'];
$imdbid = $r['imdbid'];
$genre1 = $r['genre1'];
if(!empty($r['genre2'])){ $genre2 = '- '.$r['genre2']; }
$year = $r['year'];
$views = $r['views'];
}

最佳答案

未经测试,但应该可以工作。

require_once 'functions/db/dbconfig.php';

//Database Connect - View - High - 1
$db = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

//Query and Fetch
$res = $db->prepare("SELECT * FROM tvinfo ORDER BY views DESC LIMIT 4;");
$res->setFetchMode(PDO::FETCH_ASSOC);
$res->execute();
$Result = $res->fetchAll();
//Result Variables
$views = array();
foreach ($Result as $r)
{
$name = $r['name'];
$rating = $r['rating'];
$imdbid = $r['imdbid'];
$genre1 = $r['genre1'];
if(!empty($r['genre2'])){ $genre2 = '- '.$r['genre2']; }
$year = $r['year'];
$views[] = $r['views'];
}

var_dump($views);

关于PHP PDO - 我该怎么做,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31206490/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com