Using e107's built-in classes and methods.

Database

Retrieving the database class object

		$sql = e107::getDb();

select()

Selecting data from a database table:

		$sql = e107::getDb();
$sql->select('tablename', 'field1, field2', 'field_id = 1');

fetch()

Selecting, looping through and displaying selected data with the fetch() method:

		$sql = e107::getDb();
$sql->select('tablename', 'field1, field2', 'field_id = 1');
while($row = $sql->fetch())
{
    echo $row['field1'];
}

insert()

Inserting data into a database table:

		$insert = array(
    'user_id' => 1,
    'user_email' => '@'
);

$sql->insert('user', $insert); // where 'user' is the table name

update()

Updating information in a database:

		$update = array(
    'user_email' => '@',
    // ... Long list of fields/values
    'WHERE' => 'user_id = 1'
);

$sql->update('user', $update); // where 'user' is the table name.

retrieve()

Combined select() and fetch() method.

Example: Get single value

		$string = $sql->retrieve('user', 'user_email', 'user_id=1');


Example: Get multiple table-row values

		if($allRows = $sql->retrieve('user', 'user_name, user_email', '', true))
{
	foreach($allRows as $row)
	{
		echo $row["user_name"]." - ".$row["user_email"]."<br/>";  
	}
}

delete()

Delete a record from a database table.

		$sql->delete("user", "user_id=2");

gen()

Generic query function

Example: perform a JOIN with gen():

		$sql->gen("SELECT f.*,u.user_name FROM #faqs AS f LEFT JOIN #users as u ON f.faq_author = u.user_id ");

Tuesday 05 July 2016 - 09:43:25 Néstor Sabater,

Social Links