您好,欢迎访问一九零五行业门户网

刚转学php,请大家帮小弟我写段数据库查询类的代码

刚转学php,请大家帮我写段数据库查询类的代码
大家好,本人初转php,想建立一个pdo连接的数据库操作类,但是不知道php的调用方法,请大家指点下。
类文件coon.php内容:
query(set names utf8);
$dbh->exec(set names utf8);
}
catch(exception $e)
{
echo 'data error: '.$e->getmessage();
}
}
//关闭数据库连接
public function sql_close()
{
$dbh=null;
}
//查询数据库
public function my_query($sql)
{
$this->sql_open();
//$this->sth = $this->dbh->query($sql);
//$result = $this->sth->fetchall();
//return $result;
这里面怎么写才能返回数据?
}
}
?>

页面调用代码:
my_query('select * from user_type order by user_id') as $row)
{
//这里循环输出查询内容
}
$mysql->sql_close();
?>

望详细指点下,实在弄不明白,用asp或asp.net的函数调用方法全不行,我想写一个查询,插入,更新和删除数据的通用函数,外部只要带入sql语句就可以执行的那种。
------解决方案--------------------
$this->dbh = new pdo($dsn, $user, $pass);
你的my_query看上去没错,将注释的那几行打开。先试试看。
------解决方案--------------------
你现在这个不能防注入,防注入需要用pdo::prepare
prepare('select name, colour, calories
from fruit
where calories execute(array(150, 'red'));
$red = $sth->fetchall();
$sth->execute(array(175, 'yellow'));
$yellow = $sth->fetchall();
?>

其它类似信息

推荐信息