Do you want to go to the database and check it again every time you refresh?

Source: Internet
Author: User
The page needs to use the total number of bars need to be shown on this page so this code can be written so that every time you refresh the page to go to the database inside the query it is not affect performance? What if the database has tens of thousands of data?

$pdo=new PDO("mysql:host=localhost;dbname=t1","root","");         $stmt=$pdo->prepare("select * from table1");         $stmt->execute();         $res=$stmt->fetchall(PDO::FETCH_ASSOC);         echo count($res);

Reply content:

The page needs to use the total number of bars need to be shown on this page so this code can be written so that every time you refresh the page to go to the database inside the query it is not affect performance? What if the database has tens of thousands of data?

$pdo=new PDO("mysql:host=localhost;dbname=t1","root","");         $stmt=$pdo->prepare("select * from table1");         $stmt->execute();         $res=$stmt->fetchall(PDO::FETCH_ASSOC);         echo count($res);

Why not?select count(*) from table

$stmt = $pdo->prepare ("SELECT count (*) as num from table");
$stmt->execute ();
$res = $stmt->fetch (PDO::FETCH_ASSOC);
return $res [' num ']

When the peak access is a bit of memory, you can consider the INSERT statement and the DEL statement when you put the data to the other place

You must be connected every time you write this, you can separate it. PDO encapsulates a class, the utility singleton pattern carries on the processing, avoids each time executes CURD the time to enjoy the connection operation. Examples I have written earlier:


  TRUE, Pdo::attr_errmode = pdo::errmode_exception, Pdo::mysql_attr_init_command =            > ' SET NAMES UTF8 ');        Return self:: $safepdo;        } catch (Exception $e) {throw $e; }}/** * description overwrite __clone () method, prohibit clone */Private Function __clone () {}/** * DESCRI Ption Singleton mode, instantiate call database link */public static function Calldb () {if (self:: $safepdo = = null) {self:: $s        Afepdo = self::p dolink ();    } return Self:: $safepdo; }}

Hope to help you

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.