d.cochran's blog

Connect to MySQL database using PDO

Below is how to set up a PDO connection to a MySQL database.

Prevent Spam without Captcha

The following will show you how to prevent spam without the use of those annoying captcha images. I use javascript, which according to W3C, approximately 95% of the people browsing the web have it enabled. The odds of one of your visitors having it disabled is slim. If it does concern you however, you could always place a notice to turn on javascript before using the form or you could always use another method, just place the other method in

tags.

Below is how you could prevent spam with simple javascript.

MySQL, UPDATE data into a database using CONCAT()

To insert data into a MySQL database field while preserving the existing data in that field, simply concatenate the new data to the end of the existing data.

Below is an example of how to do this using the field "changelog".

UPDATE table1 SET changelog = CONCAT(changelog, "new data" ) WHERE id = 'idnumber';

If you would like the data that you are entering to appear at the beginning of the existing data, simply flip the concatenation, example:

MySQL INSERT with a Max() + 1 SubQuery

This was a pain in my ass to figure out so I figured I would post it here in case anyone else needs to insert a record using a Max() + 1 SubQuery.

I only use the Max()+1 SubQuery when the value I want to increase is not the primary key.

INSERT INTO table (row1, row2, row3) SELECT 1 + COALESCE((SELECT MAX(row1) FROM table), 0), 'value2', 'value3';

MySQL DELETE with a Max() Subquery

Here is a nifty way to use a SubQuery when deleting a record from your MySQL table.

The following MySQL code will delete a record from "table1" where the slot is equal to the MAX() value of slot from "table2".

DELETE FROM table1 WHERE slot = (SELECT MAX(slot) FROM table2);

Latitude and Longitude of a location using PHP and Google

The following PHP code will help you acquire the latitude and longitude of a location (using an address, zip code, or city).

Note: Your will be required to have your own domain specific Google API key.

Latitude and Longitude of a location using Javascript and Google

The following Javascript will help you acquire the latitude and longitude of a location (using an address, zip code, or city).

Note: You will need to have a Google API key which is domain specific.

PHP Format Phone Number Function

Below is a short function that will take any 10 or 9 digit number and convert it into the following phone format: 555-555-5555

Javascript broke with multiple line variables from PHP

I have several sites where I use a pop up table to display information if the user rolls over and image.

Example, if the user wants to view notes on a certain client, they can roll over a little "note" image to view the notes about the client pulled from a SQL database.

The code I use for a message may be:

Creating a Pop Up Window with Javascript

This part goes in the link tag:Here is how to create a simple pop-up window using simple javascript.

Create a pop-up window on page load:

Pages

Subscribe to RSS - d.cochran's blog
If you have questions about anything on this site, you can find me on Google+: