SaeMysql操作示例 新浪sae的官方說(shuō)明文檔:http://apidoc.sinaapp.com/sae/SaeMysql.html Class SaeMysql 具體實(shí)現(xiàn):http://apidoc.sinaapp.com/__filesource/fsource_sae__saemysql.class.php.html 新浪官方文檔已經(jīng)給出了簡(jiǎn)單的示例: ?php$mysql = new Sae
SaeMysql操作示例新浪sae的官方說(shuō)明文檔:http://apidoc.sinaapp.com/sae/SaeMysql.html
Class SaeMysql 具體實(shí)現(xiàn):http://apidoc.sinaapp.com/__filesource/fsource_sae__saemysql.class.php.html
新浪官方文檔已經(jīng)給出了簡(jiǎn)單的示例:
getData( $sql ); $name = strip_tags( $_REQUEST['name'] ); $age = intval( $_REQUEST['age'] ); $sql = "INSERT INTO `user` ( `name` , `age` , `regtime` ) VALUES ( '" . $mysql->escape( $name ) . "' , '" . intval( $age ) . "' , NOW() ) "; $mysql->runSql( $sql ); if( $mysql->errno() != 0 ) { die( "Error:" . $mysql->errmsg() ); } $mysql->closeDb(); ?>
我們一般對(duì)于數(shù)據(jù)庫(kù)一般進(jìn)行增、刪、改、查的操作。
其中增加、刪除、修改操作都是執(zhí)行sql語(yǔ)句,然后根據(jù)錯(cuò)誤代碼看看是否正確執(zhí)行了就可以了;
對(duì)于查詢(xún)操作,一般分為兩類(lèi):
查詢(xún)得到一條記錄,常用在登陸的時(shí)候查詢(xún)用戶(hù)名和密碼的操作,SaeMysql類(lèi)中對(duì)應(yīng)方法getLine()返回的是一個(gè)一維數(shù)組;
查詢(xún)得到多條記錄,返回的結(jié)果是一個(gè)二維數(shù)組,需要使用兩層foreach來(lái)進(jìn)行輸出,廢話(huà)少說(shuō),上代碼:
runSql($sql); //執(zhí)行插入數(shù)據(jù)的操作 if($mysql->errno() != 0 ) { die( "Error:" . $mysql->errmsg() ); } else { echo "Data inserted successfully!
"; } //=============執(zhí)行取出數(shù)據(jù)的操作============================= //取出一行數(shù)據(jù) $sql = "SELECT * FROM `demo_table` WHERE `id`='1'"; $row = $mysql->getLine($sql); foreach ($row as $key=>$value) { echo $key."=>".$value."
"; } echo "
next demo"; //取出多行數(shù)據(jù) $sql = "SELECT * FROM `demo_table`"; $result = $mysql->getData($sql); foreach ($result as $row) { foreach ($row as $key=>$value) { echo $key."=>".$value."
"; } echo "===================
"; } $mysql->closeDb(); ?>
輸出結(jié)果:
id=>1
name=>qianshou
password=>12345
next demo
id=>1
name=>qianshou
password=>12345
===================
id=>2
name=>naruto
password=>abcde
===================
id=>3
name=>kakaxi
password=>asdfg
===================
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com