PHP Classes

PHP Query MSSQL Database: Run common SQL queries on MSSQL database tables

Recommend this page to a friend!
  Info   View files Documentation   View files View files (3)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2023-08-24 (29 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 42 This week: 2All time: 10,668 This week: 59Up
Version License PHP version Categories
mssql_database 0.2.0MIT/X Consortium ...7PHP 5, Databases
Description 

Author

This class can run common SQL queries on MSSQL database tables.

It can connect to a given Microsoft SQL server using the PHP MSSQL extension.

The class can also execute common SQL queries using parameters passed to the class functions. Currently, it can:

- Execute a SQL query using a given condition and retrieve a single result row or all result rows

- Update a given table with field values and a condition

- Delete table records using a condition

- Insert a table record with given field values and get the identifier value of an inserted record

Picture of Tahir Ali Channa
Name: Tahir Ali Channa <contact>
Classes: 1 package by
Country: Pakistan Pakistan

Details

PHP MSSQL Database Class (Database.php)

The PHP Database Class is a utility for simplifying database operations in PHP applications. It provides an object-oriented interface for connecting to a database, executing queries, and performing CRUD (Create, Read, Update, Delete) operations on database records. This class is designed to work with MSSQL databases and offers methods for executing various types of SQL queries.

Features

  • Connect to a MSSQL database.
  • Execute SELECT, INSERT, UPDATE, and DELETE queries.
  • Retrieve single rows or multiple results.
  • Handle query parameters and data binding.

Installation

  1. Clone this repository or download the `Database.php` file.
  2. Include the class in your PHP project:
require_once 'path/to/Database.php';

Usage

DB Connection

$db = new Database($dbuser, $dbpass, $dbname, $dbhost);

// OR use a helper function
function db() {
    global $db;
    return $db;
}

SELECT Single Row

$query  = "SELECT * FROM [users] WHERE UserID = ? AND Active = ?";
$params = array((int)$userid, 1);

// Get as object
$user = db()->get_row($query, $params);
echo $user->Username;
echo $user->Email;

// OR get as an associative array
$user = db()->get_row($query, $params, "array");
echo $user['Username'];
echo $user['Email'];

SELECT Multiple Rows

$query = "SELECT * FROM [users] WHERE Active = ?";
$params = array(1);

$users = db()->get_results($query, $params);
foreach ($users as $user) {
    echo $user->Username;
    echo $user->Email;
}

INSERT Query

$table = "User";
$fields = array("Username", "Email");
$params = array($data['Username'], $data['Email']);
$db->insert($table, $fields, $params);

UPDATE Query

$what   = array("Username", "Email");
$where  = array("UserID");
$params = array($data['Username'], $data['Email'], $data['UserID']);
$db->update("User", $what, $where, $params);

DELETE Query

$table   = "User";
$where   = array("UserID");
$params  = array($id);
$deleted = $db->delete($table, $where, $params);
if ($deleted) {
    echo "Record deleted successfully.";
} else {
    echo "Deletion failed.";
}

Important Note

This class working with MSSQL databases. Make sure to handle database connections, credentials, and security properly in your application.

License

This project is licensed under the MIT License.

  Files folder image Files  
File Role Description
Plain text file database.php Class Class source
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

 Version Control Unique User Downloads Download Rankings  
 100%
Total:42
This week:2
All time:10,668
This week:59Up