从数据库读出数据,输出表格
<html><body>
<?php $connection = mysql_connect("localhost","root","");
$db = "test";
mysql_select_db($db,$connection) or die("不能打开数据库$db");
mysql_query("SET NAMES utf8");
$sql = " SELECT * FROM books LIMIT 0 , 30";
$result = mysql_query($sql,$connection) or die("SQL执行失败:$sql");
$num_result = mysql_num_rows($result);
echo "$num_result";
?>
<table cellspacing="2" cellpadding="6" align="center" border="1">
<tr>
<td colspan="7">
<h3 align="center">These books are currently available</h3></td></tr>
<tr>
<td align ="center">Title</td>
<td align="center">publisher</td>
<td align="center">category</td>
<td align="center">ISBN</td>
</tr>
<?php
for ($i=0;$i<$num_result;$i++)
{
$row=mysql_fetch_array($result);
$id=$row["bookid"];
echo "<tr>";
echo "<td>";
echo stripslashes($row["Title"]);
echo "</td><td>";
if ( !$row["publisher"] )
{
echo "盗版";
}
else
{
echo $row["publisher"];
}
echo "</td><td>";
echo $row["category"];
echo "</td><td>";
echo $row["ISBN"];
echo "</td>";
echo "</tr>";
}
?> </table>
</body>
</html>
循环写入数据mysql
<html><body>
<?php $connection = mysql_connect("localhost","root","");
$db = "test";
mysql_select_db($db,$connection) or die("cannot open the datebase db");
?>
<?php
for ($i=0;$i<10000;$i++)
{
mysql_query("SET NAMES utf8");
$sql = " INSERT INTO books VALUES('你好','试试','试试',$i) ";
$result = mysql_query($sql,$connection) or die("Could not execut sql:$sql");
if ("$result"==1) echo "$sql <br>";
}
?>
</body>
</html>
把文字转换为图片
<?php
$PHP_SELF = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
if (isset ($_GET['message'])){
//加载字体和图像,计算文本宽度
$font = 'c:/windows/fonts/simsun.ttc';
$size = 12;
$im = imagecreatefrompng('button.png');
$tsize = imagettfbbox($size, 0, $font, $_GET['message']);
//居中
$dx=abs($tsize[2]-$tsize[0]);
$dy=abs($tsize[5]-$tsize[3]);
$x=(imagesx($im)-$dx)/2;
$y=(imagesy($im)-$dy)/2+$dy;
//绘制文本
$black=imagecolorallocate($im,0,0,0);
$_GET['message'] = mb_convert_encoding($_GET['message'], "UTF-8");
imagettftext($im, $size, 0, $x, $y, $black, $font, $_GET['message']);
//返回图像
header('Content-type: image/png');
imagepng($im);
exit;
}
?>
<html><head><title>button Form</title></head>
<body><form action="<?php $PHP_SELF; ?>" method="GET">enter message to appear on button:
<input type="text" name="message" /><br/>
<input type="submit" value="Create Button" />
</form></body></html>
cli版本
#!E:\coreamp\server\php-5.3.5\php.exe -q
<?php
if ($argc !=3){
die("usage:button-cli filename message\n");
}
list(,$filename,$message)=$argv;
//加载字体和图像,计算文本宽度
$font='c:/windows/fonts/simsun.ttc';
$size=12;
$im=imagecreatefrompng('button.png');
$tsize=imagettfbbox($size,0,$font,$message);
//居中
$dx=abs($tsize[2]-$tsize[0]);
$dy=abs($tsize[5]-$tsize[3]);
$x=(imagesx($im)-$dx)/2;
$y=(imagesy($im)-$dy)/2+$dy;
//绘制文本
$black=imagecolorallocate($im, 0, 0, 0);
$message= mb_convert_encoding($message, "UTF-8");
imagettftext($im, $size, 0, $x, $y, $black, $font, $message);
//返回图像
imagepng($im,$filename);
?>
测试用的图片 button.png