Using MySQL Stored Procedures with PHP mysql/mysqli/pdo

Wondering how to use stored procedures with PHP and MySQL? So was I and here’s what I’ve learned. In this tutorial I’ll explain how to use PHP (I’m using 5.2.6) to call MySQL (I’m using 5.0.2) stored procedures using the following database extensions:

First we need to setup our enviroment which consists of a new database with one table and two stored procedures. In your db tool of choice (I’ll be using the MySQL Query Browser) create a new database named test. After you create the new database, make sure to add a user called example with password example to the database and give it read access.

CREATE DATABASE `test`;

Now create the table users:

DROP TABLE IF EXISTS `test`.`users`;
CREATE TABLE  `test`.`users` (
`users_id` int(10) unsigned NOT NULL auto_increment,
`first_name` varchar(100) NOT NULL,
`last_name` varchar(100) NOT NULL,
PRIMARY KEY  (`users_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Continue reading “Using MySQL Stored Procedures with PHP mysql/mysqli/pdo”

Random image from folder – rotating banner in php

So the other day someone was looking for a quick php script that would load a random image from a folder (rotating banner script) and display it on their website. There are already a ton of scripts out there that do this but of course, this was an excuse to build something for fun. So, I decided to create a script that would do just this but with the least amount of code lines still keeping it efficient (again, just for fun). This is what I came up with:

<?php
$files = @opendir($dir=trim($_REQUEST['dir'])) or die('Not Valid');
while($file = readdir($files)) preg_match('/(.png)|(.gif)|(.jpg)/i',$file) ?
        $images[] = array($file,substr($file,strlen($file)-3)) : null;
count($images) ? header('Content-type: image/'.$images[$id=rand(0,count($images)-1)][1]) :
        die('Not Valid');
print file_get_contents($dir.(substr($dir,strlen($dir)-1)!='/'?'/':null).$images[$id][0]);
?>

Continue reading “Random image from folder – rotating banner in php”

Getting list of members from an event in meetup.com with PHP

So, last week I attended our monthly Atlanta PHP meet and had a good time. At the end of the session some prizes were awarded “randomly”. Why “randomly”? Because a member of the group wrote down two names on a piece of paper and started asking the other members for a number. The two to get the numbers correct won an award. Now, this was a quick last minute thing for fun, so I am definitely over engineering this – but hey, that’s what makes programming fun!

The problems were:

  • (and I apologize for my honesty in case any member that attended the event reads this) I could see the persons hand movement while writing down the numbers so I already knew one of the numbers before starting. 
  • The paper was lifted at some point before he started calling out for numbers and I could see the second number written on the paper.
  • It’s not truly random

Continue reading “Getting list of members from an event in meetup.com with PHP”

ob_start(), ob_flush(), flush(), set_time_limit() – Give user feedback during execution

So every once in a while I want/need to write some code that may take some time to execute. The problem is because it takes so long to execute, I need to make sure to give the user some feedback during this process so they know the page is working. If I hit submit in a page and nothing changes in my screen after 20-30 seconds I assume something is wrong. For example, I wrote a php page that would query multiple users in one db, then for each user load their xml data from another db and parse out the information to return a report for all users in the date range. Depending on the date range, this could take a few minutes to complete. This is where the php output buffer comes into play. Continue reading “ob_start(), ob_flush(), flush(), set_time_limit() – Give user feedback during execution”

Atlanta PHP December Meeting – Singleton Design Pattern and Moodle

So I attended the Atlanta PHP meeting last week and it was fun just like the previous time. I find this particular group fun and knowledgeable making me look forward to the next meeting. The topics covered this time were the singleton design pattern and the Moodle course management system. It seems like we are going to start covering a design pattern plus another topic during each future meeting which I think is a great idea. You can never know enough about design patterns.

Singleton Pattern

So there wasn’t much said that was new to me about the singleton pattern. It’s pretty standard, the objective of this pattern is to prevent creating multiple instances of a class. The code example was something like this: Continue reading “Atlanta PHP December Meeting – Singleton Design Pattern and Moodle”

PHP – Copying x Files to n Folders

So a co-worker needed to find a way of coping a few files (10 files) to a bunch of folders if the files weren’t already present. Instead of doing a manual copy paste in Windows I created this code to do the above. If anyone needs to do something similar feel free to use this code. Pretty much all is does is create an array with all the folders where this PHP file is being run (this is the root where the bunch of folders reside). Then it creates another array of files – these are the files that we need to copy to all the folders. This array is created by looking in the templates folder and adding each file to the array.

The ‘foldersOnly’ function gets the array from scandir() and cleans it up. It removes the ‘.’, ‘..’, and a couple Dreamweaver folders like ‘_notes’ and another one I can remember. The new array returns only the folders we want to work with. The ‘filesOnly’ gets the array from the PHP function scandir() and returns only the files in that folder. Once we have our arrays we are ready to start.

We are going to loop for each folder and inside each folder look for each file. We check to see if the file is in the folder, if so we do nothing. If the file is not there, we copy it from the templates folder. This is pretty much it, once all the loops are done every folder should have the missing files. The code is below: Continue reading “PHP – Copying x Files to n Folders”

Quick update to Uploader – fixed some minor bugs

Two bugs were found and addressed. The first bug happened while trying to upload a link. I dropped a URL on the Uploader and the app got stuck in the “Thinking” phase. After some troubleshooting, I noticed the page for the URL did not have a <title> tag so the app was sending the PHP page an empty ‘name’ variable causing the PHP page to stop executing during the validation phase.  The fix was to assign a default name to the link if no title tag is present. In these cases, I’ll manually give the link a proper title.

The second issue also had to do with the <title> tag. In this case, the HTML did have a title tag but the case was different that what I was looking for. My code was looking for all lowercase characters and the tag in the HTML was in uppercase. My first thought was to do a .toLowerCase() on the HTML code but then all the titles would be in lower case and wouldn’t look as presentable (ex: the size of our planet vs The Size of Our Planet). After giving this some more thought, it seemed using regex would be the cleanest solution. A quick Google search returned this page which said by adding an ‘i’ to the end of my regex, the search would be case insensitive. I gave it a try and it worked like a charm.

Before Code:

var start:Number = s.indexOf('<title>');
var end:Number = s.indexOf('</title>');
var title:String = s.substr(start + 7, end - start - 7);

After Code:

var start:Number = s.search(/<title>/i);
var end:Number = s.search(/<\/title>/i);
var title:String = s.substr(start + 7, end - start - 7);

Uploader Phase 2 complete!

Here is a screen shot of the Uploader 0.1.0
Uploader 0.2.0

So today after work I decided it was about time to start working on phase two of my Uploader tool. Phase two consists of allowing me to drag and drop an image URL into the app so the app can then download the image to a folder in my server, create a thumbnail of this image, and update the WordPress database with information about the new image. The new image would then show up the next time a person visits my blog. If you notice, there is now a “Pics of Interest” section on the sidebar to the right of the page (similar to what people are doing with the flickr plugin). Every time I drag an image URL into the Uploader, the new image thumbnail will show in that area and if you click on the thumbnail you’ll see the full size image.

It was actually much easier than I originally thought to make this work. Technically, the Uploader already has the drag/drop functionality and already takes in a URL. The only extra AS3 code was a check to see if the URL was an image and if so call the PHP page with a code (I’m passing –@IMAGE@– to know it’s an image – it was just the first thing that came to my head) so then PHP knows this URL should be treated as an image not just a URL. If you remember, phase 1 for the Uploader allows a person to drag/drop a URL into the app so it creates a new link under the “Links of Interest”. Continue reading “Uploader Phase 2 complete!”

Car problems fixed, new tune results!

So I recently made a post about the issues I’ve been experiencing with my car for the last couple weeks. Well I’m happy to say everything has finally been taken care off and my car is better than ever. Scott Siegel did an amayzing job last night on my car as always and here are the results: Continue reading “Car problems fixed, new tune results!”

CarLab X-Brace Review – 05 STI

After spending plenty money on go fast mods I decided it’s time to spend some money on making my car handle better. It seems most people tend to start with sway bars, struts, or springs so I started looking into those a bit more. After giving these options some thought, I decided sway bars would be the better choice since they aren’t too expensive and would be an easy install. I went to the ‘for sale’ forums at IWSTI looking for some sway bars and noticed a group buy for the CarLab X-Brace.

I didn’t know much about the x-brace but when I clicked on the thread I saw lots of very positive comments about this brace. I did a site search for the x-brace and even more info came up on how wonderful this mod is and some people were saying this was the perfect first suspension mod for them. The price was about the same as getting both front and rear sway bars and it sounded like I would get more benefit from the brace so I decided to join the group buy and bought the brace. Continue reading “CarLab X-Brace Review – 05 STI”