TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Database and classes (http://www.talkphp.com/general/1143-database-classes.html)

Haris 09-17-2007 05:02 PM

Database and classes
 
My question is how can classes help database ? I mean in the form of insert, update, delete and etc.

Shaun 09-18-2007 07:33 AM

Basically can help you cut down on repetitive code. I'd show an example, but about to go out.
will add more later

EDIT:
Lets say you wanted to update your database, i make a class that includes an 'update' function.

So below i have the query that is going to be sent to the function
PHP Code:

<?php
            $data 
= array(
                    
'movie_title' => $_POST['title'], 
                    
'movie_rating' => $_POST['movie_rating'],
                    
'movie_genre' => $_POST['genre'],
                    
'movie_synopsis' => $_POST['content'],
                    
'movie_link' => $_POST['link'],
                    
'movie_release_date' => $_POST['release_date'],
                    
'movie_running_time' => $_POST['running_time'],
                    
'movie_image' => $image
                    
);    
                
            
Sql::update'movies'$data "movieID = '" .$_POST['movieID']. "'");
?>

As you can see, i have this in an array, which makes it really easy for me to see what's what. Basically the function gets the table first, in my case its "movies", next is the data array im trying to update, and finally, what its got to match, again in my case, i want the "movieID" is the row in which i want to edit.

Next, the function:
PHP Code:

<?php
    
function update$table$data$where){
        
$query "UPDATE ".$table." SET ";
        foreach(
$data as $var => $value){
            if(
$i>0){
                
$query .= ", ";
            }
            
$query .= $var" = '" .$value"'";
            
$i++;
        }
        
$query .=" WHERE ".$where." LIMIT 1;";
        
Sql::query($query);
        
        return 
true;
    }
?>

The foreach grabs all the data from the "$data" array and puts it back together, placing a "," when needed, and then "$where" it matches, it then executes the query using my "Sql::query()" function.

I know this is only brief, but i hope that helps you a little, and im sure their a more advanced PHP coders here that could show an even better way of doing it, as im certainly not advanced lol.


All times are GMT. The time now is 09:13 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0