专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »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-CppExample看过的后发现这几个~呵呵

  如果你再看看PHP里mysql扩展源码更会发现它到C原始lib转换是如此直接



0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: