一、PHP使用MYSQL数据库方式
本例主要是用PHP连接MYSQL数据库。通过相关语句对SQL语句的执行,达到操作数据库的目的。
1、实例
- $link_id=mysql_connect($hostname,$username,$password);
- mysql_query("SET CHARACTER SET utf8");
- mysql_query("SET NAMES utf8");
- $result=mysql_select_db("xyymg");
- if( !$result )
- {
- echo "连接数据库出错!";
- exit();
- }
- $search_sql_0 = "select webPicId, webPicTitle, webPicTime, webPicPath from web_pictable where webPicSort = $t_index";
- $res_search_0 = @mysql_query("$search_sql_0");
- $num_records = @mysql_num_rows( $res_search_0 ); //记录行总数
- $search_sql_1 = $search_sql_0." order by webPicId desc limit $init_record,$num_to_show";
- $res_search_1 = mysql_query("$search_sql_1");
- $t_picList = array();
- if( $res_search_1 )
- {
- while( $r = mysql_fetch_row( $res_search_1 ) )
- {
- $t_picSaveSmallPath = "./".PIC_SAVE_SMALL_PATH.$r[3];
- $t_picList[] = array( 'id'=>$r[0], 'title'=>$r[1], 'ttime'=>timeToDate( $r[2] ), 'smallPicPath'=>$t_picSaveSmallPath );
- }
- }
2、连接数据库
- <?php
- $con = mysql_connect("localhost","peter","abc123");
- if (!$con)
- {
- die('Could not connect: ' . mysql_error());
- }
- // some code
- ?>
3、断开数据库
- <?php
- $con = mysql_connect("localhost","peter","abc123");
- if (!$con)
- {
- die('Could not connect: ' . mysql_error());
- }
- // some code
- mysql_close($con);
- ?>
4、用mysql_query() 执行SQL语句
5、从数据库表选取数据
- <?php
- $con = mysql_connect("localhost","peter","abc123");
- if (!$con)
- {
- die('Could not connect: ' . mysql_error());
- }
- mysql_select_db("my_db", $con);
- $result = mysql_query("SELECT * FROM person");
- while($row = mysql_fetch_array($result))
- {
- echo $row['FirstName'] . " " . $row['LastName'];
- echo "<br />";
- }
- mysql_close($con);
- ?>
6、ODBC实例
- <html>
- <body>
- <?php
- $conn=odbc_connect('northwind','','');
- if (!$conn)
- {exit("Connection Failed: " . $conn);}
- $sql="SELECT * FROM customers";
- $rs=odbc_exec($conn,$sql);
- if (!$rs)
- {exit("Error in SQL");}
- echo "<table><tr>";
- echo "<th>Companyname</th>";
- echo "<th>Contactname</th></tr>";
- while (odbc_fetch_row($rs))
- {
- $compname=odbc_result($rs,"CompanyName");
- $conname=odbc_result($rs,"ContactName");
- echo "<tr><td>$compname</td>";
- echo "<td>$conname</td></tr>";
- }
- odbc_close($conn);
- echo "</table>";
- ?>
- </body>
- </html>
二、VC连接数据库方式
本例使用VC连接Access数据库,连接方式为ADO
1、导入引用
- #import "c:\program files\common files\system\ado\msado15.dll"\
- no_namespace rename( "EOF", "adoEOF" )
2、初始化COM组建
- CDbOpe::CDbOpe()
- {
- CoInitialize(NULL);
- }
3、连接数据库
- //连数据库
- BOOL CDbOpe::LinkDb(CString db, CString usr, CString psw)
- {
- CString strDb;
- strDb.Format( _T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s"), db );
- m_pConnection.CreateInstance(__uuidof(Connection));
- try{
- m_pConnection->Open( (_bstr_t)strDb,(_bstr_t)usr,(_bstr_t)psw,adModeUnknown);
- }
- catch(_com_error e )
- {
- CString errMessage;
- errMessage.Format( _T("连接数据库失败!\r\n错误信息:%s"),(char*)e.Description());
- AfxMessageBox( errMessage );
- return FALSE;
- }
- return TRUE;
- }
4、执行SQL语句
- BOOL CDbOpe::OpenTable( CString sql )
- {
- m_pRecordset.CreateInstance(__uuidof(Recordset));
- // 在ADO操作中建议语句中要常用try...catch()来捕获错误信息,
- // 因为它有时会经常出现一些意想不到的错误。jingzhou xu
- try
- {
- m_pRecordset->Open( (_bstr_t)sql,// 查询DemoTable表中所有字段
- m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针
- adOpenDynamic,
- adLockOptimistic,
- adCmdText);
- }
- catch(_com_error *e)
- {
- //AfxMessageBox(e->ErrorMessage());
- return FALSE;
- }
- return TRUE;
- }
- //执行SQL语句
- BOOL CDbOpe::ExecutSql(CString sql)
- {
- _variant_t RecordsAffected;
- try
- {
- m_pConnection->Execute( (_bstr_t)sql, &RecordsAffected, adCmdText);
- return TRUE;
- }
- catch(_com_error e )
- {
- AfxMessageBox( _T("执行SQL语句错误!") );
- return FALSE;
- }
- }
5、关闭记录集
- //关闭记录集
- BOOL CDbOpe::CloseTable()
- {
- if( m_pRecordset->State)
- m_pRecordset->Close();
- m_pRecordset = NULL;
- return TRUE;
- }
6、关闭连接
- //关闭连接
- BOOL CDbOpe::CloseLink()
- {
- CloseTable();
- if(m_pConnection->State)
- m_pConnection->Close();
- m_pConnection = NULL;
- return TRUE;
- }
7、释放COM资源
- CoUninitialize();
8读取表数据相关代码
- //判断打开的记录集是否到了最后一行
- BOOL CDbOpe::IsTalbeEnd()
- {
- if( m_pRecordset->adoEOF )
- return TRUE;
- else
- return FALSE;
- }
- //向下一条移动
- BOOL CDbOpe::MoveNext()
- {
- if( !m_pRecordset->adoEOF )
- {
- m_pRecordset->MoveNext();
- return TRUE;
- }
- else
- {
- return FALSE;
- }
- }
- //取得字符串值
- CString CDbOpe::GetStringValue(CString colName)
- {
- CString strReturnValue = _T("");
- _variant_t var = m_pRecordset->GetCollect( (_bstr_t)colName );
- if(var.vt != VT_NULL)
- strReturnValue = (LPCSTR)_bstr_t(var);
- strReturnValue.TrimRight(); //移除右侧的空格
- return strReturnValue;
- }