Welcome to my first PHP tutorial. In this one I’ll be going over an easy way to streamline your PHP code. The set of PHP functions I’m going to show you is really useful to keep your PHP code clean. In my day to programming I tend to write a TON of Mysql queries for my PHP. Well this tutorial strives to give you an easy way to execute a series of Mysql commands without having a ton of $sql variables.

So starting things out we have our opening PHP command.

<?php

We then establish our connection to our database, this is only useful if you’re going to be connected to one database for the run of this page.


mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("trial") or die(mysql_error());

Then we’re going to write our first PHP function, this is just a simple Mysql query.

function simple($table) {
$sql = "SELECT * FROM ".$table;
$result = mysql_query($sql);
return $result;
}

We’ll be taking in a variable called table, which identifies which table we should look into. Then we make an $sql variable to take the Mysql command, run the Mysql query, and then return the results of the query via the return function built into PHP.

Now if we just want to be running simple queries all day we can use this. In order to use it we need to write some more PHP.
First we should make a variable with the Mysql table name in it.

$table = 'userdata';

Then run the function. We’re setting $result to catch what ever the function sends back out with the return.

$result = simple($table);

Then we create an array with the Mysql data.

$row = mysql_fetch_array($result);

And use the data with our PHP code. In this case we’re simpling echoing user’s names.


echo $row['username'];

Now then, at the beginning of the page we can assign a few other PHP functions. These are 2 PHP functions that I use and thought you might find helpful

This first function will look at what ever Mysql table you give it and will randomize the results.

function random($table) {
$sql = "SELECT * FROM ".$table." ORDER BY RAND( )";
$result = mysql_query($sql);
return $result;
}

This function is taking in 2 terms, and demonstrates how you can look for a specific row within a Mysql table.

function double($table, $term) {
$sql = "SELECT ".$term." FROM ".$table;
$result = mysql_query($sql);
return $result;
}

And here’s the entire PHP page.

<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("trial") or die(mysql_error());

function simple($table) {
$sql = "SELECT * FROM ".$table;
$result = mysql_query($sql);
return $result;
}

function random($table) {
$sql = "SELECT * FROM ".$table." ORDER BY RAND( )";
$result = mysql_query($sql);
return $result;
}

function double($table, $term) {
$sql = "SELECT ".$term." FROM ".$table;
$result = mysql_query($sql);
return $result;
}

$table = 'userdata';
$result = simple($table);
$row = mysql_fetch_array($result);
echo $row['username'];
?>

Hopefully this has proven useful for you, if you have any questions be sure to leave them in the comments!