Jump to content

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


MajorVictory

Recommended Posts

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
Link to comment
Share on other sites

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.
[code]$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']);
}[/code]

let me know what you think,
-Major
Link to comment
Share on other sites

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 [code=php:0]global $INFO;[/code]. That way the function will have access to the $INFO variable.
Link to comment
Share on other sites

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[code]$SQL = " SELECT * FROM '$tablename ";
$SQL = $SQL . " WHERE category = '$category' ";[/code]
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[code]$SQL = " SELECT * FROM '$tablename ";
$SQL = $SQL . " WHERE '$category'  = somethingelse ";[/code]

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.

Link to comment
Share on other sites

  • 2 weeks later...
:EDIT: I fixed that last one

now can anyone tell me why this function:[code]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();
}
}

}[/code]

produces this error:[quote]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[/quote]

while using this SQL statement:[quote]CREATE TABLE nerdcavedata ( unique_id AUTO_INCREMENT NOT NULL, itemname VARCHAR(40) NULL, itemlink VARCHAR(100) NULL, datestamp VARCHAR(50) NULL );[/quote]

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.