Using PHP to Copy an SQL Server Table to MySQL

At work, we have a scheduled task run every day that pulls information out of a huge Oracle database and dumps it into a Microsoft SQL table on one of our local IIS Web servers. We then have a script on our remote LAMPP Web server (the one we use to serve up our public Web site) that queries that MSSQL table and displays results on the Web site.

Unfortunately, because the Web server and the MSSQL server are in different locations, the results are sometimes slow and unreliable. For instance, if, for some reason, the network connection to our IIS server is down or if the IIS server itself is down, we can’t pull any data from the MSSQL table.

The other day, I began working on a script that will pull only the information we’ll need for any given day out of the MSSQL table and then create a new table in our MySQL installation that resides on the same server as our Web site.

Following are some good tips for you if you decide to do the same thing.

Finding and Replacing the First or Last Substring in a String

search-replace-graphicI found this image on http://search-replace.fr.tc/ using Google Image search.

Today, I found myself in a position where I needed to find a substring within a larger string and then replace the last occurrence of that substring. I started out using the str_ireplace function, but quickly realized there was no way to add an offset or a limit to that function. I then started searching the Internet for a solution. I came across a handful of places where someone had asked to replace the first instance of the substring, but all of the answers seemed to recommend using the preg_replace function (which allows you to set a limit).

With no good answer, I decided to set out and build my own function to do this. I have not tested the performance of this function to determine whether it’s slower or faster than using preg_replace, but I would imagine it’s probably faster (I’d love for someone to benchmark the comparison using a variety of string lengths to find out for sure, though).

Posting to Twitter from Your Web Site

Most content management systems and blogging platforms have a Twitter plug-in available nowadays that allows you to automatically submit tweets when you update a page or post a news item. However, for those of us that aren’t using packaged systems, we have to look elsewhere for solutions to post updates to Twitter.

The other day, while making some changes to the news posting script we use at work, I decided that we should post our news updates on Twitter when we post them to the Web site. As such, I started reviewing the Twitter API. Thankfully, Twitter has a good resource on libraries you can use to make your life easier (after all, there’s no point in reinventing the wheel if you don’t have to).

CGForms First Beta Released

Over the last few years, I have been slowly working on developing a series of PHP classes to handle Web forms. I have finally got enough of the code together and in a portable format to feel comfortable releasing it to the public. The project is called CGForms, is hosted on SourceForge and is completely open and free.

CGForms is intended to make things easier and provide a process for PHP developers to churn out Web forms without having to worry about writing complex processing and validating scripts over and over again. CGForms offers a centralized codebase from which all of your forms are generated, validated and processed (by sending results through e-mail – using HTMLMimeMail5 – and/or saving them to a database).

New Web Development Job Search

Today we launched a new job search for web development jobs with job aggregator Indeed. The job search starts with a generic search for web development jobs. From there you can modify the search to find exactly the job you are looking for. Some searches might be for php, html, ruby, html, etc. and in locations like New York, 90210, San Francisco, Portland, etc.

You can also post a job on the board for $30 which includes distribution across the Indeed network.

So get off your couch and get your self employed so you can buy more tech crap and tweet about your new job.

Programming your own PHP framework Part 2 – MVC

This is part 2 of the programming your own php framework – view part 1. The below article was authored by Terry Smith. Terry is an aspiring entrepreneur and PHP code ninja working for b5media. He spends his “weekends” and “free time” working on all sorts of crazy ideas and one day wants to rule the world (or a small part of it)!

To get things started, you should be familiar with the Model-View-Controller paradigm.  The basics components are models (classes/objects/etc.) that represent items in your application (users, database records, etc.), controllers which do the processing for a page or module and views, which contain the HTML/CSS for your output.  You can read more about it here.

Step 1: Directory Structure

As noted in the last part, I have chosen to structure my URLs in the format

domain.com/[controller]/[module]?vars

In this case, the controller indeed represents the controller in the MVC paradigm.  I will take a moment to explain the directory structure I’ve used; again, note that you can use almost any structure for your own applications.

/config – Basic configuration files (database settings, etc.)
/controllers – Controllers are all in this directory.
/lib – Default library files included with every new site I deploy (database class, URL rewriting, templating and other base classes)
/models – Custom models for each application (users, sessions, etc.)
/views – View files (PHP files), that contain the HTML
/web – The actual web directory we point our web server to.
There are two things of note here.  First, our images, CSS, etc. go into our /web directory, since the web server can’t read anything above the /web folder.
Second, and most important, all of these files should be outside/above the directory you actually point your web server to serve files from.

Pages: