UltraMega Tech.
28Jul/0927

Using MySQL Prepared Statements in PHP

Prepared statements in MySQL are an alternative to writing raw SQL code to execute. Instead, you write a statement with placeholders (?) where you want variable to go, then attach variables to those placeholders.

A prepared statement is basically a template that can be reused with different variables. There are some benefits and drawbacks to prepared statements that should be considered:

Pros:

  • Prevents SQL injection without needing to escape data
  • Allows you to repeat the same statement without the overhead of parsing the SQL
  • Allows you to send raw binary data in packets
  • Creates code that is easier to read by separating SQL logic from data

Cons:

  • Slower for one time queries since it requires two requests from the MySQL server
  • Limited to SELECT, INSERT, REPLACE, UPDATE, DELETE, and CREATE TABLE queries
  • Placeholders can only be used for values and not table/column names

Conclusion: I'd say prepared statements win due to security benefits alone

PHP supports MySQL prepared statements using the Mysqli (MySQL Improved) extension in PHP 5 via the MySQLi_STMT class. They are fairly easy to use once you get used to the differences from writing raw SQL statements. This tutorial will explain how to use prepared statements.

21Jul/095

Modifying Templates Using DOMDocument In PHP

In the previous post, Generating (X)HTML Documents Using DOMDocument In PHP, we explored the PHP DOMDocument class by generating an (X)HTML page completely within PHP. Now, we'll look at a more practical application that involves modifying an existing template. The template gives us a good starting point so we can focus on generating only the dynamic parts of the page.

Tagged as: , , Continue reading
14Jul/095

Generating (X)HTML Documents Using DOMDocument In PHP

PHP 5 includes a powerful set of DOM manipulation classes that gives you full control over XML documents. This functionality behaves very similar to JavaScript's DOM manipulation engine. In this tutorial, we'll explore the DOMDocument class by generating an entire (X)HTML page without writing a single bit of raw markup. This may not be practical for most applications, but it should give you a good idea of how the basic DOMDocument methods work.

Tagged as: , , Continue reading
7Jul/0910

Fetch Twitter Updates in PHP

Would you like to integrate Twitter into your website? In this tutorial, I'll explain how to access the Twitter API to fetch a user's updates using raw PHP (version 5.0 or greater). This will be a very basic use of the Twitter API, but it should give you a starting point if you need to do something more advanced. There are also some Twitter libraries that will let you do the same thing without knowing how it all works.

We will be utilizing the statuses/user_timeline method of the Twitter REST API. Basically, this is a special URL that returns the requested data in the requested format. In this case, we will request the user_timeline as XML, so our URL looks like: "http://twitter.com/statuses/user_timeline.xml?screen_name=username".

30Jun/090

FireFox 3.5 – What does it mean for Web developers?

FireFox 3.5 has been released! Along with a long list of new features and faster browsing, FireFox 3.5 also implements the latest Web technologies such as HTML5 and CSS3. Here is an overview of some of the more interesting features...

New in FireFox 3.5

  • HTML 5 features:

    • audio and video - These elements provide native support for embedded audio and video, which means supporting browsers won't require any 3rd party plugins.
    • Drag and drop - API that allows dragging items within or even between websites.
  • CSS3 features:
    • @font-face - Allows you to use fonts without requiring the user to have them installed. Fonts are downloaded from the server as needed!
    • Media queries - Allow you to specify different CSS rules based on different media types, or even a wide range of specific features (such as screen size).
    • text-shadow - Applies shadow effects to text.
    • -moz-box-shadow - Applies shadow effects from almost any element!
      • -moz-border-image - Specifies an image to use for an element's border.
    • JavaScript features:

    See the full list of developer features available with FireFor 3.5.

    25Jun/0933

    Create a Slide-In Panel with jQuery

    In this tutorial, I'll explain how to create a slide-in panel effect with jQuery. Basically, this means a hidden panel the slides into view when it is triggered by a button or other action. This is often used to display a contact form in a fun way, which is exactly what I am using it for on the new UltraMega website. As it turns out, this is very easy to do.

    View Demo