重要的事情说三遍:代码复制完要格式化,格式化,格式化,最好重新抄一遍,小心陷阱
一、配置数据库
配置文件路径/data/common.inc.php
二、连接数据库
dirname(__FILE__)表示当前文件的绝对路径
如果在根目录下
<?php
require_once (dirname(__FILE__) . "/include/common.inc.php");
print_r($db);
如果在根目录下的子目录下
<?php
define('DEDEADMIN', preg_replace("/[/\\]{1,}/", '/', dirname(__FILE__) ) );
require_once(DEDEADMIN.'/include/common.inc.php');
print_r($db);
如果非上面两种情况,则需要在配置文件自定义根目录常量DEDEADMIN
<?php
define('DEDEADMIN', preg_replace("/[/\\]{1,}/", '/', 'E:/ixingmei3/ixingmeib2c/') );
require_once(DEDEADMIN.'include/common.inc.php');
三、增删改查
1.查询一条记录 GetOne()
<?php
require_once (dirname(__FILE__) . "/include/common.inc.php");
$res=$db->GetOne("SELECT * FROM cms_member limit 1");
var_dump($res);
2.查询多条记录 Execute()
<?php
require_once (dirname(__FILE__) . "/include/common.inc.php");
$sql="SELECT * FROM cms_member limit 10";
$db->Execute('me',$sql);
while($arr=$db->GetArray('me')){
echo "用户名={$arr['uname']},性别={$arr['sex']}";
}
//或
while($obj=$db->GetObject('me')){
echo "用户名={$obj->uname},性别={$obj->sex}";
}
3.插入、修改数据
<?php
require_once (dirname(__FILE__) . "/include/common.inc.php");
$sql="insert into cms_member (uname) values ('huahua')";
$db->ExecuteNoneQuery($sql);
$lastInsertID = $db->GetLastID();
echo $lastInsertID;
4.删除
<?php
require_once (dirname(__FILE__) . "/include/common.inc.php");
$sql = "DELETE FROM cms_member WHERE (mid=609690)";
$db->ExecuteNoneQuery($sql);