首页 »PHP教程 » php入门:PHP大型Web应用入门(十一) »正文
php入门:PHP大型Web应用入门(十一)
来源: 发布时间:星期一, 2009年1月12日 浏览:34次 评论:0
//后续 Cache 目 是可以脱离数据库 所以用什么语言查询生成缓存Cache都没关系 PHP直接连接操作MySQL 效率是很好 C并不是很适合做Web 虽然有过很多人用C做CGI 它 好处是可以跑多线 可以底层地处理数据 可以做算法 缺点是开发效率太低 至于C语言连接MySQL 你会发现它和PHP如此相象 本来就是用 同样 mysql.h... Just Like this: <?php //Includes... #<windows.h> #<MySQLmysql.h> #<stdlib.h> #<stdio.h> #<conio.h> //Databasename... charg_szHost="localhost"; UINTg_unPort=MYSQL_PORT; charg_szUserName="charlieface"; charg_szPassword="pudgy"; charg_szDatabase="Candy"; charg_szSQLStatement="SELECT*chocolates"; //Entrypo... (nArguments,char*pszArguments) { //Variables... MYSQL*myDatabase=NULL; MYSQL_RES*myResult=NULL; MYSQL_FIELD*myField=NULL; MYSQL_ROWmyRow=NULL; UINTunRecords=0; UINTunFields=0; UINTunIndex=0; UINTunFieldIndex=0; //InitializeMySQL... myDatabase=mysql_init(NULL); //Failed... (!myDatabase) { //Alertuser... prf("]Error:UnabletoinitializeMySQLAPI...n"); //Cleanup,abort,terminate... mysql_close(myDatabase); getch; 0; } //Connecttoserverandcheckforerror... (mysql_real_connect(myDatabase,g_szHost,g_szUserName,g_szPassword, NULL,g_unPort,NULL,0)!=0) { //Alertuser... prf("]Error:Unabletoconnecttoserver...n"); //Cleanup,abort,terminate... mysql_close(myDatabase); getch; 0; } //Selectdatabaseinserverandcheckforerror... (mysql_select_db(myDatabase,g_szDatabase)<0) { //Alertuser... prf("]Error:Unabletoselectdatabase...n"); //Cleanup,abort,terminate... mysql_close(myDatabase); getch; 0; } //Querydatabaseandcheckforerror... (mysql_query(myDatabase,g_szSQLStatement)!=0) { //Alertuser... prf("]Error:Unabletoexecutequery...n"); //Cleanup,abort,terminate... mysql_close(myDatabase); getch; 0; } //Retrievequeryresultfromserver... myResult=mysql_store_result(myDatabase); //Failed... (!myResult) { //Alertuser... prf("]Error:Unabletoretrieveresult...n"); //Cleanup,abort,terminate... mysql_close(myDatabase); getch; 0; } //Howmanyrecordswereedheresult? //Calculate... unRecords=mysql_num_rows(myResult); //Alertuser... prf("]Query:%drecordsfound...n",unRecords); //Howmanyfieldsarepresentinarecord? //Calculate... unFields=mysql_num_fields(myResult); //Alertuser... prf("]Query:Thereare%dfieldsineachrecord...",unFields); //Outputrecords... for(unIndex=0;unIndex<unRecords;unIndex) { //Fetchrowfromresults... myRow=mysql_fetch_row(myResult); //Fetchfieldsfromrow... myField=mysql_fetch_fields(myResult); //Showrecord... prf("]Record:%d/%dn",unIndex,unRecords); //Outputallfieldshisrow... for(unFieldIndex=0;unFieldIndex<unFields;unFieldIndex) { //Output... prf("t%s",myField[unFieldIndex].name); } } //Freeresult... mysql_free_result(myResult); //Closeserverconnection... mysql_close(myDatabase); myDatabase=NULL; //Alertuser,exit... prf("]Done,pressanykeytoexit...n"); getch; 0; } ?>
这是Dev-Cpp Example 看过的后 发现这几个 ~呵呵 如果你再看看PHP里mysql扩展 源码 更会发现 它到C原始lib 转换是如此直接
相关文章
读者评论
发表评论
|
|