Developers Archive for the 'php programming' Category

Migrating From MySQL To PostgreSQL In PHP

Migrating From MySQL To PostgreSQL In PHP Tuesday, February 12th, 2008

Connecting to a database:

Old function: mysql_connect($server, $username, $password)
New function: pg_connect($connectString)

Connects to the database. Note that pg_connect() takes a single connection string, instead of separate arguments for each connection parameter.

Querying a database:

Old function: mysql_query($query)
New function: pg_query($query) or pg_query($link, $query)

This returns a result resource if the query was executed, or false if the query was invalid.

Counting the number of rows:

To count the number of rows retrieved by a select statement:

Old function: mysql_num_rows($result)
New function: pg_num_rows($result)

To count the number of rows affected by an insert, update or delete:

Old function: mysql_affected_rows($link)
New function: pg_affected_rows($result)

Note that MySQL took in (optionally) the link identifier, whereas PostgreSQL takes in the result resource.

Extracting data:

There are a number of different functions that can be used to extract rows from a result set. Each of these functions return the next row, and automatically advance the internal row pointer. If there are no rows returned, false is returned.

Old function: mysql_fetch_row($result)
New function: pg_fetch_row($result)

Old function: mysql_fetch_array($result)
New function: pg_fetch_array($result)

Old function: mysql_fetch_object($result)
New function: pg_fetch_object($result)

Fetching last ID:

Old function: mysql_insert_id()
New function: pg_last_oid()

Note that the MySQL function returns the last value inserted into a field with auto_increment set, whereas PostgreSQL returns an OID of the last record inserted.

Fetching errors:

Old function: mysql_error()
New functions: pg_last_error($conn), pg_result_error($result)

This can be slightly tricky, as pg_last_error() returns the last error for the given connection, whereas pg_result_error() returns the error for the given result – although if a query fails, the result is false! The manual page for pg_result_error discusses how to overcome this, although realistically, pg_last_error should suffice generally. Generally speaking, a select query should never return any errors. The only situations where errors might occur is if constraints are violated with inserts, updates or deletes.

Disconnecting from a database:

Note that this is not usually necessary, as connections are closed automatically at the end of a script’s execution.

Old function: mysql_close($link)
New function: pg_close($link)

Putting it all together:
Every row that exists in a PostgreSQL database has an OID column associated with it. When you normally select data from a table (e.g. select * from myTable) you won’t see this field

Using serial is basically a shortcut for the server to create an integer column, a sequence and a trigger. A sequence is a basically a function that returns an incremented number each time you call it, while a trigger is a function that is executed every time data is inserted or updated (in this case only inserts are relevant).

Finally, to fetch a newly inserted serial from a PostgreSQL table from PHP, you firstly need to call pg_last_oid(). This returns an OID (see the previous chapter), and you then need to select the serial column using the OID. It’s a little bit longer to do than in MySQL, but you’re probably using some kind of database abstraction anyway, into which you could automate this into a single function call.

Online Thumbnail Creator using GD function :

Online Thumbnail Creator using GD function : Wednesday, May 9th, 2007

The first step in our online thumbnail creator is to create a form, and make it possible to upload an image. See the example below:

<?php

if ($_POST) {
// No image?
if (empty($_FILES[’image’]) OR $_FILES[’image’][’error’] != UPLOAD_ERR_OK) {
die (’<strong>Invalid image uploaded. Please go back and try again.</strong>’);
}

$imagepath = $_FILES[’image’][’tmp_name’];

// Load image
$image = open_image($imagepath);

if ($image == false) {
die (’<strong>You uploaded an invalid image. Please go back and try again.</strong>’);
}

// TODO: resizing the image

}

// Display the upload form:
?>
<html>
<head>
<title>Image Resizer</title>

<style type=”text/css”>
th { text-align: right; }
</style>

</head>

<body>
<form method=”POST” enctype=”multipart/form-data”>
<table>
<tr>
<th>Image:</th>
<td><input type=”file” name=”image”></td>
</tr>

<tr>
<th>Resize by: </th>
<td><input type=”text” name=”percent” size=”1″ />% (percentage)</td>
</tr>

<tr>
<th>OR new width: </th>
<td><input type=”text” name=”new_width” size=”1″ /> pixels (height will be calculated automatically)</td>
</tr>

<tr>
<th>OR new height: </th>
<td><input type=”text” name=”new_height” size=”1″ /> pixels (width will be calculated automatically)</td>
</tr>

<tr>
<th>OR new height and new width: </th>
<td>
<table>
<tr><td>width:</td><td><input type=”text” name=”width” size=”1″ /> pixels</td></tr>
<tr><td>height:</td><td><input type=”text” name=”height” size=”1″ /> pixels</td></tr>
</table>
</td>
</tr>

<tr><td colspan=”2″ style=”text-align:center;”><input type=”submit” value=”Resize Image” /></td></tr>
</form>
</body>
</html>

Now that we have a form to upload the image and are able to specify how we want to resize it, we will have to write the actual code for resizing.

// Percentage?
if (!empty($_POST[’percent’]) AND is_numeric($_POST[’percent’])) {
$percent = floatval($_POST[’percent’]);
$percent = $percent/100;

$new_width = $width * $percent;
$new_height = $height * $percent;

// New width? Calculate new height
} elseif (!empty($_POST[’new_width’]) AND is_numeric($_POST[’new_width’])) {
$new_width = floatval($_POST[’new_width’]);
$new_height = $height * ($new_width/$width);

// New height? Calculate new width
} elseif (!empty($_POST[’new_height’]) AND is_numeric($_POST[’new_height’])) {
$new_height = floatval($_POST[’new_height’]);
$new_width = $width * ($new_height/$height);

// New height and new width
} elseif (!empty($_POST[’height’]) AND is_numeric($_POST[’height’]) AND !empty($_POST[’width’]) AND is_numeric($_POST[’width’])) {
$new_height = floatval($_POST[’height’]);
$new_width = floatval($_POST[’width’]);
} else {
die (’<strong>You didn\’t specify any resizing options.</strong>’);
}

In above example, we use a simple if-then-else structure to check all the resizing options.

All that’s left to do is resize the image, and then display it, using the following standard code:

// Resample
$image_resized = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Display resized image
header(’Content-type: image/jpeg’);
imagejpeg($image_resized);
die();

We’ve now got a fully functional online image resizer, which can be used to resize almost any kind of image.

Use PHP to Create Dynamic pdf Files

Use PHP to Create Dynamic pdf Files Wednesday, May 2nd, 2007

Firstly let’s find the extension that is needed for this exercise. If you installed php on a windows platform, then the php_pdf.dll, on my computer it is located at C:\PHP\php-4.3.1-Win32\extensions. If you are using a linux platform, believe you’ll need to download this dll file.

Now, in your php.ini file remove the “;” character in front of the extension=php_pdf.dll line. Since we updated the ini file, we must restart the apache web server so that the web server can reinitialize the php with the php_pdf library included.

Create a php file with the following code,

<html>
<head>
<title>Name Entry Level</title>
</head>
<body>
<table border=”0″ width=”100%” height=”100%”>
<tr>
<td valign=”middle” align=”center”>
<form method=”POST” action=”pdf_file.php”>
<p>
<font size=”3″ face=”Arial”>
Enter your name here please =>
<input type=”text” name=”userName” size=”20″ maxlength=”20″>
</font>
</p>
<p>
<input type=”submit” value=”Submit” name=”B1″>
<input type=”reset” value=”Reset” name=”B2″>
</p>
</form>
</td>
</tr>
</table>
</body>
</html>

Now we will grab the post information. That is, the page we take the user information ($user) will be added to the hyperlink and pdf file.

$user = $HTTP_POST_VARS[”userName”];

Now, we are ready to start. Firstly let’s create a blank pdf file, say bennyboy.pdf. To do this we set a object, say $pdf, to handle pdf manipulations.

<?php
$pdf = pdf_new();

Pass the object at the first position in all of the PHP pdf functions when required for that page. To open the file, code we use the pdf_open_file function.

pdf_open_file($pdf, “C:\bennyboy.pdf”);

This should create a blank new pdf file size 0kb. The new file has no properties, so let’s assign some. You’ll need to use the pdf_set_info function for this.

pdf_set_info($pdf, “Author”, “Ben Shepherd”);
pdf_set_info($pdf, “Title”, “Creating a pdf”);
pdf_set_info($pdf, “Creator”, “Ben Shepherd”);
pdf_set_info($pdf, “Subject”, “Creating a pdf”);

Now we have all the particulars taken care of let’s do some pdf manipulation. Top begin we need to use the pdf_begin_page function. The parameters, apart from the first which is always $pdf, are measures in of the width and height respectively. A4 is 595 x 842, Letter is 612 x 792 and Legal is 612 x 1008.

pdf_begin_page($pdf, 595, 842);

Now it is time to assign a text font for the information to be displayed. Simply use the pdf_findfont and pdf_setfont to do this. I choose the Arial font type with size of 14.

$arial = pdf_findfont($pdf, “Arial”, “host”, 1);
pdf_setfont($pdf, $arial, 14);

Now we have set the font type, it is time to use it. To display text in the pdf file you must use the pdf_show_xy function. The x-values (i.e. the third parameter), start from the left hand side of the page and move to the right. The y-values start from the bottom of the page and work towards the top.

So, it is said that, when you work with the pdf_show_xy function the page starts at the bottom left hand corner of the page. So if we wish to type some text 50 units from the left of the page and 400 units from the bottom of the page you would type the following.

pdf_show_xy($pdf, “<Type your info here>”,50, 400);

But you may not want just text on a page. If you are creating a pdf document for a client you may wish to display a logo. There are functions like pdf_open_gif and pdf_open_jpeg that will open up images and assign them to an object to use in the document.

$gif_image = pdf_open_gif($pdf, “baseball.gif”);

To put the object onto the pdf file you use the pdf_place_image function with the parameter being pdf file, image file, x-value, y-value and scale repectively.

pdf_place_image($pdf, $gif_image, 200, 300, 1.0);

You must close the image to put it out of use.

pdf_close_image($pdf, $gif_image);

Let’s end the pdf manipulation process by using the pdf_end_page and the pdf_close functions.

pdf_end_page($pdf);
pdf_close($pdf);

Now to view your pdf file, simply create a link to open the pdf in a new window.

echo “<A xhref=\”C:\bennyboy.pdf\” TARGET=\”_blank\”>Open pdf in a new window $user</A>”
?>


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.