PHP faces the object database operation class, constructs a database operation class, encapsulates all database operations, can be extended to facilitate the use of background management programs, retrieves the number of database records, returns the number of database table fields and table field name sets, and finally does not Forgot to close the data path connection.
The first step is to download the PHP object database operation class library we need to use in this course: http://www.php.cn/xiazai/leiku/546
Second step, after the download is completed, find the php class file we need, unzip it to our local directory, and create a new php file!
The third step, after completion, we need to call this class in the new php file and instantiate the class:
<?php include_once "sql.php";//引入类文件 //使用时直接require该文件,然后实例化: $SqlDB = new MySQLDB("localhost","root","root","abcd"); $sql = "select * from t_table WHERE 1"; $result = $SqlDB->Query($sql);//查询 $rs = $SqlDB->getRows($result);//获得记录集 $num = $SqlDB->getRowsNum($result);//获得记录数 //...剩下的操作就是循环取值, for($i=0;$i<$num;$i++){ echo "sid:" .($rs[$i]["sid"])."<br>"; echo "aa:" .($rs[$i]["aa"])."<br>"; echo "bbc:" .($rs[$i]["bbc"])."<br>"; }
Run this file and get the result as shown below:
The above is the detailed content of Sharing of code examples for using PHP object to operate database. For more information, please follow other related articles on the PHP Chinese website!