ImpressCMS proudly uses SourceForge
ImpressCMS on Ohloh.net
[NewIndex :: Development :: This page]  
Tags: Development   Outdated   database  

Databases and Classes

Link to Original - http://www.kaotik.biz/tutorial/tutorial_part8.html

DB Operations and Classes

Series Index
Other parts of this series by Kaotik

Before I discuss database and class operations, first let me show you a small issue regarding quotes. Double and Single Quotes What is the difference between these 2 lines? echo 'hello world'; echo "hello world"; The difference is that in the second line, using double quotes, all vars inside get interpreted: $vin='hello'; echo "$vin world"; will output: hello world While with single quotes: $vin='hello'; echo '$vin world'; will output: $vin world reference material: php_single_and_double_quotes

CRUD

There are four basic functions of database (DB) operations you will most commonly use: Create, Read, Update, Delete; which are generally referred to as CRUD. In my previous tutorials, I’ve focused on Read and Create, now I will provide examples for all these functions.

Create

$query = "Insert into ".$xoopsDB->prefix("tutorial_myform")." (name, address, telephone, email) values ('$name', '$address', '$tel', '$email' )"; $res=$xoopsDB->query($query); if(!$res) { echo "error: $query"; }

Read

$query = $xoopsDB->query(' SELECT * FROM ' . $xoopsDB->prefix('tutorial_myform') " WHERE id='$myid' "); $myrow = $xoopsDB->fetchArray($query); $name = $myrow['name']; $address = $myrow['address']; $telephone = $myrow['telephone']; $email = $myrow['email'];

Update

$query = "UPDATE ".$xoopsDB->prefix("tutorial_myform")." SET deff=1 WHERE sid='$newdef' "; $res=$xoopsDB->query($query); if(!$res) echo "error: $query";

Delete

$query = "Delete from ".$xoopsDB->prefix("tutorial_myform")." WHERE sid='$test' "; $res=$xoopsDB->query($query); if(!$res) { echo "error: $query"; } Create, update and delete, when executed will return a TRUE or FALSE argument to $res, this allows us to perform a simple if statement to know if any error occurred when trying to change the database. Important: Create, update and delete require that your data be retrieved using $_POST. If you are using a different method GET, SESSION, etc then you will have to use a different query, this is a built in function of XOOPS. It's been discussed before on xoops.org forums, so do a search if you wish to learn more about this. $query = "Delete from ".$xoopsDB->prefix("tutorial_myform")." WHERE sid='$test' "; $res=$xoopsDB->queryF($query); if(!$res) { echo "error: $query"; } for further reading see dev.xoops.org: Using The Database, Database Tables

Classes – a Basic Overview

A class is basicly composed of 2 elements: 1 – Functions 2 – Global Vars (in the class) Both functions and vars can be private,public or protected, meaning that, if they are private they can only be used by the class itself, while if they are public, they can be called and used outside of the class. Note: This is a php 5 functionality. reference:php5 or php4 Important: PHP 4 end of life has been schedulued for 31-12-2007. Meaning no further updates will be provided after this date. XOOPS will also be built for php 5 funcionality sometime in 2008, so you might want to start learning some of this new OOP (reference: wikipedia, php.net) structure that php 5 provides. Also keep in mind that these new functions don't work on servers running only php 4. If you code a module that uses these features, some users won't be able to use your module. This has been debated a lot on xoops.org forums, do a search if you would like to read opinions from other developers. Functions can call on each other and also manipulate global vars. Let’s look at a practical example: Class testing { var $msg; function setMsg(){ $this->msg =5; } function getInfo(){ $lod=$this->msg; return $lod; } function chkData(){ } } Here we have a class with 3 functions and one global var. In order to use a class, you must first declare it: $test= new testing; Now var $test has access to all functions and vars of that class, you can use $test on all php pages that are included after this line, as long as you set it as global using: global $test; at the top of each subsequent php file. I can manipulate the var $msg from outside the class: $test->msg='hello'; I can also access it's function: $nan=$test->getInfo(); echo $nan; will output 5 Other beginning tutorials on classes and functions: spoono, phpfreaks, phpdeveloper, softwareprojects. XOOPS Classes XOOPS has its own classes (reference), some you already used such $xoopsDB or $xoopsUser. Open xoops/class or xoops/kernel to see some of these. As long as you declare them as global, you can use any of these in your own classes and functions. <[Languages |Databases and Classes]> Category: Welcome Index Page

Last modified on 2010/11/25 by fiammybe
This page was created on 2010/11/11 by skenow
This page has been viewed 2620 time(s)

Comments
The comments are owned by the poster. We aren't responsible for their content.
ImpressCMS proudly uses SourceForge
ImpressCMS on Ohloh.net