2009年7月27日 星期一
2009年7月19日 星期日
用 php 寫 sqlite3
php 5.3.0 已開始支援 SQLite3:
$db=new sqlite3("my.sql3"); // use a database
if( $db )
{
$table="foo";
header("Content-Type: text/html;charset=big5");
$result=$db->query("Select * from $table");
$col=$result->numColumns();
$str="";
for($i=0;$i<$col;$i++) $str.=$result->columnName($i)." ";
echo "$str
";
while( $s1=$result->fetchArray() )
{
$str="";
for($i=0;$i<$col;$i++) $str.=$s1[$i]." ";
echo "$str
";
}
}
$db->close();
?>
如果要 delete, update, insert into, 等 SQL query, 可以執行 $db->exec($sql_query)
$db=new sqlite3("my.sql3"); // use a database
if( $db )
{
$table="foo";
header("Content-Type: text/html;charset=big5");
$result=$db->query("Select * from $table");
$col=$result->numColumns();
$str="";
for($i=0;$i<$col;$i++) $str.=$result->columnName($i)." ";
echo "$str
";
while( $s1=$result->fetchArray() )
{
$str="";
for($i=0;$i<$col;$i++) $str.=$s1[$i]." ";
echo "$str
";
}
}
$db->close();
?>
如果要 delete, update, insert into, 等 SQL query, 可以執行 $db->exec($sql_query)
2009年7月15日 星期三
用 php 寫一個簡單的 sqlite 資料庫
$newdb ="stock.sqlite";
$table ="ase";
$db=sqlite_open($newdb,0666,$sqlerr);
if( $db )
{
$Q1=sqlite_query($db,"select count(serial) from ase");
if ( !$Q1 )
{
echo "Create $table
";
sqlite_query($db,"create table $table (serial int not null,name char(255),primary key(serial))");
$Q1=sqlite_query($db,"select count(serial) from ase");
}
$record=sqlite_fetch_array($Q1);
$id=intval($record[0]);
if( $id==0 )
{ // no record, add some record
echo "Insert $table
";
$id++;sqlite_query($db,"insert into $table (serial,name) values ('$id','第一筆')");
$id++;sqlite_query($db,"insert into $table (serial,name) values ('$id','第二筆')");
$id++;sqlite_query($db,"insert into $table (serial,name) values ('$id','第三筆')");
$id++;sqlite_query($db,"insert into $table (serial,name) values ('$id','第四筆')");
$id++;sqlite_query($db,"insert into $table (serial,name) values ('$id','第五筆')");
$id++;sqlite_query($db,"insert into $table (serial,name) values ('$id','第六筆')");
}
sqlite_close($db);
}
fclose($fin);
?>
AMP for windows
懶得安裝各式各樣 Server, 及繁瑣設定, 又想學 php 語法, 可考慮下載 WAMP Server:
http://www.wampserver.com/en/download.php
Appache2, php , mysql 安裝包一次搞定, 如果 mysql 要使用 utf8
記得修改 my.ini 內 client 及 mysqld 內的設定 default-character-set = utf8
這樣灌完就可在 local 端 (http://127.0.0.1) 執行 server 的 php 程式了.
記得將 php 程式放於 apache2 server 目錄內 htdocs 下面.
如果需要特殊外掛(例如 php_mysql.dll, php_sqlite, php_sockets ...等),
記得修改 php.ini 將對應的 dll 註解移除
http://www.wampserver.com/en/download.php
Appache2, php , mysql 安裝包一次搞定, 如果 mysql 要使用 utf8
記得修改 my.ini 內 client 及 mysqld 內的設定 default-character-set = utf8
這樣灌完就可在 local 端 (http://127.0.0.1) 執行 server 的 php 程式了.
記得將 php 程式放於 apache2 server 目錄內 htdocs 下面.
如果需要特殊外掛(例如 php_mysql.dll, php_sqlite, php_sockets ...等),
記得修改 php.ini 將對應的 dll 註解移除
訂閱:
文章 (Atom)