Return to Snippet

Revision: 55920
at February 29, 2012 12:22 by iceman


Updated Code
<?php
//Written by: Shreyas Basarge


define("db_server","localhost");

define("db_user","root");

define("db_password","root");



class mysql

{



var $conn;

var $error;



function get_error() {

	return $this->error;

}



function connect($dbname) {

	$this->conn = mysql_connect(db_server, db_user, db_password);

	if (!!$this->conn)

	{

		mysql_select_db($dbname, $this->conn);


		return true;

	}

	

	$this->error = mysql_error();

	return false;

}



function insert($table, $dataarray)

{

	$i = 0; $data = '';

	while(isset($dataarray[$i]))

	{

		$data .= "'" . $dataarray[$i] . "'";

		if (isset($dataarray[$i+1]))  $data .= ", ";

		$i++;

	}

	if(mysql_query("INSERT INTO $table VALUES ($data)", $this->conn))

	{

		return true;

	}

	else

	{

		$this->error = mysql_error($this->conn);

		return false;

	}

}



function query($q)

{

	if($result = mysql_query($q, $this->conn))

	{

		return $result;

	}

	else

	{

		$this->error=mysql_error($this->conn);

		return false;

	}

}



function update($table, $fields, $values, $clause)

{

	if (count($fields) != count($values))

	{

		return false;

	}

	$sql = "UPDATE $table SET ";

	$i=0;

	while(isset($fields[$i]))

	{

		$sql = $sql . $fields[$i] . " = '" . $values[$i] . "'";

		if (isset($fields[$i])) $sql = $sql + ', ';

		$i++;

	}

	$sql = $sql + " WHERE $clause";

	

	return mysql_query($sql);

	

}



function max($table, $field)

{

	$result = mysql_query("SELECT MAX($field) FROM $table", $this->conn);

	$row = mysql_fetch_array($result);

	return $row["MAX($field)"];

}



function getfields($table)

{

	if($result = mysql_query("SHOW COLUMNS FROM $table", $this->conn))

	{

		$cms = mysql_fetch_assoc($result);

		return $cms['Field'];

	}

	else

	{

		$this->error = mysql_error($this->conn);

		return false;

	}

}



function getrows($table,$fields, $clause=NULL, $order=NULL, $llimit=NULL, $ulimit=NULL)

{

	$sql = "SELECT ";

	$sql .= ($fields != '')? $fields . " " : "* ";

	$sql .= "FROM $table ";

	if (isset($clause)) $sql .= "WHERE $clause ";

	if ($order != '') $sql .= "ORDER BY $order ";

	if (isset($clause)) $sql .= "LIMIT $llimit, $ulimit";

	$sql .= ";";

	


	if($result = mysql_query($sql))

	{

		return $result;

	}

	else

	{

		$this->error=mysql_error($this->conn);

		return false;

	}

}



function getrow($table, $fields=NULL, $clause=NULL)

{

	$rows = $this->getrows($table, $fields, $clause, '', 0, 1);

	

	if (mysql_num_rows($rows))

		return mysql_fetch_array($rows);

	else

		return false;

	

}



function nextrow($rows)

{

	return mysql_fetch_array($rows);

}



function delete($table, $clause)

{

	mysql_query("DELETE * FROM $table WHERE $clause");

}



function close()

{

	mysql_close();

}



}

?>

Revision: 55919
at February 29, 2012 07:17 by iceman


Initial Code
<?php



define("db_server","localhost");

define("db_user","root");

define("db_password","root");



class mysql

{



var $conn;

var $error;



function get_error() {

	return $this->error;

}



function connect($dbname) {

	$this->conn = mysql_connect(db_server, db_user, db_password);

	if (!!$this->conn)

	{

		mysql_select_db($dbname, $this->conn);

		//$this->error = 'ding';

		return true;

	}

	

	$this->error = mysql_error();

	return false;

}



function insert($table, $dataarray)

{

	$i = 0; $data = '';

	while(isset($dataarray[$i]))

	{

		$data .= "'" . $dataarray[$i] . "'";

		if (isset($dataarray[$i+1]))  $data .= ", ";

		$i++;

	}

	//echo "INSERT INTO $table VALUES ($data)";

	if(mysql_query("INSERT INTO $table VALUES ($data)", $this->conn))

	{

		return true;

	}

	else

	{

		$this->error = mysql_error($this->conn);

		return false;

	}

}



function query($q)

{

	if($result = mysql_query($q, $this->conn))

	{

		return $result;

	}

	else

	{

		$this->error=mysql_error($this->conn);

		return false;

	}

}



function update($table, $fields, $values, $clause)

{

	if (count($fields) != count($values))

	{

		return false;

	}

	$sql = "UPDATE $table SET ";

	$i=0;

	while(isset($fields[$i]))

	{

		$sql = $sql . $fields[$i] . " = '" . $values[$i] . "'";

		if (isset($fields[$i])) $sql = $sql + ', ';

		$i++;

	}

	$sql = $sql + " WHERE $clause";

	

	return mysql_query($sql);

	

}



function max($table, $field)

{

	$result = mysql_query("SELECT MAX($field) FROM $table", $this->conn);

	$row = mysql_fetch_array($result);

	return $row["MAX($field)"];

	//return mysql_fetch_array(mysql_query("SELECT MAX($field) FROM $table", $this->conn))["MAX($field)"];

}



function getfields($table)

{

	if($result = mysql_query("SHOW COLUMNS FROM $table", $this->conn))

	{

		$cms = mysql_fetch_assoc($result);

		return $cms['Field'];

	}

	else

	{

		$this->error = mysql_error($this->conn);

		return false;

	}

}



function getrows($table,$fields, $clause=NULL, $order=NULL, $llimit=NULL, $ulimit=NULL)

{

	$sql = "SELECT ";

	$sql .= ($fields != '')? $fields . " " : "* ";

	$sql .= "FROM $table ";

	if (isset($clause)) $sql .= "WHERE $clause ";

	if ($order != '') $sql .= "ORDER BY $order ";

	if (isset($clause)) $sql .= "LIMIT $llimit, $ulimit";

	$sql .= ";";

	

	//echo $sql;

	if($result = mysql_query($sql))

	{

		return $result;

	}

	else

	{

		$this->error=mysql_error($this->conn);

		return false;

	}

}



function getrow($table, $fields=NULL, $clause=NULL)

{

	$rows = $this->getrows($table, $fields, $clause, '', 0, 1);

	

	if (mysql_num_rows($rows))

		return mysql_fetch_array($rows);

	else

		return false;

	

}



function nextrow($rows)

{

	return mysql_fetch_array($rows);

}



function delete($table, $clause)

{

	mysql_query("DELETE * FROM $table WHERE $clause");

}



function close()

{

	mysql_close();

}



}

?>

Initial URL

                                

Initial Description
Write less and do more mysql in php.

Initial Title
Quick mysql in php

Initial Tags
mysql, php

Initial Language
PHP