亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

正在回答

3 回答

重新寫,我現在用的也是PHP7.0的版本,不支持mysql擴展,只有重新寫,自己定義業務邏輯,當然了,只要你能看懂這個老師的代碼,自己寫也應該不會太難的

0 回復 有任何疑惑可以回復我~

問下樓上 $str == null 報變量未定義錯誤 怎么處理?

0 回復 有任何疑惑可以回復我~
#1

星雨星辰

在if上面加上$str="";
2019-03-09 回復 有任何疑惑可以回復我~

用pdo吧? 目前基本都是pdo 會比較方便


<?php

/* 鏈接數據庫 */
function connet() {
?? ?try {?? ??? ?
?? ??? ?$link = new PDO ( PDO_HOST, DB_USER, DB_PWD );
?? ??? ?$link->setAttribute ( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
?? ??? ?/* $link->exec ( "set names" . DB_CHARSET ); */
?? ?} catch ( PDOException $e ) {
?? ??? ?echo '數據庫連接失敗' . $e->getMessage ();
?? ?}
?? ?return $link;
}

/* 插入數據 */
function insert($table, $array) {
?? ?try {
?? ??? ?$conn = connet ();
?? ??? ?$key = join ( ",", array_keys ( $array ) );
?? ??? ?$vals = "'" . join ( "','", array_values ( $array ) ) . "'";
?? ??? ?$sql = "insert into {$table} ({$key}) values({$vals})";
?? ??? ?$result = $conn->exec ( $sql );
?? ??? ?return $conn->lastInsertId ();
?? ?} catch ( PDOException $e ) {
?? ??? ?echo '插入失敗' . $e->getMessage ();
?? ?}
}

/* 更新數據 */
function update($table, $array, $where = null) {
?? ?try {
?? ??? ?$conn = connet ();
?? ??? ?foreach ( $array as $key => $val ) {
?? ??? ??? ?if ($str == null) {
?? ??? ??? ??? ?$sep = "";
?? ??? ??? ?} else {
?? ??? ??? ??? ?$sep = ",";
?? ??? ??? ?}
?? ??? ??? ?$str .= $sep . $key . "='" . $val . "'";
?? ??? ?}
?? ??? ?$sql = "update {$table} set {$str}" . ($where == null ? null : " where ". $where);
?? ??? ?$result = $conn->exec ( $sql );
?? ??? ?if ($result) {
?? ??? ??? ?return $result;
?? ??? ?} else {
?? ??? ??? ?return false;
?? ??? ?}
?? ?} catch ( PDOException $e ) {
?? ??? ?echo '更新數據失敗' . $e->getMessage ();
?? ?}
}

/* 刪除數據 */
function delete($table, $where = null) {
?? ?try {
?? ??? ?$conn = connet ();
?? ??? ?$where = $where == null ? null : " where " . $where;
?? ??? ?$sql = "delete from {$table} {$where}";
?? ??? ?$result = $conn->exec ( $sql );
?? ??? ?return $result;
?? ?} catch ( PDOException $e ) {
?? ??? ?echo '刪除數據失敗' . $e->getMessage ();
?? ?}
}

/* 得到指定一條記錄 */
function fetchOne($sql, $result_type = PDO::FETCH_ASSOC) {
?? ?try {
?? ??? ?$conn=connet();
?? ??? ?$result=$conn->query($sql);
?? ??? ?$row=$result->fetch($result_type);
?? ??? ?return $row;
?? ?} catch ( PDOException $e ) {
?? ??? ?echo '得到指定一條記錄失敗' . $e->getMessage ();
?? ?}
}

/* 得到所有結果 */
function fetchAll($sql, $result_type = PDO::FETCH_ASSOC) {
?? ?try {
?? ??? ?$conn = connet ();
?? ??? ?$result = $conn->query ( $sql );
?? ??? ?
?? ??? ?while ( @$row = $result->fetch($result_type)) {
?? ??? ??? ?$rows [] = $row;
?? ??? ?}
?? ??? ?return $rows;
?? ?} catch ( PDOException $e ) {
?? ??? ?echo '得到所有結果失敗' . $e->getMessage ();
?? ?}
}
/* 得到所有結果條數 */
function getResultNum($sql) {
?? ?try {
?? ??? ?$conn = connet ();
?? ??? ?$result = $conn->query ( $sql );
?? ??? ?$row_count = $result->rowCount ();
?? ??? ?return $row_count;
?? ?} catch ( PDOException $e ) {
?? ??? ?echo '得到所有結果條數失敗' . $e->getMessage ();
?? ?}
}

/**
?* 得到上一步插入記錄的ID號
?* @return number
?*/
function getInsertId(){
?? ?try {
?? ??? ?$conn = connet ();
?? ??? ?return $conn->lastInsertId();
?? ?} catch (PDOException $e) {
?? ??? ?echo '得到上一步插入記錄的ID號失敗' . $e->getMessage ();
?? ?}

}
?>

2 回復 有任何疑惑可以回復我~
#1

星雨星辰

感謝大佬
2019-03-09 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
手把手教你實現電商網站后臺開發
  • 參與學習       117251    人
  • 解答問題       2101    個

手把手教你用PHP搭建電子商務平臺,由淺入深教你搭建電商系統

進入課程

mysql怎么轉換成mysqli,求代碼

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號