Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 求问我自己写的这个DB类,哪里错了,插不进数据

求问我自己写的这个DB类,哪里错了,插不进数据

Jun 23, 2016 pm 02:05 PM

1

<?php    class DB{       private $hostname;      private $username;      private $password;      private $select_db;     private $con;       private $Error;             public function __construct($hostname,$username,$password,$select_db){          if(!empty($hostname)&&!empty($username)&&!empty($select_db))//检查参数是否为空,否则不赋值            {               $this->hostname = $hostname;             $this->username = $username;             $this->password = $password;             $this->select_db = $select_db;               $this->con = mysql_connect($this->hostname,$this->username,$this->password);                if(!$this->con){                 $this->Error = die('Could Not Connect:'.mysql_error);                }               else{                   mysql_select_db($this->select_db,$this->con);             }           }       }       public function __destruct(){//退出时结束连接          mysql_close($this->con);     }       public function insert($table,$body){//插入table中的一个数组            $line1 = implode(',',$body);            $line2 = implode(',',array_keys($body));            echo $sql = "INSERT INTO $table ($line2) VALUES ($line1)";          $result = mysql_query($sql,$this->con);          if(!$result){               echo $this->Error;               echo '111';         }                   }       public function update($table,$body){               }       public function read($table,$keyword){                  }       public function delete($table,$keyword){                }       public function getLastError(){//返回最后一条错误信息         return $this->Error;     }   }?>

Copy after login

主页调用的是

1

<?phprequire('DB.class.php');    $DB = new DB('localhost','root','','dbtest');   $line1 = array(         'aa'=>'`aa`',            'bb'=>'`dd`' );  $DB->insert('1234',$line1);?>

Copy after login

echo $sql语句是INSERT INTO 1234 (aa,bb) VALUES (`aa`,`dd`)


回复讨论(解决方案)

用mysql_error查询$this->con,没有返回错误信息。。。。我现在很抓狂- -求人解答啊。。。。

require('DB.class.php');
    $DB = new DB('localhost','root','','dbtest');
    $line1 = array(
            'aa'=> "'aa'",
            'bb'=> "'dd'"
    );
    $DB->insert('1234',$line1);

你的$line1数组写反了。
而且insert 语句还有一种格式:insert into tb_member set username = "test", type = 1, lastlogindt = now()。跟update样式差不多。
你也可以看看人家写的数据库类,我感觉挺好的:http://www.cnblogs.com/hooray/archive/2012/07/21/2603017.html

你的$line1数组写反了。
而且insert 语句还有一种格式:insert into tb_member set username = "test", type = 1, lastlogindt = now()。跟update样式差不多。
你也可以看看人家写的数据库类,我感觉挺好的:http://www.cnblogs.com/hooray/archive/201…… 其实没写反- -三楼指出来了。。。都是些小符号的问题。。。。。

require('DB.class.php');
    $DB = new DB('localhost','root','','dbtest');
    $line1 = array(
            'aa'=> "'aa'",
            'bb'=> "'dd'"
    );
    $DB->insert('1234',$li…… 唉- -总是些奇奇怪怪的小错误。。。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles