課程
/后端開發
/PHP
/手把手教你實現電商網站后臺開發
急求電商網站一模一樣的代碼
2016-12-21
源自:手把手教你實現電商網站后臺開發 2-2
正在回答
重新寫,我現在用的也是PHP7.0的版本,不支持mysql擴展,只有重新寫,自己定義業務邏輯,當然了,只要你能看懂這個老師的代碼,自己寫也應該不會太難的
問下樓上 $str == null 報變量未定義錯誤 怎么處理?
星雨星辰
用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 ();?? ?}}?>
舉報
手把手教你用PHP搭建電子商務平臺,由淺入深教你搭建電商系統
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2017-06-28
重新寫,我現在用的也是PHP7.0的版本,不支持mysql擴展,只有重新寫,自己定義業務邏輯,當然了,只要你能看懂這個老師的代碼,自己寫也應該不會太難的
2016-12-25
問下樓上 $str == null 報變量未定義錯誤 怎么處理?
2016-12-22
用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 ();
?? ?}
}
?>