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