Selecting data from MySql using a PHP array containing strings

MySql
Just a quick note on selecting mysql data using PHP arrays, specifically arrays containing strings.

MySql has the ability to accept array values via IN comparison operator.

See below:

SELECT * FROM table_name WHERE colum1 IN ($array)
Which could equate to something like:
SELECT * FROM table_name WHERE colum1 IN (2, 3, 8)
This will select all data from table_name where colum1 = all array values*

*Assuming these values are numerical and not strings.
If you want to select MySql data from an array that contains strings then one of the quickest and easiest possibilities is to implode your array into a single string to form a new variable that you can use in your MySql SELECT command in brackets after IN.
Like so:

$array_strings = implode("', '", $array();
SELECT * FROM table_name WHERE colum1 IN ('$array_strings')

Here we are using $array_strings to store all values inside of $array() separated with what ever is between ""
For instance:

$array[] = "ax";
$array[] = "au";
$array[] = "xb";
$array_strings = implode("|", $array();
echo $array_strings

Would display:
ax|au|xb

A good thing to know, but not exactly fitting for the syntax required while using IN.
We need to read IN (ax, au, xb).
So we added ' ' to either side of our variable $array_strings and place all of that in brackets:

$array[] = "ax";
$array[] = "au";
$array[] = "xb";
$array_strings = implode("' '", $array();
$sql="SELECT * FROM table_name WHERE colum1 IN ('$array_strings')";

Hopefully somebody will find this of some use


Notice: Use of undefined constant MCRYPT_BLOWFISH

Of late there have been a noticeable amount of support requests on freelance websites and on the Magento forums regarding the MCRYPT_BLOWFISH error some users have seenon both Linux and Microsoft servers. Some are effected after server moves and others suggest it is random. Here we are going to take a quick look at the error, why it has happened and how to resolve it.

The reason for the error is actually quite simple and the fix is usually not a massive problem either unless your server is running old or legacy software. The error is caused when PHP cannot find the php-mcrypt module and or the libmcrypt libary.

How to install php-mcrypt and libmcrypt on my Linux server?

First find out which version of PHP you are running and also find out your version of Linux. Once you know your version of Linux you need to lookup how to install programs on there. Usually this will be done with the RPM command, apt-get command or yum install command etc. Then at the command line type (without the hash and replacing apt-get with whatever you installing is, of course) ...

# apt-get install php5-mcrypt

or if you are running php 4 ..

# apt-get install php4-mcrypt

Also if you don't have the library we mentioned ...

# apt-get install libmcrypt

Once you have installed those you will need to restart apache to get them to work. That should be it! If you are having troubles with this then post a comment below and let us know your system specs, we will do our best to help.

How to install php-mcrypt and libmcrypt on my Windows server?

Download libmcrypt.dll from php.net and place it in your PHP directory on your server. This is the directory that usually holds the PHP configuration files and so forth. Now open up php.ini (usually in the same directory, if not then do a Windows search for it) and find the line...

# extension=php_mcrypt.dll

Remove the hash '#' and restart your server and you should be good to go.


Debugging Cake PHP Applications

A common question new starters to CakePHP ask is regarding debugging. Many users start off with basic scaffolding and confronted with the uninformative error ....

Not Found

The requested URL /index.php was not found on this server.

This is because your CakePHP application is set to production mode. In production mode you would not want your visitors and clients seeing detailed error reports, it would worry them.

To find out what the actual problem is open up file app/config/core.php and find line 45(ish) and you will see the following ....


Configure::write('debug', 0);

Change the above value from 0 to either 1, 2 or 3 and you will get a much more helpful error message. We recommend you set it to 2.

Direct from the CakePHP comments itself; here is an explanation of each debug level...

* Development Mode:
* 1: Errors and warnings shown, model caches refreshed, flash messages halted.
* 2: As in 1, but also with full debug messages and SQL output.
* 3: As in 2, but also with full controller dump.

It is worth noting that when debug level is set to anything other than production mode (0), any 'flash' messages and redirects you have will not redirect automatically, you must click the link to forward on.


Saving Form Data Twice in CakePHP

CakePHP makes it increbibly easy to insert data into MySQL tables with the use of their inbuilt form helper and and save function however one question I have seen alot is how to save form data more than once equating to multiple row inserts? The answer is actually quite simple.

When you use the CakePHP save method, CakePHP holds on to the last primary ID it created, and so if you try using the save method again, in the same session.. nothing happens, as the row already exists as far as Cake is concerned. You need to make Cake forget about the last primary ID it's holding.


$this->Whatever->create();
$this->Whatever->save($this->data);

$this->Whatever->create();
$this->Whatever->save($this->data);

The code above runs the create() method on the Whatever model reseting any primary ID Cake was holding for it. Then it runs the save method saving the form data to the Whatever model. It then runs the create method again, resetting the primary ID and then saves the form data again, giving you two rows in your table with the same data all bar the primary key.

Of course you would more than likely put your save in a loop of some kind if you were planning on multiple saves.


$inserts_required = 1;
while ($inserts_required <= 10) {

$this->Whatever->create();
$this->Whatever->save($this->data));
echo $inserts_required++;
}

The above will insert 10 rows with the same data, bar the primary ID.


Links on Images in the CakePHP Framework

Introduction: I have seen a fair number of posts on the cakePHP google group and also on a few blogs regarding how to make links in views that are images using the cakePHP helpers. There are ways to fudge this and hard code paths to images and links but when you start swapping between /pages and /controllers this all gets very messy and is not using what cakePHP provides so I thought I would write a quick tutorial on this. Actually it's more like a few notes with an example - this is a real easy task, and quite obvious once you consider what we are doing.

Assumptions: I'm assuming your using cakePHP 1.2 here if your not, go get it :) If you can't upgrade then just read this example code below then check the 1.1 manual for 'image' and 'link' and you should be able to work it.

Step 1: Displaying an Image on a View or Layout

O.K as you may or may not know, to display and image on a layout or view in correct the correct cake manor just ...

echo $html->image('home.gif');

Obviously above, change 'home.gif' to your image.

Step 2: Making a HREF Link on a View or Layout

Another one you may already know...

echo $html->link("Home", array('action' => 'features'), array('escape' => false));

And again, change 'Home' to your link text and 'features' to your action in your controller. The above example will link the text 'Home' to the 'features' action of the currently active controller. If you have this on a view in the /pages cake directory it will link to /pages/features. This is no good if you wan't to link to another controller, however this is easy too ...

echo $html->link("Products", array('controller'=>'products', 'action' => 'features'));

Above we are also specifying the controller name. The above example will link the text 'Products' to the controller 'products' and the action 'features'. ie /products/features.

Step 3: Coming image and link for a linked image

O.K the bit of code you came to this page for ...

echo $html->link($html->image("products.gif"), array('controller'=>'products', 'action' => 'features'), array('escape' => false));

As you can see, from a PHP point of view it is as simple as including a function within another function. So here, the image 'products.gif' links to the 'products' controller and the 'features' action of that controller. You may also noticed we have added escape = false. If you do not specify this as fasle, or you specify it as true then the raw html for the image will be out printed to the view as the link instead of the actual image.

That's everything you need for now to get you linking images within CakePHP views. For more information regarding CakePHP and more tutorials please visit CakePHP.co.uk