User Tools

Site Tools


php:php

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
php:php [2015/08/04 07:57] – [config debug mode in PHP] adminphp:php [2019/02/20 06:10] – [Convert from mysql to mysqli] admin
Line 4: Line 4:
   * Eclipse pdt: [[php:eclipsepdt|Eclipse PDT(PHP Development Tool)]]   * Eclipse pdt: [[php:eclipsepdt|Eclipse PDT(PHP Development Tool)]]
 ===== Create PHP simple code and run ===== ===== Create PHP simple code and run =====
-==== create helloworld.php ====+==== Create helloworld.php ====
 <code php> <code php>
 <?php <?php
Line 10: Line 10:
 ?> ?>
 </code> </code>
-==== create phpinfo.php =====+==== Create phpinfo.php =====
 <code php> <code php>
 <?php <?php
Line 16: Line 16:
 ?> ?>
 </code> </code>
-==== run PHP code ====+==== Run PHP code ====
   * linux: <code bash>   * linux: <code bash>
 php helloworld.php php helloworld.php
Line 24: Line 24:
 </code> </code>
 ===== How to debug in PHP and Javascript ===== ===== How to debug in PHP and Javascript =====
-==== config debug mode in PHP ====+==== Config debug mode in PHP ====
 check http://192.168.2.11/info.php(To run with debug mode, **remove option --disable-debug** when build PHP from source). In php.ini, we need turn on **log_errors = On** for production and development and where to save log file<code> check http://192.168.2.11/info.php(To run with debug mode, **remove option --disable-debug** when build PHP from source). In php.ini, we need turn on **log_errors = On** for production and development and where to save log file<code>
 error_log = /var/log/php_errors.log error_log = /var/log/php_errors.log
 +</code>To be can save log to php_errors.log, you must change own to allow http write log:<code bash>
 +chown www.www /var/log/php_errors.log
 </code> </code>
 Config turn on/off debug log in **php**: Config turn on/off debug log in **php**:
Line 41: Line 43:
 track_errors = On track_errors = On
 </code> </code>
-Config turn on debug log in **php-fpm**: +  * Explain config:display_errors = On => Allow display debug information in front end 
-<code>+Config turn on debug log in **ngix with php-fpm backend**: 
 +  * in php-fpm.conf<code>
 [global] [global]
 error_log = /var/log/php-fpm.log error_log = /var/log/php-fpm.log
Line 48: Line 51:
 [www] [www]
 catch_workers_output = yes catch_workers_output = yes
 +</code>
 +  * in php.ini:<code ini>
 +display_errors = On ⇒ Allow display debug information in front end
 </code> </code>
 Config turn on/off log in httpd: Config turn on/off log in httpd:
Line 64: Line 70:
 LogLevel error LogLevel error
 </code> </code>
-==== config in web server to display error log ====+==== Config in web server to display error log ====
   * in nginx<code>   * in nginx<code>
 ###demo.vn ###demo.vn
Line 107: Line 113:
 </VirtualHost> </VirtualHost>
 </code> => error log: ErrorLog "logs/demo-error.log" </code> => error log: ErrorLog "logs/demo-error.log"
-==== debug with php code ====+==== Debug with php code ====
   * Debug with print<code php>   * Debug with print<code php>
 print(variable_name) print(variable_name)
Line 125: Line 131:
 ?> ?>
 </code> </code>
-==== debug with xdebug ====+==== Debug with xdebug ====
 Refer: [[php:xdebug|Debug PHP with xdebug]] Refer: [[php:xdebug|Debug PHP with xdebug]]
-==== Query log of mysql server ==== +
-[windows] +
-  log = "C:/genquery.log" +
-[linux] +
-[mysqld] +
-  log=/var/log/mysqld.general.log +
-<code bash> +
-touch /var/log/mysqld.general.log +
-chown mysql.mysql /var/log/mysqld.general.log +
-SET GLOBAL general_log = 'ON' +
-/etc/init.d/mysqld restart +
-</code>+
 ==== Debug with Javascript ==== ==== Debug with Javascript ====
   * Debug with Internet Explorer: Turn on Debug Mode in Internet Options for debugging code javascript error   * Debug with Internet Explorer: Turn on Debug Mode in Internet Options for debugging code javascript error
   * Debug in Chrome: Using Developer Tools for debugging Javascript with break points   * Debug in Chrome: Using Developer Tools for debugging Javascript with break points
 +===== PHPUnit =====
 +refer: https://phpunit.de/getting-started/phpunit-5.html
 +==== Install PHPUnit ====
 +  * Install as binary<code bash>
 +wget -O phpunit https://phar.phpunit.de/phpunit-5.phar
 +chmod +x phpunit
 +./phpunit --version
 +</code>
 +  * Install with composer:<code bash>
 +composer require --dev phpunit/phpunit ^5
 +./vendor/bin/phpunit --version
 +</code>
 +==== Run PHPUnit ====
 +===== PHP Slow Log Config =====
 +==== Config Slow Log in php-fpm ====
 +<code>
 +request_slowlog_timeout = 5s
 +slowlog = /var/log/php-fpm/slowlog-site.log
 +</code>
 +==== Config Slow Log for php ====
 ===== Basic Syntaxs ===== ===== Basic Syntaxs =====
 ==== PHP tags ==== ==== PHP tags ====
Line 332: Line 347:
 if(!mysql_connect("localhost", "bob", "secret")) //tries to connect to the database server if(!mysql_connect("localhost", "bob", "secret")) //tries to connect to the database server
 die("Unable to connect to the database server!"); //terminates the script, and outputs the error die("Unable to connect to the database server!"); //terminates the script, and outputs the error
-</code>  +</code> 
 +==== PHP mysql ==== 
 +<code php> 
 +'connect'            => 'mysql_connect', 
 +'pconnect'           => 'mysql_pconnect', 
 +'select_db'          => 'mysql_select_db', 
 +'query'              => 'mysql_query', 
 +'query_unbuffered'   => 'mysql_unbuffered_query', 
 +'fetch_row'          => 'mysql_fetch_row', 
 +'fetch_array'        => 'mysql_fetch_array', 
 +'fetch_field'        => 'mysql_fetch_field', 
 +'free_result'        => 'mysql_free_result', 
 +'data_seek'          => 'mysql_data_seek', 
 +'error'              => 'mysql_error', 
 +'errno'              => 'mysql_errno', 
 +'affected_rows'      => 'mysql_affected_rows', 
 +'num_rows'           => 'mysql_num_rows', 
 +'num_fields'         => 'mysql_num_fields', 
 +'field_name'         => 'mysql_field_name', 
 +'insert_id'          => 'mysql_insert_id', 
 +'escape_string'      => 'mysql_escape_string', 
 +'real_escape_string' => 'mysql_real_escape_string', 
 +'close'              => 'mysql_close', 
 +'client_encoding'    => 'mysql_client_encoding', 
 +'ping'               => 'mysql_ping', 
 +</code> 
 +==== PHP mysqli ==== 
 +<code php> 
 +'connect'            => 'mysqli_real_connect', 
 +'pconnect'           => 'mysqli_real_connect', // mysqli doesn't support persistent connections THANK YOU! 
 +'select_db'          => 'mysqli_select_db', 
 +'query'              => 'mysqli_query', 
 +'query_unbuffered'   => 'mysqli_unbuffered_query', 
 +'fetch_row'          => 'mysqli_fetch_row', 
 +'fetch_array'        => 'mysqli_fetch_array', 
 +'fetch_field'        => 'mysqli_fetch_field', 
 +'free_result'        => 'mysqli_free_result', 
 +'data_seek'          => 'mysqli_data_seek', 
 +'error'              => 'mysqli_error', 
 +'errno'              => 'mysqli_errno', 
 +'affected_rows'      => 'mysqli_affected_rows', 
 +'num_rows'           => 'mysqli_num_rows', 
 +'num_fields'         => 'mysqli_num_fields', 
 +'field_name'         => 'mysqli_field_tell', 
 +'insert_id'          => 'mysqli_insert_id', 
 +'escape_string'      => 'mysqli_real_escape_string', 
 +'real_escape_string' => 'mysqli_real_escape_string', 
 +'close'              => 'mysqli_close', 
 +'client_encoding'    => 'mysqli_client_encoding', 
 +'ping'               => 'mysqli_ping', 
 +</code> 
 +==== Convert from mysql to mysqli ==== 
 +Using tool: https://github.com/philip/MySQLConverterTool 
 +<code bash> 
 +php MySQLConverterTool-master/cli.php -f changeusername.php > changeusername_new.php 
 +</code> 
 +And Modify some missing in tool:<code php> 
 +mysqli_connect("127.0.0.1", "forum_ttb", "xxxxxxxxxx")); 
 +</code>to<code php> 
 +=> mysqli_connect("127.0.0.1", "forum_ttb", "xxxxxxxxxx","forum_ttb")); 
 +</code> 
 +===== PHP curl ===== 
 +<code php> 
 +<?php 
 +function curl_post($url, $vars=array(), $second=10) 
 +
 +    try{ 
 +        $ch = curl_init(); 
 +        if ($ch === false) { 
 +            throw new Exception('failed to initialize'); 
 +        } 
 +        curl_setopt($ch,CURLOPT_TIMEOUT,$second); 
 +        curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1); 
 +        curl_setopt($ch,CURLOPT_URL,$url); 
 +        curl_setopt($ch,CURLOPT_POST, 1); 
 +        curl_setopt($ch,CURLOPT_POSTFIELDS,$vars); 
 +        $data = curl_exec($ch); 
 +        if ($data === false) { 
 +            throw new Exception(curl_error($ch), curl_errno($ch)); 
 +        } 
 +        curl_close($ch); 
 +    }catch(Exception $e){ 
 +        echo "Curl Error: " . $e->getCode . $e->getMessage(); 
 +    } 
 +    if($data) 
 +        return $data; 
 +    else 
 +        return false; 
 +
 +</code>
php/php.txt · Last modified: 2022/10/29 16:15 by 127.0.0.1