<?php
$variable = $_POST['variable']; //Post从表单中提取变量
if(!$variable) //如果变量为空,输出错误信息并退出
{
echo 'You hava not entered search details.Please go back and try again.';
exit;
}
if(!get_magic_quotes_gpc()) //该函数用于判断get_magic_quotes_gpc是否开启,get_magic_quotes_gpc参数是用于确定是否将从post,get,cookie过来的数据增加转义字符和从数据库出来的数据去掉转义字符
{
$variable = addlashes($variable);//在特殊文本字符前增加转义字符
//stripslashes()用于在去掉转义字符
}
$localhost = 'hostname';//主机名
$user = 'username';//用户名
$pwd = 'password';//密码
$db = 'databasename';//
@$link = new mysqli($localhost,$user,$pwd,$db);//连接数据库
if(mysqli_connect_errno())//如果数据库连接失败,输出错误信息并退出
{
echo 'Error: Coulid not connect to database. Please try again later.';
exit;
}
$query = "SELECT row from table where some situation";//查询语句
$result = $link -> query($query);//查询并返回结果
$num_results = $result -> num_rows; //结果行数
echo "<p>Number of row found: ". $num_results ."</p>";//输出行数
for($i = 0;$i < $num_results;$i++)//循环输出每组元素
{
$row = $result -> fetch_assoc();//提取元素,一次一行,fetch_assoc()提取出的元素,有属性以及值
echo stripslashes($row['attributename']);//按属性(键)提取值
echo "<br/>";
}
$result -> free();//释放内存
$link -> close();//断开数据库连接
?>
php中mysqli用法举例
最新推荐文章于 2022-08-31 14:51:33 发布