<?php
//串連資料庫參數
$host="";
$user="";
$pass="";
$db="";
//建立串連
$connection=mysql_connect($host,$user,$pass) or die("unable to connect!");
mysql_select_db($db) or die("unable to select db!");//選擇資料庫
mysql_set_charset('utf8',$connection);
$query="";//sql語句
//執行sql語句
$resutl=mysql_query($query) or die ("Error in query :$query.".mysql_error());
//顯示返回的記錄
if(mysql_num_rows($result)>0){
while($row=mysql_fetch_row($result)){
//mysql_fetch_array()--->row[0] or $row['id'] ,mysql_fetch_assoc()-->$row['name'],mysql_fetch_object()--$row->id
echo $row[0];
echo $row[1];
echo $row[2];
}
}else{
echo "沒有相關記錄";
}
//釋放記錄所佔記憶體
mysql_free_result($result);
//關閉資料庫連接
mysql_close($connection);
list()方法
$sql="select id,title,thumb,url from `v9_news` where catid=9 ";
$result = mysqli_query($db_conn,$sql) or die("Err in query 3:".$sql.mysqli_error());;
while (list($id, $title, $thumb,$url) = mysqli_fetch_row($result)) {
echo " <tr>\n".
" <td><a href=\"info.php?id=$id\">$title</a></td>\n".
" <td>$thumb----$url</td>\n".
" </tr>\n";
}
list(), each(),reset(),current(),next(),end(),prev()
each()返回 array 數組中當前指標位置的鍵/值對並向前移動數組指標。索引值對被返回為四個單元的數組,鍵名為
0,1,key
和 value。單元 0 和
key 包含有數組單元的鍵名,1 和
value 包含有資料。
<?php$array = array('step one', 'step two', 'step three', 'step four');
// by default, the pointer is on the first element
echo current($array) . "<br />\n"; // "step one"
// skip two steps
next($array);
next($array);
echo current($array) . "<br />\n"; // "step three"
// reset pointer, start again on step one
reset($array);
echo current($array) . "<br />\n"; // "step one"
?>
//die() 和 exit(0)
?>
<?php
//一般處理方法
$db_host="localhost";
$db_user="root";
$db_pass="";
$db_name="feng";
$db_conn= mysqli_connect($db_host,$db_user,$db_pass,$db_name) or die ("dblink is bad !");
//mysqli_select_db($db_name) or die("unable to select db !");
$db_sql="select * from `newtable`";
$db_result=mysqli_query($db_conn,$db_sql) or die("Err in query:".mysqli_error());
if (mysqli_num_rows($db_result)>0)
{
echo "begin";
while($row=mysqli_fetch_row($db_result))
{
echo $row[0];
echo $row[1];
echo $row[3];
}
echo "end";
}
else
{
echo "no recoder";
}
mysqli_free_result($db_result);//釋放結果集
mysqli_close($db_conn);//釋放串連
//使用Binder 方法,出來提交資料,杜絕sql注入
$mysqli=new mysqli($db_host,$db_user,$db_pass,$db_name);
if (mysqli_connect_errno())
{
printf("dblink is bad ! %s/n",mysqli_connect_error());
exit();
}
$db_sql2="select id ,uid,regdate from `newtable` where id=?";
$stmt=$mysqli->prepare($db_sql2);//預先處理
$stmt->bind_param("i",$id);//綁定變數,限定變數格式【i 數值 ,s字元,d浮點型,b blobs型 】
if (!get_magic_quotes_gpc()) {
$id = addslashes($id=$_GET["id"]);//處理單引號問題
//$id = mysql_real_escape_string ($_GET["id"]);
/*addslashes的問題在於駭客可以用0xbf27來代替單引號,用於單位元組字串的處理
mysql_real_escape_string 必須在(PHP 4 >= 4.3.0 PHP 5)的情況下才能使用。否則只能用 mysql_escape_string ,兩者的區別是:
mysql_real_escape_string 考慮到串連的當前字元集,而mysql_escape_string 不考慮。
*/
} else {
$id=$_GET["id"];
}
//$id=$_GET["id"];
$stmt->execute();
$stmt->bind_result($col1,$col2,$col3);//綁定結果
while($stmt->fetch())
{
echo $col1."<br>";
echo $col2;
echo $col3;
}
echo $db_sql2;
$stmt->close();
$mysqli->close();
?>