This small class will make it easy to run queries using PHP and MySQL. It's very simple and basic, but will make your job much easier.
<?
/*
//=======================================
* phphq.Net Custom PHP Scripts *
//=======================================
:- Class Name: phdb
:- Version: 0.1
:- Release Date: June 1st 2005
:- Author: Scott L. <scott@phphq.net> http://www.phphq.net
:- Copyright (c) 2005 All Rights Reserved
:-
:- This script is freeware. Permission is granted to copy, modify
:- and distribute this program as long as no fees are exchanged.
:- All origional copyright text must remain visible and unmodified.
//=======================================
* Description
//=======================================
:- This is a simple php/mysql class to run your mysql querys.
:- It will save you lots of time typing out the function names of
:- querys and may also reduse the ammount of errors you get from
:- typing long function names.
:- Your code will also look much cleaner.
//=======================================
* Setup
//=======================================
:- Paste this into a file named phdb.class.php and include it
:- into every file that uses the class.
//=======================================
* Some example usuage
//=======================================
Initilize the class:
$ph = new phdb;
Connect to mysql & select database:
$ph->connect("host","user","password","database");
Run a query:
$query=$ph->query("Your Query");
Get a 1 row result:
$result=$ph->result("Your Query");
Get multiple row result:
$query=$ph->query("Your Query");
while($result=$ph->fetch($query)) {
//rows
}
$ph->free($query); //will free the $query
Get rows from query:
$query=$ph->query("Your Query");
$rows=$ph->rows($query);
Get rows without query first:
$rows=$ph->getrows("Your Query");
Get last insert id:
$last_id=$ph->last_id();
Disconnect from mysql:
$ph->disconnect();
*/
class phdb {
function connect($host="localhost",$user,$pass,$data,$persistency=False) {
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->data = $data;
$this->persistency = $persistency;
If($this->persistency) {
$this->link=@mysql_pconnect($this->host,$this->user,$this->pass);
} Else {
$this->link=@mysql_connect($this->host,$this->user,$this->pass);
}
If(($this->link) AND ($this->data)) {
If(@mysql_select_db($this->data,$this->link)) {
return True;
}
}
return False;
}
function fetch($query="") {
$query=@mysql_fetch_array($query);
return($query);
}
function query($query="",$sup="") {
If($sup) {
$query=@mysql_query($query,$this->link);
} Else {
$query=@mysql_query($query,$this->link) or die("<br /><br />".mysql_error()."<br /><br />");
}
return($query);
}
function rows($query="") {
$query=@mysql_num_rows($query);
return($query);
}
function free($query="") {
@mysql_free_result($query);
}
function result($query="",$sup="") {
$query=$this->query($query,$sup);
$result=$this->fetch($query);
$this->free($query);
return($result);
}
function getrows($querystring="") {
$query=$this->query($querystring);
$result=$this->rows($query);
$this->free($query);
return($result);
}
function disconnect() {
@mysql_close($this->link);
}
}
?>
Simple function to return the extension of a file. With the filename image.jpg, the function will return jpg only, it will not return the extension with the 'period'.
<?
function get_ext($key) {
$key=strtolower(substr(strrchr($key, "."), 1));
//$key=str_replace("jpeg","jpg",$key);
//Uncomment above if you want to convert jpeg to jpg.
// Makes life a lil easier since they are the same.
return($key);
}
?>
This function will split rows into multiple pages.
<?
/*
Example usage:
$query=mysql_query("SELECT id FROM tbl_name");
$rows=mysql_num_rows($query);
$max_rows="10";
$pages=split_pages("/index.php?show=maps",$rows,$max_rows);
If($pages) {
Echo("Pages: ".$pages);
}
*/
function split_pages($current_url,$total_pages,$max_pages) {
$pages=ceil($total_pages/$max_pages);
For($i=1;$i<= $pages;$i++) {
$j=($i-1);
$start=($j*$max_pages);
If ($pages>="2") {
If (intval($_GET['start'])==$start) {
$pagenumbers[$i]="<b>[".$i."]</b>\n";
} Else {
$pagenumbers[$i]="<a href=\"".$current_url."&start=".$start."\">".$i."</a>\n";
}
}
}
$pages="";
If (!empty($pagenumbers)) {
$pages=implode("",$pagenumbers);
}
return($pages);
}
?>
This short function will trim a long string into as many chars as you wish.
<?
/*
Example usage:
$long_string="This is a very long string that I want shortened";
$short_string=str_short($long_string,10);
Echo($short_string);
Will output "This is..."
*/
function str_short($key,$len) {
If(strlen($key)>intval($len)) {
return(substr($key,0,($len-3))."...");
}
return($key);
}
?>
This is a simple user authentication script that will allow you to have a login system on your site to display private content.
<?
/*==============
:- Sample Usage:
==============
If($_POST['login']) {
If(auth_user($_POST['username'],$_POST['password'])) {
Echo("You are now logged in. Click here to go to your page");
exit;
} Else {
Echo("Login Failed! Please try again. <br /><br />");
}
}
login_form(); //display the login form
?>
:- ==============
:- Put this at the top of files you want to protect.:
:- ==============
<?
If(!is_logged_in()) {
Echo("Please login first");
login_form(); //display the login form
exit;
}
// Display protected content after calling is_logged_in();
*/
?>
/*=====================
The Script:
=====================*/
<?
function auth_user($username,$password) {
//This assumes you have a mysql table setup with user accounts.
$query=mysql_query("SELECT username,password FROM tbl_name WHERE username='".addslashes($username)."' AND password='".addslashes($password)."' LIMIT 1");
If(mysql_num_rows($query) > 0) {
setcookie("loggedin",true,time()+170000);
$_SESSION['loggedin']=true;
return true;
} Else {
return false;
}
return false;
}
function is_logged_in() {
If(($_COOKIE['loggedin']==true) OR ($_SESSION['loggedin']==true)) {
return true;
} Else {
return false;
}
}
function login_form() {
?>
<form method="post" action="<?Echo($_SERVER['PHP_SELF']);?>
Username: <input type="text" name="username" value="" /><br />
Password: <input type="password" name="password" value="" /><br />
<input type="hidden" name="login" value="true" />
<input type="submit" name="submit" value=" Auth Me " />
</form>
<?
}
?>


