Jump to content


Photo

I'm making my own MySQL PHP Engine...


  • Please log in to reply
7 replies to this topic

#1 MajorVictory

MajorVictory
  • New Members
  • Pip
  • Newbie
  • 4 posts
  • LocationIllinois, America

Posted 17 August 2006 - 03:20 AM

Hello everyone, over the last week or two i've been teaching myself php and updating myself on html.

I've gotten to the point where i need to use MySQL to store data and retrieve it later. So far I understand most of the commands and syntax but there's still areas i need to explore. Over the last two days i've been learning MySQL and trying to make my own set of functions to access the DB easily and without a lot of code in my other scripts.

right now the functions i have include connecting to the DB, getting data, setting data, and updating data. Now I havn't tried to test my code because i want to finish the functions as much as possible before i need to test. I know it can be a tough way to code but it's how I learn best.

What I want from this community is a little help in making sure my functions are coded correctly.
I also want to ask if it is ok if i post these functions here. I want to post the functions i do have done here and have you guys evaluate them and tell me if i'm on the right track and if not, how far off-base I am.

For now i won't post any code until i get approval to do so as I know some forums disapprove of large text chunks.

Thanks,
-Major

#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 17 August 2006 - 01:10 PM

Not sure what kind of response you'll get, but I would recommend posting links to the code, rather that posting the code itself.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 17 August 2006 - 03:38 PM

You can post the code, as long as your use the code tags:
[ code]YOU CODE HERE[ /code] (without the spaces before the [ characters)

DO NOT post the code without the code tags. NOTE posts are limited 40,000 chars.

#4 MajorVictory

MajorVictory
  • New Members
  • Pip
  • Newbie
  • 4 posts
  • LocationIllinois, America

Posted 17 August 2006 - 05:26 PM

alright thanks, just wanted to make sure before i poated.

Here are my functions, just tell me if you think thay'll work and what you would recommend.
$INFO = array(
//Admin Settings
	"admin_username"	=> "adminname",
	"admin_password"	=> "adminpass",	
//mySQL Settings
	"dbHost"	=> "host",
	"dbName"	=> "name",
	"dbUser"	=> "dbname",
	"dbPass"	=> "pass",
	"cid"		=> "",
	"retid"		=> "",
//Application Url(Use Trailing Slash)
	"app_url"	=> "href",
);
function DBConnect($a_user,$a_pass) {
	global $INFO
	if ($a_user != $INFO['admin_username'] | $a_pass != $INFO['admin_password']) {
		include_once('./xtemplate.class.php');
		$xtpl = new XTemplate('templates/confirmation.xtpl');
		$xtpl -> assign('MESSAGE', "The Username or Password is incorrect!");
		$xtpl -> assign('PAGETITLE', "Error");
		$headerfile = "None"; //Full, Nav, Main, or None
		include_once('./headerfooter.php');
		$xtpl -> rparse('main.inc');
		$xtpl -> rparse('main.goback');
		$xtpl -> parse('main');
		$xtpl -> out('main');
		die("");
	} else {
		$usr = $INFO['dbUser'];
		$pwd = $INFO['dbPass'];
		$host = $INFO['dbHost'];
		$INFO['cid'] = mysql_connect($host,$usr,$pwd);
	}
}

function DBAddData($tablename = '', $category = '', $data) {
	global $INFO
	if (!$INFO['cid']) {
		echo("ERROR: " . mysql_error() . "\n");
	} else {
	$SQL = " INSERT INTO '$tablename' ";
	$SQL = $SQL . " ('$category') VALUES ";
	$SQL = $SQL . " ('$data') ";
	$result = mysql_db_query($INFO['dbName'],"$SQL",$INFO['cid']);
		if (!$result) { 
			echo("ERROR: " . mysql_error() . "\n$SQL\n");
		}
	}
}

function DBGetData ($tablename = '', $category = '') {
	global $INFO
	$SQL = " SELECT * FROM '$tablename ";
	$SQL = $SQL . " WHERE category = '$category' ";
	$INFO['retid'] = mysql_db_query($INFO['dbName'], $SQL, $INFO['cid']);
		if (!$INFO['retid']) {
			echo("ERROR: " . mysql_error() . "\n$SQL\n");
		}
	$row = mysql_fetch_array($INFO['retid']);
	while ($row = mysql_fetch_array($INFO['retid'])) { 
		$returndata = $row["$data"]; 
	}
	return $returndata
}

function DBUpdateData ($tablename = '', $category = '', $data) {
	global $INFO
	if (!$INFO['cid']) {
		echo("ERROR: " . mysql_error() . "\n");
	} else {
	$SQL = " UPDATE '$tablename' SET";
    $SQL = $SQL . " WHERE '$category' = $data ";
    $result = mysql_db_query($db,"$SQL",$cid);
		if (!$result) {
			echo("ERROR: " . mysql_error() . "\n$SQL\n");
		}
	}
}

function DBClose() {
	global $INFO
	mysql_close($INFO['cid']);
}

let me know what you think,
-Major

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 17 August 2006 - 05:52 PM

I'm not sure on this, howver from looking at it doesnt look like the functions are robust enable, such as multiple WHERE Clause arguments etc. However there are a few errors I noticed which I'll list below.

In a few places you have missing end braces - }
This is due to indentation errors. Especially in the else statements.

Also you are using a variable called $INFO within functions. This will not work as functions have there own variable scope and do not search for variables that where created in the global scope (outside of the function). You'll want to define $INFO as global within the function by adding the following as the first line of code (after the opening { brace) within the function
global $INFO;
. That way the function will have access to the $INFO variable.

#6 MajorVictory

MajorVictory
  • New Members
  • Pip
  • Newbie
  • 4 posts
  • LocationIllinois, America

Posted 17 August 2006 - 06:47 PM

ok i found those closing brace errors you mentioned and I added the globals in. I also replaced the code in my first post with the new code.

now i have a question though,

for my DBGetData() function

i'm using
$SQL = " SELECT * FROM '$tablename ";
	$SQL = $SQL . " WHERE category = '$category' ";
i want to be able to specify the tablename and category and then have it return everything in that category. The rest of the function seem like it would work bu i havn't tested it yet.

i just wann know if it shouldn't be
$SQL = " SELECT * FROM '$tablename ";
	$SQL = $SQL . " WHERE '$category'  = somethingelse ";

I'm not sure but i think the first code specifies a category called "category" and not the one i'm defining when i call my function.



#7 MajorVictory

MajorVictory
  • New Members
  • Pip
  • Newbie
  • 4 posts
  • LocationIllinois, America

Posted 25 August 2006 - 09:37 PM

:EDIT: I fixed that last one

now can anyone tell me why this function:
function DBCreateTable_Static() {
	GLOBAL $INFO;	
	if (!$INFO['cid']) {
		echo("<hr>ERROR: " . mysql_error() . "<hr>");
		DBClose();
	} else {
		$SQL = "CREATE TABLE nerdcavedata ( 
			unique_id AUTO_INCREMENT NOT NULL,
			itemname VARCHAR(40) NULL,
			itemlink VARCHAR(100) NULL,
			datestamp VARCHAR(50) NULL );";
		$result = mysql_db_query($INFO['dbName'], "$SQL" ,$INFO['cid']);
		if (!$result) { 
			echo("<hr>ERROR: " . mysql_error() . "<hr><br>$SQL<br><hr>");
			DBClose();
		}
	}

}

produces this error:

ERROR: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'AUTO_INCREMENT NOT NULL, itemname VARCHAR(40) NULL, ite


while using this SQL statement:

CREATE TABLE nerdcavedata ( unique_id AUTO_INCREMENT NOT NULL, itemname VARCHAR(40) NULL, itemlink VARCHAR(100) NULL, datestamp VARCHAR(50) NULL );


Please help me as i've followed the manual to the letter, even copying and pasting but it will not work. I am connected to the database and i do have access to all the commands.

#8 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 28 August 2006 - 10:07 PM

Remove the "NULLs" -- columns default to NULLable unless you say "NOT NULL"; if you mean "DEFAULT NULL", that's something else.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users