gpt4 book ai didi

php - 基于其他行的 PDO 查询过滤器

转载 作者:行者123 更新时间:2023-11-30 00:45:56 25 4
gpt4 key购买 nike

这是我的代码:

//Connects to Database
if ($_SERVER["REQUEST_METHOD"] == "POST")
try
{
$dbh = new PDO('dbconnection');
foreach ($dbh->query('SELECT * from table' ) as $row)
{
$instructor = ($_POST["name"]);
//Loads Certificate Template
$jpg_image = imagecreatefromjpeg('cert.jpg');
$color = imagecolorallocate($jpg_image, 82, 122, 124);
//Sets Font
$font_path = 'name.ttf';
//Calls first name from database
$firstname = $row['fname'];
//Makes entire first name lower case
$firstnamelower = strtolower($firstname);
$firstnameucfirst = ucfirst($firstnamelower);
//Calls Last Name From Database
$lastname = $row['lname'];
$lastnamelower = strtolower($lastname);
$lastnameucfirst = ucfirst($lastnamelower);
$name = $firstnameucfirst . ' ' . $lastnameucfirst;
$font_size = 75;
imagettftext($jpg_image, $font_size, 0, 810, 530, $color, $font_path, $name);
imagettftext($jpg_image, 40, 0, 375, 945, $color, $font_path, $instructor);
imagejpeg($jpg_image, 'images/' . $name . '.jpg', 50);
imagedestroy($jpg_image);
}
$dbh = null;
}
catch (PDOException $e)
{
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
echo "All certificates have been created."

?>

所以我尝试更改它,以便仅处理满足另一个要求的图像。基本上我需要使用 WHERE column_name 运算符值;但当我这样做时,我会遇到错误,我用谷歌搜索了一下,但我不太明白他们在说什么。使用 pdo 完成此操作是否还需要另一段代码?

这是我收到的错误警告:为 foreach() 提供的参数无效

最佳答案

该错误是由于您的 $pdo->query 在失败时返回 false 而不是空数组。

http://www.php.net/manual/en/pdo.query.php

foreach 需要一个数组。

调整您的代码:

$dbh = new PDO('dbconnection');

// Throw exceptions on errors
$dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);

// To make it easier to debug
$sql = 'SELECT * from table';

// To allow check for failuer
$rows = $dbh->execute($sql);

if ($rows === false) ... oopsie

foreach($rows as $row) ...

然后让您的查询发挥作用。考虑直接使用mysql来检查你的sql。

关于php - 基于其他行的 PDO 查询过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21358207/

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