php获取mysql数据菜鸟_PHP操作数据库
function?con(){
//?MYSQL連接參數
$dsn='mysql:host=127.0.0.1;dbname=hello;charset=utf8;port=3306;';
$dbname="root";
$dbpwd="root";
try?{
$p?=?new?PDO($dsn,$dbname,$dbpwd);
}?catch?(PDOException?$e)?{
print_r('連接未成功,錯誤信息:'.$e);
exit;
}
return?$p;
}
//?查詢數據
function?selectData($sql){
//?方法一讀取數據
$p=con();
//?$res=$p->query($sql);
//?$arr?=?[];
//?foreach?($res?as?$k=>$row)?{
//??$arr[]?=?$row;
//?}
//?return?$arr;
//?$p="";
//?方法二讀取數據
//?$a=$p->prepare($sql);
//?$a->execute();
//?$a->setFetchMode(PDO::FETCH_ASSOC);
//?$res=$a->fetchAll();
//?return?$res;
//?方法三讀取數據
//?$id=6;
//?$a=$p->prepare($sql);
//?$a->bindValue("id",$id,PDO::PARAM_INT);
//?$a->bindParam("id",$id,PDO::PARAM_INT);
//?$a->setFetchMode(PDO::FETCH_ASSOC);
//?$a->execute();
//?return?$a->fetchAll();
}
//?增刪改方法
function?dmlData($sql){
$p=con();
$a=$p->prepare($sql);
if($a->execute()){
if($a->rowCount()){
return?'寫入成功';
}else{
return?'沒有影響行數';
}
}else{
return?"寫入不成功";
}
}
//?查詢方法調用
//?print_r(selectData('select?*?from?ck?where?id=:id'));
//?增加方法調用
//?print_r(dmlData("INSERT?INTO?ck?(name,age)?VALUES?('小芳',1000)"));
//?刪除方法調用
//?print_r(dmlData("DELETE?FROM?ck?where?id=8"));
//?修改方法調用
print_r(dmlData("UPDATE?ck?SET?name='修改的名稱'?where?id=7"));
?>
總結
以上是生活随笔為你收集整理的php获取mysql数据菜鸟_PHP操作数据库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ajax请求挂起_Ajax请求内部循环挂
- 下一篇: python调用golang 数组_Go