PHP MySQLデータベースdml文とdql文の操作
3869 ワード
データベース操作文は主にdml文とdql文に分けられる:データ操作文(dml)文:具体的にはUPDATE更新、INSERT挿入、DELETE削除を指す.データ問合せ文(dql)文:SELECTデータ問合せ言語を指すdml文を実行した場合bool値を返すdql文を実行した場合、問合せ結果$resを返し、$resから問合せ結果を取り出すには主に以下の4つの方法がある:mysql_fetch_row($res)、インデックスの配列を返します(推奨.)mysql_fetch_assoc($res)、関連配列mysqlを返します.fetch_array($res)、インデックス配列と関連配列(2つのセット)mysql_を返します.fetch_object($res)は、1行のデータを、1つのオブジェクトとして返す.
mysql_free_result()は、大きな結果セットを返すときにどれだけのメモリが消費されるかを考慮して呼び出す必要があります.スクリプトが終了すると、関連付けられたすべてのメモリが自動的に解放されます.
$row = mysql_fetch_assoc($result); mysql_free_result($result); $rowの後に使えばいいです
<?php
// test , , ,
$conn=mysql_connect("localhost","root","111111");
if(!$conn){
die(" !".mysql_error());
}
mysql_select_db("test",$conn) or die(" !".mysql_error());
mysql_query("set names utf8");
//$sql="insert into test(id,name,age,class,home,school) values(1,'qw',12,12,12,12)";
//$sql="delete from test where id=1";
$sql="update test set age=100 where id=2";
$sql_dql="select * from test";
// dml , bool
$res=mysql_query($sql,$conn);
if(!$res){
die(" ".mysql_error());
}
// ,
if(mysql_affected_rows($conn)>0){
echo " :".mysql_affected_rows($conn)." ";
}else{
echo " ";
}
// dql , $res, $res
$result = mysql_query($sql_dql,$conn);
if(!$result){
echo ' !'.mysql_error();
exit;
}
echo "<br/>";
//
// while($row = mysql_fetch_row($result)){
// echo $row[0]."--";
// echo $row[1]."--";
// echo $row[2]."--";
// echo $row[3]."--";
// echo $row[4]."--";
// echo $row[5]."--";
// echo "<br>";
// }
// echo " ".mysql_affected_rows($conn)." !";
//
// while($row = mysql_fetch_assoc($result)){
// echo $row['id']."--";
// echo $row['name']."--";
// echo $row['age']."--";
// echo $row['home']."--";
// echo $row['class']."--";
// echo $row['school']."--";
// echo "<br>";
// }
// echo " ".mysql_affected_rows($conn)." !";
// ( )
// while($row = mysql_fetch_array($result)){
// echo $row['id']."--";
// echo $row[0]."--";
// echo $row['name']."--";
// echo $row[1]."--";
// echo $row['age']."--";
// echo $row[2]."--";
// echo $row['home']."--";
// echo $row['class']."--";
// echo $row['school']."--";
// echo "<br>";
// }
// echo " ".mysql_affected_rows($conn)." !";
// , .
while($row = mysql_fetch_object($result)){
echo $row->id."--";
echo $row->name."--";
echo $row->age."--";
echo $row->home."--";
echo $row->class."--";
echo $row->school."--";
echo "<br>";
}
echo " ".mysql_affected_rows($conn)." !";
// , $result , , :
mysql_free_result($result);
// ,$conn ,
mysql_close($conn);
?>
<?php
$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
/* Use the result, assuming we're done with it afterwards */
$row = mysql_fetch_assoc($result);
/* Now we free up the result and continue on with our script */
mysql_free_result($result);
echo $row['id'];
echo $row['email'];
?>
mysql_free_result()は、大きな結果セットを返すときにどれだけのメモリが消費されるかを考慮して呼び出す必要があります.スクリプトが終了すると、関連付けられたすべてのメモリが自動的に解放されます.
$row = mysql_fetch_assoc($result); mysql_free_result($result); $rowの後に使えばいいです