Get information about your memory usage using PHP


Get information about your memory usage using PHP.
In order to optimize your scripts, you may definitely want to know how many amount of RAM they use on your server. This snippet will check memory and then print initial, final and peak usages.

PHP Code :-
<?php   echo "Initial: ".memory_get_usage()." bytes \n"; /* prints Initial: 361400 bytes */   // let's use up some memory for ($i = 0; $i < 100000; $i++) { $array []= md5($i); }   // let's remove half of the array for ($i = 0; $i < 100000; $i++) { unset($array[$i]); }   echo "Final: ".memory_get_usage()." bytes \n"; /* prints Final: 885912 bytes */   echo "Peak: ".memory_get_peak_usage()." bytes \n"; /* prints Peak: 13687072 bytes */   ?>

Optimize Code In Php(Code Optimization)

Select COUNT() will be faster than mysql_num_rows(), internally the server will process the request differently. mysql_query() transfers all result records from the MySQL into the php process before it returns. That alone would make the mysql_num_rows() version slower. Furthermore for some engines MySQL can serve a Count(*) request from the index of the table without hitting the actual data. A SELECT * FROM foo on the other hand results in a full table scan and MySQL has to read every single dataset 

<?php // The better way , it will be faster $sql = “select count(id) from user where email like’%gmail%’”; $res = mysql_query($sql) or die(mysql_error()); echo $value = mysql_fetch_row($res);   // it will be slower $sql = “select count(id) from user where email like’%gmail%’”; $res = mysql_query($sql) or die(mysql_error()); echo $value = mysql_num_rows($res);   ?>   <?php //Example ONE     for($i = 0; $i < count($myLargeArray); $i++ ) { echo myLargeArray[$i]; }   //Example TWO   $count = count($myLargeArray); for($i = 0; $i < $count; $i++ ) { echo myLargeArray[$i]; }   ?>

How to Import the contents of CSV files into MySQL Database using PHP


Step1: create a database with name test and create table user
Step2: Put a csv file (which you want to import) into your document
root.
Step3: Established database connection and first open the file in read
mode, using fgets () all data will be collected in an array and that
array will be stored in database.
Create user table:

CREATE TABLE IF NOT EXISTS `user` ( `ID` int(10) NOT NULL AUTO_INCREMENT, `name` varchar(50) NOT NULL, `email` varchar(50) NOT NULL, PRIMARY KEY (`ID`) )

Code For csv-import.php:

<?php   $connect = mysql_connect('hostname','username','password'); if (!$connect) { die('Could not connect to MySQL: ' . mysql_error()); }   $cid =mysql_select_db('test',$connect); // supply your database name   define('CSV_PATH','C:/xampp/htdocs/csvfile/'); // path where your CSV file is located   $csv_file = CSV_PATH . "infotuts.csv"; // Name of your CSV file $csvfile = fopen($csv_file, 'r'); $theData = fgets($csvfile); $i = 0; while (!feof($csvfile)) { $csv_data[] = fgets($csvfile, 1024); $csv_array = explode(",", $csv_data[$i]); $insert_csv = array(); $insert_csv['ID'] = $csv_array[0]; $insert_csv['name'] = $csv_array[1]; $insert_csv['email'] = $csv_array[2]; $query = "INSERT INTO csvdata(ID,name,email) VALUES('".$insert_csv[ID]."','".$insert_csv['name']."','".$insert_csv   ['email']."')"; $n=mysql_query($query, $connect ); $i++; } fclose($csvfile);   echo "File data successfully imported to database!!"; mysql_close($connect); ?>