简体   繁体   English

PHP减少数据库上的重复连接

[英]PHP reduce duplicity connection on database

i am using 3 different functions that connect on my MySQL 1th one - Check if there is any result in database for string 2th one - Display some values based on searched string 3th one - Insert some info about search to logs 我正在使用3个不同的函数连接到我的MySQL第1个-检查数据库中是否有字符串第2个第1个结果-根据搜索到的字符串第3个显示一些值-将一些有关搜索的信息插入日志

So in general First 2 seartch same table and make duplicity , and 3th one connect on same database as well just different table. 因此,通常前2个Seartch都在同一张表上进行重复,而第3个Seartch则在同一数据库上也只是不同的表上进行连接。 I really start thinking that its not the way my script should work. 我真的开始认为这不是我的脚本应该工作的方式。 Because tables are pretty big and it takea time for script to search it. 因为表非常大,脚本需要花费一些时间来搜索它。 So if i make duplicity it take just 2x longer what is pointless, can somebody give me some advises how can i edit it to make it faster or avoid this without security issues? 因此,如果我做重复,只花了2倍多的时间,那是没有意义的,有人可以给我一些建议吗,我该如何编辑它以使其更快或更避免安全问题呢?

In general i create 2x connection on datbase: 通常,我在datbase上创建2x连接:

data.php data.php

 public function fetchByVinEvidenceDetail($con) {
     $success = false;
     try{
        $sql = "SELECT * FROM evidence_calculations WHERE vin = :vin LIMIT 5";
        $stmt = $con->prepare( $sql );
        $stmt->bindValue( "vin", $this->vin, PDO::PARAM_STR );
        $stmt->execute();
        echo $this->vin;
            while ($row = $stmt->fetch()){
                echo "<tr>";
                echo "<br>#4";
                echo "<br>".$row['street'];
                echo "<br>".$row['city'];
                echo "<br>".$row['claim_number'];
                echo "<br>".$row['company'];
                echo "<br>".$row['country'];
                echo "<br>".$row['other'];
                echo "<br>".$row['parts'];
                echo "<br>".$row['labor'];
                echo "<br>".$row['calculation_start'];
                echo "<br> -------------------------------- <br>";
            }
            }catch(PDOExeption $e){
            echo $e->getMessage();
            echo $con->errorInfo();
            }

         return $success; 
     }                   

     public function ExistVinEvidence() {
     $success = false;
     try{
        $con = new PDO( DB_HOST, DB_USER, DB_PASS ); 
        $con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
        $sql = "SELECT * FROM evidence_calculations WHERE vin = :vin LIMIT 30";
        $stmt = $con->prepare( $sql );
        $stmt->bindValue( "vin", $this->vin, PDO::PARAM_STR );
        $stmt->execute();
        $count = $stmt->rowCount();
        if ($count>0){
            echo 'For VIN:<b>'.$this->vin.'</b> we found '.$count.' rows in our far history database.</a> <br>';
        }
        else
        {
             echo 'We are sorry but we didnt find any data for VIN <b> '.$this->vin.' in far history database</b> <br>';
        }
        }catch(PDOExeption $e){
         echo $e->getMessage();
         echo $con->errorInfo();
        }
 }

 public function searchLog() {
    $correct = false;
        try {
            $date = new DateTime();
            $con = new PDO( DB_HOST, DB_USER, DB_PASS );
            $con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
            $sql = "INSERT INTO search_log(vin, date, user, ip, search_directory) VALUES(:vin, :date, :user, :ip, :search_directory)";

            $stmt = $con->prepare( $sql );
            $stmt->bindValue( "vin", $this->vin, PDO::PARAM_STR );
            $stmt->bindValue( "date", $date->getTimestamp(), PDO::PARAM_STR );
            $stmt->bindValue( "user", $this->getUsername(), PDO::PARAM_STR );
            $stmt->bindValue( "ip", $this->getUserIP(), PDO::PARAM_STR );
            $stmt->bindValue( "search_directory", "1", PDO::PARAM_STR );
            $stmt->execute();
            return "Logged";
        }catch( PDOException $e ) {
            return $e->getMessage();
        }
 }

sql.php (which i include_once to data.php) sql.php(我在data.php中include_once)

  $con = new PDO( DB_HOST, DB_USER, DB_PASS ); 
        $con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

index.php 的index.php

                    $data = new Data;
                    $data->storeFormValues( $_POST );
                    $data->fetchByVinEvidence();
                    $data->ExistVinEvidence();

and it throw error UNDEFINED $con 并抛出错误UNDEFINED $ con

Open one time the connection to the database, and pass the $con parameter to your functions: 打开一次与数据库的连接,然后将$con参数传递给您的函数:

$con = new PDO( DB_HOST, DB_USER, DB_PASS );
$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

searchLog($con);
fetchByVinEvidenceDetail($con);
ExistVinEvidence($con);

And searchLog : searchLog

public function searchLog($con) {
    $correct = false;
    try {
        $date = new DateTime();
        $sql = "INSERT INTO search_log(vin, date, user, ip, search_directory) VALUES(:vin, :date, :user, :ip, :search_directory)";

        $stmt = $con->prepare( $sql );
        ...

You will want to use the Singleton pattern to return a single instance of your PDO object throughout your application. 您将需要使用Singleton模式在整个应用程序中返回PDO对象的单个实例。

see : http://en.wikipedia.org/wiki/Singleton_pattern 参见: http : //en.wikipedia.org/wiki/Singleton_pattern

Link to PHP implementation of singleton pattern: 链接到单例模式的PHP实现:

http://www.phptherightway.com/pages/Design-Patterns.html http://www.phptherightway.com/pages/Design-Patterns.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM