Questions #Tagged [php]

Questions: 6

How to get num_rows value from result set of sql query in php?

In PHP mysqli_num_rows() function is an inbuilt function which is used to return the number of rows present in the result set. It is generally used to check if data is present in the database or not. Below is the syntax for this function:<br> <pre><xmp>mysqli_num_rows ( $result );</xmp></pre>

 

#php     #mysql     #sql    

What is the significance of QSA in .htaccess file?

QSA appends to the internal scripting query string, and makes it the Last rewrite rule executed.<br> <pre><xmp>Example of .htaccess file: RewriteRule ^folder/([a-zA-Z-]*).html /folder/ab.php?id=$1 [QSA,L]</xmp></pre>

 

#php     #.htaccess    

How to sort Multi-dimensional Array using one of its parameter(dimension) in PHP?

Using below logic this multidimentional array can be sorted using parameter 1 which is $arr_count.<br> <pre><xmp>Code: $result = array_map(null, $arr_name, $arr_count,$arr_desc); $keys = array_column($result, '1'); //Here 1 means it will sort basis count, 0 means it will sort based on arr_name, 2 means it will sort based on arr_desc array_multisort($keys, SORT_DESC, $result); print_r($result);</xmp></pre> <pre><xmp>Result: Array ( [0] => Array ( [0] => c [1] => 8 [2] => z ) [1] => Array ( [0] => a [1] => 5 [2] => x ) [2] => Array ( [0] => b [1] => 3 [2] => y ) )</xmp></pre>

 

#php     #array     #multidimensional-array     #sorting    

How to create array of arrays using array_map() in php?

We can create array of arrays using array_map() function in PHP. To do this we have to pass null as parameter in place of functionName parameter and the list of arrays to create an array of arrays.<br> <pre><xmp><?php $a = array(1, 2, 3); $b = array("e", "f", "g"); $result = array_map(null, $a, $b); print_r($result); ?> </xmp></pre> <pre><xmp> Output: Array ( [0] => Array ( [0] => 1 [1] => e ) [1] => Array ( [0] => 2 [1] => f ) [2] => Array ( [0] => 3 [1] => g ) ) </xmp></pre>

 

#php     #array     #multidimensional-array    

How to display HTML tags as plain text without conversion in php?

By default in php we don't convert html tags to plain text. Lets understand this more with below example:<br><br> <pre>echo("<b>abc</b>")</pre> <p>Output:<br> <b>abc</b><br><br> In php we have special function htmlspecialchars() which can be used to display display HTML tags as plain text. Below is the syntax of this function:<br> string htmlspecialchars( $string, $flags, $encoding, $double_encode )<br> &#9679 <b>$string</b>: This parameter is used to hold the input string.<br> &#9679 <b>$flags</b>: This parameter is used to hold the flags. It is combination of one or two flags, which tells how to handle quotes.<br> &#9679 <b>$encoding</b>: It is an optional argument which specifies the encoding which is used when characters are converted. If encoding is not given then it is converted according to PHP default version.<br> &#9679 <b>$double_encode</b>: If double_encode is turned off then PHP will not encode existing HTML entities. The default is to convert everything.<br><br>Now by using this function below will be the output:</p> <pre>echo(htmlspecialchars("<b>abc</b>",ENT_QUOTES) Output: <b>abc</b> </pre>

 

#php     #html    

What is .htaccess file? And in .htaccess how to rewrite url folder/file.php to folder/file?

An .htaccess file is a directory-level configuration file supported by several web servers. It is used for configuration of website-access issues, such as URL redirection, URL shortening, access control, customized error responses, cache control and more. <br> When a .htaccess file is placed in a directory which is in turn 'loaded via the Apache Web Server', then the .htaccess file is detected and executed by the Apache Web Server software. <br> Following code need to be written in .htaccess file to rewrite url folder/file.php to folder/file: <br><br> <pre> RewriteEngine On<br> RewriteRule ^folder/([^/]*)$ folder/file.php [L] </pre> <br><br> <p>Below is another variation where we can pass parameter in the url:</p><br><br> <pre>RewriteRule ^folder/file/([^/]*)$ folder/file.php?name=$1 [L]</pre><br><br> <p>The output of the above url will be something like this if the parameter we are passing in name as file-1:<br> websitename/folder/file/file-1</p>

 

#php     #.htaccess     #apache