No-Queries MySQL

Setting Up


Set up constants settings and include the database class, as shown in the fugure:

Settings

And thats it.

Usage


Lets take an Example table:

Table

 

To insert a record into it, we make an array and fill it with the desired values:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
    define("DATABASE","test");
    define("HOST","localhost");
    define("USER","root");
    define("PASS","");
    include "database.class.php";
    $data = new Database();       // database class object
    $arr = array();               //array to store data to insert into the table   
    //fill the array with all the table fields   
    $arr["user"] = "john";     
    $arr["pass"] = "abc";
    $arr["firstname"] = "John";
    $arr["lastname"] = "Carter";
    // insert the data
    $count  $data->insert(" users ", $arr);
    echo $count;                //number of rows inserted
?>

The insert function takes an associative array of values and insert those values into the database.

 

To update a row inside your table:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
    define("DATABASE","test");
    define("HOST","localhost");
    define("USER","root");
    define("PASS","");
    include "database.class.php";
    $data = new Database();      // database class object
    $arr = array();              //array to store data to insert into the table   
    //fill the array with new table fields   
    $arr["user"] = "john";     
    $arr["pass"] = "abc";
    $arr["firstname"] = "John";
    $arr["lastname"] = "Rambo";
    // update function, third argument is WHERE clause
    $count  $data->update(" users ", $arr , " `user`= 'john' ");
    echo $count;                // number of rows updated
?>

Update function takes table name, array of updated values and WHERE clause values.

 

To select a row, we use select function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
    define("DATABASE","test");
    define("HOST","localhost");
    define("USER","root");
    define("PASS","");
    include "database.class.php";
    $data = new Database();   // database class obejct
    // select function, third argument is WHERE clause
    $count  $data->select(" * ", " users ", " `user` = 'john' ");
    // getResults function returns the dataset of results
    while($row = $data->getResults()){
    echo $row["firstname"];
    }
    // you can also get results in an abject
    $count  $data->select(" * ", " users ", " `user` = 'john' ");
    while ($row = $data->getObjectResults()){
        echo $row->lastname;
    }
    
?>

getResults() function returns the result of the last query, getObjectResults() returns the result in an object.

 

To delete a row, we use the delete function:

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
    define("DATABASE","test");
    define("HOST","localhost");
    define("USER","root");
    define("PASS","");
    include "database.class.php";
    $data = new Database();       // database class object
    // delete function, third argument is WHERE clause
    $count  $data->delete(" users ", " `user` = 'john' ");
    echo $count;                  // number of rows deleted
?>

 

Other Features


Real Escape function:

1
$string = $data->escape($string);