By Jason Lengstorf
PHP and other server-side programming languages are tricky. The manual can be tough to decipher, and there isn’t really a way to “validate” a PHP script. If you’re new to programming, you may lost and not know where to look for help. When I first started programming, I spent hours pulling my hair out, digging through manuals, and poring over books. It wasn’t until I found a great online community that I really started to get in the swing with PHP and felt like I was actually accomplishing something.
Here at Smashing Magazine, we want to help out PHP programmers who are just getting started or who want to improve their programming chops. Our goal is to support our community by answering their questions and trying to find solutions to their problems.
While Chris Coyier takes care of CSS and JavaScript-related questions, from now on me, Jason Lengstorf, will take care of your PHP- and MySQL-related questions.
You can just tweet me @jasonatennui with the tag “[Ask SM].” In our first installment, we’ll answer a smattering of user-submitted questions about PHP and MySQL. Posts focused on Ruby, Python, Photoshop and Illustrator are coming as well.
1. Form validation with PHP
@titel asks:
What is an easy-to-implement and reusable set of functions or small class that automates form validation with PHP?
I’ve heard good things about this form validation class, but I think it’s really better to take the time to write your own. There are tons of easy tutorials on Google to get you started; and as your needs change, you’ll probably end up tweaking the code to fit your needs.
If you’re going to be storing the validated values in a MySQL database, it would be wise to look into methods of avoiding injection attacks. There are some wonderful free libraries (mysqli and PDO, for instance) that go a long ways towards securing your Web applications by creating prepared statements that help prevent SQL injection.
2. Converting MySQL to XML
@igmuska asks:
What’s the best practice for converting MySQL to XML for using Google Maps on a PHP page?
There’s a pretty good tutorial on the Google Maps API page to get familiar with the format that you’ll need to use for the XML file, and then you’ll want to write a function or class to handle the creation of individual XML entries.
You could do something like this to generate your XML output:
while($entry = mysql_fetch_assoc($result)) {
$xml .= <<<XML_OUTPUT
<{$entry[‘datatype’]}>
<point lng=“{$entry[‘longitude’]}” lat=“{$entry[‘latitude’]}” />
<icon image=“{$entry[‘icon’]}” class=“local” />
</{$entry[‘datatype’]}>
XML_OUTPUT;
}
3. require_once()-problem
@DanBowles asks:
I performed a require_once(…) on a config file only to find I could not access the variables in the file. How come?
There’s no hard and fast answer to that question, but possible problems could be that you’re trying to access variables inside a function without declaring them as globals, or that your config file is in a format that your server isn’t configured for to parse PHP.
To make sure the PHP in the config file is being parsed, make the file output some text (i.e. echo ‘Is this thing on?’;) and see if it shows up when you require the file. If you’re trying to use variables from the config file in a function contained in the parent file, declare the variable as a global at the top of the function (i.e. global $myVar;).
4. Search in different tables?
@MikevHoenselaar asks:
What is the best way to search a website with MySQL/PHP in different tables?
To search multiple tables, start by using JOIN in your MySQL query. A great introductory article on the concept is available here. With regard to the best method of searching, that depends on the type of information you’re searching for.
If you’re looking for an exact phrase, it’s probably best to start off with a LIKE-statement, which looks for an exact word or phrase (i.e. a search of entry titles). More general queries would best be handled by a fulltext-search, which runs through a table and finds relevant entries (i.e. a site-wide search for entries related to a cetain word or phrase).
5. Getting information out of an XML-file
@korteev asks:
How can I get information out of an XML file?
RSS is an extremely useful tool for developers because it allows us to take information from one website and put it in another. It also has the benefit of allowing you to format that content fairly easily.
For PHP5, SimpleXML is a great tool that makes parsing XML feeds really easy. There’s a great article here on how to use it, as well as a resource on w3schools.com that reviews the different methods available.
After you get the hang of it, using it is pretty straightforward. For example, take this XML file:
<?xml version=“1.0”?>
<people>
<person>
<name>John Doe</name>
<age>27</age>
</person>
<person>
<name>Jane Doe</name>
<age>31</age>
</person>
</people>
To get information out of the file, all we have to do is this:
<?php
$people = simplexml_load_file(‘people.xml’);
foreach ($people->person as $person) {
echo “Name: {$person[‘name’]}n”;
echo “Age: {$person[‘age’]}n”;
}
?>
SimpleXML also supports namespaces, which is very useful when parsing Flickr’s RSS feed, for example.
Further Resources
- PHP Manual — This is my bible.
- MySQL Manual — A little harder to understand, but still incredibly useful.
- W3Schools Forum — When I get stuck, I can always count on these guys for help.
(al)