[英]Generate a Certificate in PHP
我遇到了以下代碼的問題問題是當我運行此文件時,它會自動打印數據庫中的所有用戶我很難選擇一個用戶並只打印該用戶。
這里有人可以幫助我嗎?
$con=mysqli_connect('localhost','root','','mylib');//connect sa database
$query="select * from reguser" ;//select column
$fire=mysqli_query($con,$query);
while($row=mysqli_fetch_array($fire))
{
header('content-type:image/jpeg');
$font= realpath('arial.ttf');
$image=imagecreatefromjpeg("format.jpg");
$color=imagecolorallocate($image, 51, 51, 102);
$date=date('d F, Y');//Current Date
imagettftext($image, 18, 0, 880, 188, $color,$font, $date);
$name=$row['username'];
imagettftext($image, 48, 0, 120, 520, $color,$font, $name);
imagejpeg($image,"certificate/$name.jpg");//Storing certificate here
imagedestroy($image);
}
?>```
您正在通過執行Select * from reguser
查詢並使用 while 循環在循環內運行證書打印代碼。
相反,如果您只想為單個特定用戶打印它,請嘗試以下操作:
$sql = mysqli_query($con, select * from reguser where id=5);
如果您不想讓用戶獲得他們的 id,則可以用您喜歡的任何方式替換 where 子句的條件。
$user = mysqli_fetch_assoc($sql); // Get the required user like this:
// certificate printing code
header('content-type:image/jpeg');
$font= realpath('arial.ttf');
$image=imagecreatefromjpeg("format.jpg");
$color=imagecolorallocate($image, 51, 51, 102);
$date=date('d F, Y');//Current Date
imagettftext($image, 18, 0, 880, 188, $color,$font, $date);
$name=$user['username'];
imagettftext($image, 48, 0, 120, 520, $color,$font, $name);
imagejpeg($image,"certificate/$name.jpg");//Storing certificate here
imagedestroy($image);
這應該做到這一點!
您必須使用 WHERE 子句在 MySQL 查詢中指定要顯示的用戶。
WHERE 子句用於僅提取滿足指定條件的那些記錄。 更多細節
這將幫助您:
***
$user_id = "replace the user id here";
$query="select * from reguser WHERE id='".$user_id."'" ;//select column
***
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.