0

A Very Nice PHP Array Technique

Introduction

You properly already knew the following code:

$strings = array();
$strings[] = "apple";
$strings[]="bamboo";
$strings[]="cat";

This will contain an array of strings:

array(1=>"apple", 2=>"bamboo", 3=>"cat");

However, this is not very useful for multi-dimensional arrays where each array in a parent array contains the same keys and different values like the database array. This is where associative arrays come into play. The code below uses the same technique as above without using the array_push function.

Source Code

foreach ($strings as $row) {
 foreach ($row as $col=>$val) {
  $data[$col] = $val;
 }
}

Explanation

As you can see from the code, each array in the $strings array is copied into a new $data array. I can then use this newly copied array just after the end of second foreach loop to do something. For example, insert it into another database to change the database driver (MySQL to SQLite, for example).

Please share it in the comments below if you have any other uses for this technique.

0

Create a DSN-less database connection using ColdFusion 8

For a long time, I had been searching around online to find a solution to switch to MySQL database for my ColdFusion school server from Microsoft Access database. Since I have no control over the ColdFusion Administration settings, I can’t change the DSN connection.

Recently I have been finishing my remaining undone projects. I use Railo open source CF server and MySQL under Ubuntu to do the assignments for the class. The SQL code I was writing is a bunch of aggregate functions:

SELECT
	   b.BookId,
   	   b.BookTitle,
	   s.SalePrice,
          COUNT(s.SalePrice) AS bcount,
          SUM(sd.TotalSale) AS ts,
          SUM(sd.Quantity) AS qt
FROM tblBooks AS b,
     tblSales AS s,
     tblSalesDetail AS sd
WHERE b.BookId=s.BookId AND s.SaleId=sd.SaleId AND s.SalePrice IS NOT NULL
GROUP BY b.BookTitle
ORDER BY #variables.ob# #URL.s#;

When I was testing these code on the localhost which is MySQL-powered, the page was rendered fine without any errors. However, once I uploaded to the school server which is powered by MS Access, the errors began to appear. Noticeably, I can’t use the name such as b.BookId, b.BookTitle, s.SalePrice without using aggregate functions. It struggled me a bit because the final is due tomorrow.

I then remembered my unsolved solution for connecting MySQL database dynamically through CF code because most of the DSN-less code online only works in CF 5. Once again I began to searching for the solution to release my struggle to the MS Access. Finally, I found a CF custom tag to do just that. It is called query, similar to cfquery. Since it’s a custom tag, the use of it is cf_query. This tag is primary used to connect to the MS SQL server by its author. Fortunately he has given a code snippet to dump out the available JDBC drivers on the server.

After I tested this code, I was surprised to see that my school server supports lots of databases that I don’t know about. I chose the MySQL one and copied its JDBC URL to the CF test page. I noticed that I was unable to connect to my own MySQL server on this web server since my hosting provider doesn’t allow remote connection to MySQL. Therefore I googled for “free mysql hosting” and found a website called FreeSQL.org that specifically for hosting free MySQL databases. I connect to this newly created database by using the cf_query code:

<cf_query jdbcURL="jdbc:mysql://www.freesql.org/ColdFusion" username="username" password="password" qName="qGetData">

Note that ColdFusion is the database name. Within the cf_query beginning and ending tag, I wrote the SQL statements to import the data since the phpMyAdmin link does not work on FreeSQL.org. After that, I replaced the above cf_query code with cfquery in the project that I struggled with.

You can download the above code here along with the custom tag and the original code from its author. The project page that I was struggled with can be found here.

1

Backup MySQL Database without PHPMyAdmin

When I heard that there is no graphical interface for managing MySQL database on the school web server during the first day of my CA 282 class, I thought that was a good thing because I can in depth learn SQL query language. However, after several months of usage, I noticed that I couldn’t be able to backup my database for the localhost use of the database without using PHPMyAdmin. This made me frustrating because I want my CA282 assignments to be published after I reviewed and tested on my local machine.

Because of this reason, I searched around the Internet and found a useful PHP script on mt-soft.com.ar, The script is called MySQLdump. It will dump out all the data inside a database an allows me to download the zip achieve or .sql file. Since I’m able to view the source code once I downloaded this PHP file, I decided to modify the code to work for my own purpose – to backup the database to my localhost.

The script originally had some advanced output options which it doesn’t have any usefulness to me.

Original page layout
Click the image to enlarge

So I changed into the following:

Modified page layout
Click the image to enlarge

Basically. the data in the MySQL database will be dumped out to the text box once the page is loaded. I can then copy the content in the text area to the PHPMyAdmin in my local machine. If I want one more copy in my hard drive, I can click the Download button to download the .sql file.

You can download the source code for the modified PHP script and the download page from here and the demonstration of this script is here.