Count the number of MySQL Queries
How to count the number of MySQL queries used by your script
One way of optimizing your scripts that connect to MySQL databases is to determine how many queries to the database your script makes. If you would like to count the number of queries, you can write a script that will do so. You use this function in place of the regular mysql_query() function.
PHP Code:
<?php
function cnt_mysql_query($sql=FALSE)
{
static $queries = 0;
if (!$sql)
return $queries;
$queries ++;
return mysql_query($sql);
}
$sql= "SELECT 8+9";
In PHP3 you could access HTTP_GET, HTTP_POST, HTTP_COOKIE and HTTP_POST_FILES values directly as globals. This made scripts very easy to code but was bad security. Starting with PHP 4.2.0. the 'register_globals' setting is off by default. The GET, POST, COOKIE, e.t.c. variables will no longer be directly available as global variables. Instead there are now arrays of variables that are 'superglobal', or automatic global variables available in all scopes throughout a script. You can access it within functions or methods, without having to declare it a global variable.
Transfer Values Between Pages using Javascript
(Written by: geeeet -> Visit Homepage)
With this little piece of code, it's possible to transfer values between pages, just like if you were using a serverside language, like php, jsp or asp.
HTML Code:
// This function extracts the values from the url
function getValues(){
var urlEnd = document.URL.indexOf('?');
var values = new Array();
var names;
if (urlEnd != -1){
var params = document.URL.substring(urlEnd+1, document.URL.length).split('&');
Form Validation
Validating a form
You can perform form validation before a form is submitted via JavaScript or after the form is submitted via PHP.
I've actually already demonstrated simple validation using PHP in Form Processing with PHP: Part 1, with the input.php3 script. After the form is submitted, the script checks to see whether a variable does or does not exist.
PHP Code:
<?php
//check if submit button clicked and the input field isn't empty, print hello
if ($submit == "click" && $UserName !=""):
echo "Hello, $UserName";
Form Processing with PHP: Part 3
How to populate form fields with previously submitted values.
When you see the scripts in action, take a look at the source code, especially what the values of the form fields are set to before and after the scripts execute.
Input field:
To automatically have the value of an input field filled out, use the echo/print function to place the value submitted into the value attribute of the field.
PHP Code:
<?php
if ($submit && $firstname){
$fname = $firstname;
?>
What is your first name?
Form Processing with PHP Part 2
Radio buttons, checkboxes, select lists
Radio Buttons:
<input type="radio" name="radioset" value="data" CHECKED> data
VALUE: Sets the value of the form element.
RADIOSET links a group of radio buttons together, only one radio per set can be checked.
RADIOSET is alsothe name of the variable that will contain the value submitted by the form.
CHECKED is an optional attribute that makes a radio button active by default.
If the value is not set, "on" is the default value submitted. You can't tell which button in the set was chosen.
Form Processing with PHP: Part 1
Form Processing
Forms are usually used to gather information from visitors to your website. Once you've gathered the information, it can be used in several ways. It can be sent via email for someone to read, saved to database, or used to find data in the database that matches a specific criteria.
For example: