PHP Classes

File: Example

Recommend this page to a friend!
  Classes of Aleksey Nemiro   MySql Manager   Example   Download  
File: Example
Role: Example script
Content type: text/plain
Description: Example usage of MySql class
Class: MySql Manager
Query MySQL databases using parameter lists
Author: By
Last change: include the code to the php block :)
Date: 12 years ago
Size: 1,983 bytes
 

Contents

Class file image Download
<?
define
("DB_NAME", "database_name"); // database name
define("DB_USER", "database_user"); // database user
define("DB_PASSWORD", "1234567890"); // database password
define("DB_HOST", "localhost"); // database server address
define("DB_CHARSET", "utf8"); // database codepage
define("DB_COLLATE", "");
define("DB_DEBUG", false); // true - debug mode, or false - no debug
define("DB_MODE", 2); // connection mode:
// 0 - manula, you need use Open and Close methods
// 1 - automatically by query (NO recommended),
// 2 - automatically (recommended)

// include the class
require_once "system.data.php";

// create new instance
$MySql = new MySql();

###################################################
// get table example
$t = $MySql->GetTable("SELECT * FROM users");

if (
$t != NULL && count($t) > 0)
{
 
// each rows
 
foreach ($t as $r)
 {
  echo
$r["nickname"]."<br />"; // show nickname column
 
}
}
else
{
 echo
"Data not found, sorry..";
}
###################################################

// get single row example
$userId = 1; // user id
$r = $MySql->GetRow("SELECT * FROM users WHERE id_users = %s", array($userId));
if (
$r != NULL)
{
 echo
$r["nickname"];
}
else
{
 echo
"User $userId no found.";
}

// or more parameters
$login = "admin";
$password = "123456789";
$r = $MySql->GetRow("SELECT * FROM users WHERE [login] = %s AND [password] = %s", array($login, $password));
if (
$r != NULL)
{
 echo
$r["nickname"];
}
else
{
 echo
"User no found.";
}
###################################################

// remove data example
$userId = 1; // user id
$MySql->ExecuteNonQuery("DELETE FROM users WHERE id_users = %s", array($userId));
###################################################

// add data example
$newId = $MySql->ExecuteScalar("INSERT INTO users (nickname, email, [login], [password])", array("Ivan", "vasya@kbyte.ru", "mylogin", "1234567890"));
echo
"Created new user with id = $newId";
?>