MajorVictory Posted August 17, 2006 Share Posted August 17, 2006 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 Quote Link to comment Share on other sites More sharing options...
fenway Posted August 17, 2006 Share Posted August 17, 2006 Not sure what kind of response you'll get, but I would recommend posting links to the code, rather that posting the code itself. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted August 17, 2006 Share Posted August 17, 2006 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. Quote Link to comment Share on other sites More sharing options...
MajorVictory Posted August 17, 2006 Author Share Posted August 17, 2006 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 Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted August 17, 2006 Share Posted August 17, 2006 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. Quote Link to comment Share on other sites More sharing options...
MajorVictory Posted August 17, 2006 Author Share Posted August 17, 2006 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() functioni'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. Quote Link to comment Share on other sites More sharing options...
MajorVictory Posted August 25, 2006 Author Share Posted August 25, 2006 :EDIT: I fixed that last onenow 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. Quote Link to comment Share on other sites More sharing options...
fenway Posted August 28, 2006 Share Posted August 28, 2006 Remove the "NULLs" -- columns default to NULLable unless you say "NOT NULL"; if you mean "DEFAULT NULL", that's something else. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.