Archive

Archive for the ‘Scripts’ Category

PHP Script Library

February 7th, 2009

Script Library
In order to help pay for the site I have thrown togather a great starter library for the noobie php programmer:

PHP Programmer Scripts

Includes:

arrays - An array is a data structure that stores single or multiple values in a single value
dbconnect - Shows how to connect to a MySQL Database
functions - Teaches how to create a function and call it out in a script
for each/while/for loops - how to effectively use loops
forms - How to use a form to send information to the script
helloworld - The basic hey Im here, Im new and I am proud script
if/else/elseif statements - How to selectively choose what the script executes
switches - If you would like to create a simple oop website this is a good thing to know
includes - This will help in organizing all your external files
variables - How to properly use a variable
comments - How to comment out your code

Scripts

Fetch Data From MYSQL Using PHP

November 16th, 2008

$query = “SELECT * FROM table”;
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
echo $row['column1']. ” - “. $row['column2']. “
“;
}

This script will come in handy for the new PHP Programmer. You can fetch information from the database and display it directly to your browser. These scripts are made for simplicity. I may someday write a tutorial but as you know there are plenty of tutorials for a PHP Programmer.

Scripts

Connect to MYSQL database using PHP

November 16th, 2008

mysql_connect(”localhost”, “USER”, “PASS”) or die(mysql_error());
echo “Connected to MySQL”;
mysql_select_db(”DATABASE”) or die(mysql_error());
echo “Connected to Database”;

This is a simple mysql db connection script for the PHP Programmer. All you have to do is replace the USER, PASS and DATABASE with your settings. You may also need to change the localhost with your own setting also. These scripts are intended for educational purposes. Feel free to use them as you need.

OOP Programming is becoming more and more popular among web owners and programmers. Here is an example for getting data from the db using OOP PHP Programming.

Create a table named our_table and create a field for ID. Now enter a coupe id’s in this new table. When you have your db set create the following files.

Name a file include.php and add this:

include ‘function.php’;
require_once(’DB.php’);

$host = ‘localhost’;
$user = ‘your user’;
$pass = ‘your pass’;
$database = ‘your db’;

$dsn = “mysql://$user:$pass@$host/$database”;
$db = DB::connect($dsn, false);

function GetMainContent()
{
global $db;

$result = $db->query(’SELECT * FROM our_table ORDER BY id ASC’);

$selfArray = array();
while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
{
$myContent = new ContentBag($row["id"]);
array_push($selfArray, $myContent);
}
return $selfArray;
$db->disconnect();

}

Now create a file named function.php and add this:

class ContentBag
{

public $id;

function __construct($Id=null)
{
if (!empty($Id))
{
$this->id = $Id;
}

}

}

Now in your view file named index.php add this:

include (”includes.php”);
$get_id = GetMainContent();
echo “\n”;
foreach ($get_id as $text)
{
echo “ID: ” . $text->lid . ”
\n”;
}

Scripts

Send Email with PHP

November 14th, 2008

$EMAIL_HEADER = “From: Sender Name <sender@example.com>\r\n”;
$EMAIL_HEADER .= “MIME-Version: 1.0\r\n”;
$EMAIL_HEADER .= “X-Priority: 1\r\n”;
$EMAIL_HEADER .= “X-MSmail-Priority: High\r\n”;

$MESSAGE=”
Hello there,

This is a test message.

From Admin.”;

mail(”recipient@domain.com”, “Example Subject”, $MESSAGE, $EMAIL_HEADER);

Scripts