<?php
$conn=mysql_connect("localhost","root","")
$select=mysql_select_db("books",$conn)
$query="insert into computers(name,price,publish_data) "
$query.="values('JSP',28.00,'2008-11-1')"
$query="select * from computers"
$result=mysql_query($query)
//以下是使用mysql_result()函数来获取到查询结果
$num=mysql_num_rows($result)
for($rows_count=0$rows_count<$num$rows_count++){
echo "书名:".mysql_result($result,$rows_count,"name")
echo "价格:".mysql_result($result,$rows_count,"price")
echo "出版日期:".mysql_result($result,$rows_count,"publish_data")."<br>"
}
//以下是使用mysql_fetch_row()函数来获取到查询结果
while($row=mysql_fetch_row($result))
{
echo "书号:".$row[0]."<br>"
echo "书名:".$row[1]."<br>"
echo "价格:".$row[2]."<br>"
echo "出版日期:".$row[3]."<br>"
echo "<br>"
}
//以下是使用mysql_fetch_array()函数来获取到查询结果
while($row=mysql_fetch_array($result))
{
echo "书号:".$row[0]."<br>"
echo "书名:".$row[1]."<br>"
echo "价格:".$row["price"]."<br>"
echo "出版日期:".$row["publish_data"]."<br>"
echo "<br>"
}
//以下是使用mysql_fetch_object()函数来获取到查询结果
while($row=mysql_fetch_object($result))
{
echo "书号:".$row->id."<br>"
echo "书名:".$row->name."<br>"
echo "价格:".$row->price."<br>"
echo "出版日期:".$row->publish_data."<br>"
echo "<br>"
}
?>
/*** MySQL 随机取记录
*
* @param $t 表
* @param $c ID列,默认为id
* @param $n 取多少个
* @param $w 条件语句
* @param $f bool 是否强制以多维数组形式返回,默认false
* @return array 取1个直接返回结果数组(除非$f为true),取>1个返回多维数组,用foreach取出
*/
function rand_row($t , $c = 'id' , $n = '1', $w = '' , $f = false) {
$m=new mysqli(mysqli信息,自行查找php文档)
if (!empty($w)) {
$w = ' AND '.$w
}
$sql = "SELECT * FROM `{$t}` WHERE {$c} >= (SELECT floor(RAND() * (SELECT MAX({$c}) FROM `{$t}`))) {$w} ORDER BY {$c} LIMIT {$n}"
$xq = $m->query($sql)
$r = array()
while ($x = $m->fetch_array($xq)) {
$r[] = $x
}
if ($f == false &&count($r) == 1) {
return $r[0]
} else {
return $r
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)