What to do with depreciated functions

If PHP returns messages saying that functions have depreciated, this is a warning to indicate that the code that you are running has functions in your installed version of PHP that will be removed at some time in the future by a later version of PHP.

This may occur if one day your web host suddenly decides to upgrade their servers to a new version of PHP.  You should make sure that you are on the mailing lists for your hosting company so that you will be notified of changes that may cause issues.

Depreciated functions still exist and you get warnings. So they work as expected.  However in a future version of PHP, they might disappear.  It’s a way to signal changes to users which have code based on an older PHP version.

Normally the deprecated features get removed after some time, but it’s not predictable how long this takes.

So if you see these warnings, update the code. Most often the PHP documentation has more information why something has been deprecated and what to do. Most often it’s an improvement.

Changing to a new version of PHP will mean that you will have access to the latest features of the language, however, it may mean that older versions of an application will either fail or display warning messages.

You have to decide if you want your application to operate over different PHP platforms – if you do then you have to code with common functions that are in all versions of PHP.

Comparing two columns using SQL

If you have two tables which have columns with should contain the same data, and you want to check that the columns are the same, then it is possible compare the two columns using SQL.

If table1 has a column called users and table2 has a column called username, then to compare those two columns use something like :

SELECT users FROM table1 WHERE username NOT IN (SELECT username from table2)

A simple Javascript Submit and Validation example

The following HTML script illustrates a simple Javascript validation and submit script that checks inputs on the PC before it is submitted to the server.  You might use this method on a web site before submission to the PHP script.  The Javascript displays a nice error message on submission if there are any errors and it is quite easy to add more tests when other text boxes are added.

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Submit a form using Javascript with input validation</title>

<script language=”javascript” type=”text/javascript”>
<!–

//——————————————–
//Javascript that will submit the form when all entries are valid
function button_onclick(frmCreate) {

var themessage = “Error in entries : “;
var isOK = true;

if ( frmCreate.txtItem_Number.value == “” ) {
themessage = themessage + “\n” + “* Enter PayPal item code”;
isOK = false;
}

if ( frmCreate.txtItem_Name.value == “” ) {
themessage = themessage + “\n” + “* Enter PayPal item name”;
isOK = false;
}

if ( isOK == false ) {
window.alert(themessage);
} else {
frmCreate.submit(frmCreate);
}

}

//——————————————–
//Checks if the entry is numeric
function isNumeric(inputVal) {
numeric = true;
inputStr = inputVal.toString();
for (var i = 0; i< inputStr.length; i++) {
var oneChar = inputStr.charAt(i);

if (oneChar < “0” || oneChar > “9”) {
return false;
}

}

if (numeric == false) {
return false; }
else {
return true; }

}

//——————————————–
function LowerLimit_onblur(frmCreate) {
if ( !isNumeric( frmCreate.txtLowerLimit.value ) ) {
window.alert(“Enter a integer number”);
frmCreate.txtLowerLimit.value = “”;
frmCreate.txtLowerLimit.focus();
}
}

//–>
</script>

</head>
<body>

<!– START OF MAIN FORM –>
<form name=”frmCreate” method=”post” action=”test.html”>

<input type=”button” name=”Create” value=”Create” language=”javascript” onClick=”return button_onclick(frmCreate)” />

<br/><br/>
<table>
<tr>
<td>
<strong>Product Item Reference</strong>
</td>

<td>
<input type=”text” name=”txtItem_Number” />
</td>
<tr>

<tr>
<td>
<strong>Product Item Name</strong>
</td>

<td>
<input type=”text” name=”txtItem_Name” size=”50″ /><br/>
</td>
</tr>

<tr>
<td>
<strong>Enter lower limit</strong>
</td>
<td>
<input type=”text” name=”txtLowerLimit”  value=”5″ size=”3″  LANGUAGE=”javascript” onBlur=”return LowerLimit_onblur(frmCreate)” />
</td>

</table>
</form>
<!– END OF MAIN FORM –>

</body>
</html>

PHP File Handling Tutorial

PHP File handling Tutorial

The kinds of things that you will do with files include:

•    Reading and writing text in files.
•    Creating and deleting directories and file manipulation.

Part 1 – File permissions on the server

If you have to write data to a text file on a web server, or you have to copy, rename, delete or do any kind of file manipulation, the file or folder will require correct permissions.

To change file or folder permissions in Dreamweaver, open the site up in the remote view and right click on the file or folder.  Select permissions and then choose the kind of permissions that you want.  This would be 666 if you want to write and manipulate files.

This may not be necessary, as it may already be set on your web server.

Part 2 – Working with files

The typical way in which you will work with a text file is something like:

•    Open the file that you are going to work with by associating a file handle
•    Read or write to the file using the file handle
•    Close the file using the file handle.

Task 1 – Opening a file

(1)   In your text editor create a new PHP web page called file.php.  If you are using Dreamweaver, remove all the html code from the page.

(2)  Enter the following:

<?php

//file.php
$filename = “data.txt”;

if  (  !$fp = fopen($filename, “r”) )
{
die (“cannot open file $filename”);
}

fclose($fp);

?>

The $fp is the file pointer which is used for reading and writing operations.

(3)  Upload the file to your web space and run it in your web browser and see what happens.

(4)   Create a text file called data.txt and upload it to the web space and run file.php in your browser again.

The fopen() function

fopen() takes two arguments.  The first argument is the file name.  The file name can be relative to the location of this PHP file, or absolute with the full path name of the file in the form “/myfiles/www/data.txt” or it can be a url or an ftp path name to a different server.

The second argument is the mode which can be:

r   Read only access
r+ Read and write access
w Write access only
w+ Read and write access, any existing data will be lost if the file does not exit and PHP will attempt to create the file
a Appending only.  Write to the end of the file, if the file does not exist then PHP will attempt to create the file
a+ Open for reading and appending. Data is written to the end of the file, if the file does not exist, then PHP will attempt to create it.

Task 2 – Writing to the file

(1)  In file.php, change the mode from r to w+

(2)  Before the fclose() line enter the following:

(3)  fwrite($fp, “this is some text);

(4)  Your file will now look like:

<?php

//file.php

$filename = “data.txt”;

if  (  !$fp = fopen($filename, “w+”) )
{
die (“cannot open file $data.txt”);
}

fwrite($fp, “this is some text”);

fclose($fp);

?>

(5)  Upload the file to your web server, and run file.php in your web browser.  When you examine the data.txt file you should see the extra text.

Task 3 – Reading from the file

There are a number of ways of reading the contents of file.  One method is to read a block of characters into an array until the end of the file is reached

while ( !feof($fp) ) {
$lines[] = fgets($fp, 1024);
}

fgets($fp, 1024) means to read a line with up to 1024 bytes or until an end of line is found.

(1) Modify file.php with the above code added and an extra bit of code to display the text in the array.

The file.php code will now look like:

<?php

//file.php

$filename = “data.txt”;

if  (  !$fp = fopen(“data.txt”, “w+”) )
{
die (“cannot open file $data.txt”);
}

fwrite($fp, “this is some text\r\n”);
fwrite($fp, “this is some more text\r\n”);
fwrite($fp, “this is even more text\r\n”);

while ( !feof($fp) ) {
$lines[] = fgets($fp, 1024);
}

fclose($fp);

foreach ($lines as $key => $value) {
echo($value . “<br/>”);
}
?>

(2) Upload the file to the web server and run it in your web browser.  It should display the text that has been entered into the file.
Part 3 – File and folder manipulation

There are a number of PHP functions that can be used to work with files and folders.

Some of these functions may need permissions to be modified on the server to allow.

copy() copy a file
rename() rename a file
file_exists() shows if the file exists
basename() returns the base part of a file name

A few interesting mySQL functions

You can see the process list, find out who is doing what and kill a process if needed.

If you log into PHPmyAdmin and enter the following into the SQL text box :

show processlist;

You will see a list of the processes.

If you are using the mysql command line environment then you can do it at the command line as well

mysql> show processlist;

Once you know the process ID you can then kill the process using the ID number as follows :

mysql>kill 349

Here are some further functions that may be of interest :

show status;

show table status like ‘%’;

The above gives you create time and other information.

All can be run from within PHPmyAdmin or from the mysql command line.

An INSERT INTO with other calculations

This mySQL “Insert from another table with select” example shows how you can concatenate two of the selected fields into one inserted field.

In this example we are inserting data into tbllistings using data from tblrawdata but we are doing other processing on fields before it is inserted.  Obviously you can use any of the other mySQL select string, arithmetic and mathematical operators and functions when you do your select statement and then insert those into the new table.

INSERT INTO tbllistings (
property_id,
agent_id,
property_address,
property_borough,
.. etc
)
SELECT
Prop_ID,
concat(property_id, agent_id),
prop_name,
prop_street,
.. etc
FROM tblrawdata;