ex">
时间:2021-07-01 10:21:17 帮助过:4人阅读
class my_sql
{
public $dbh;
public $sth;
//连接数据库
public function sql_open()
{
$host='localhost'; //数据库主机名
$dbName='lif2'; //使用的数据库
$user='root'; //数据库连接用户名
$pass='123456'; //对应的密码
$dsn='mysql:host='.$host.';dbname='.$dbName;
try
{
$dbh = new PDO($dsn, $user, $pass);
$dbh->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;
这里面怎么写才能返回数据?
}
}
?>
输出查询内容
include 'conn.php';
$mysql = new my_sql;
foreach ($mysql->my_query('SELECT * FROM user_type order by user_id') as $row)
{
//这里循环
/* Execute a prepared statement by passing an array of values */
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < ? AND colour = ?');
$sth->execute(array(150, 'red'));
$red = $sth->fetchAll();
$sth->execute(array(175, 'yellow'));
$yellow = $sth->fetchAll();
?>