Tag Archives: SQL keywords

Computer Algorithms: Linked List

Introduction

The linked list is a data structure in which the items are ordered in a linear way. Although modern programming languages support very flexible and rich libraries that works with arrays, which use arrays to represent lists, the principles of building a linked list remain very important. The way linked lists are implemented is a ground level in order to build more complex data structures such as trees.

It’s true that almost every operation we can perform on a linked list can be done with an array. It’s also true that some operations can be faster on arrays compared to linked lists.

However understanding how to implement the basic operations of a linked list such as INSERT, DELETE, INSERT_AFTER, PRINT and so on is crucial in order to implement data structures as rooted trees, B-trees, red-black trees, etc.

Overview

Unlike arrays where we don’t have pointers to the next and the previous item, the linked list is designed to support such pointers. In some implementations there is only one pointer pointing to the successor of the item. This kind of data structures are called singly linked lists. In this case the the last element doesn’t have a successor, so the pointer to its next element usualy is NULL. However the most implemented version of a linked list supports two pointers. These are the so called doubly linked lists.

Arrays vs. linked list
Arrays items are defined by their indices, while the linked list item contains a pointer to its predecessor and his successor!
Continue reading Computer Algorithms: Linked List

How to Dump the Generated Zend_Db SQL Query

The Typical PHP Approach

Typically a PHP programmer will write his SQL query as a string and will execute it via mysql_query.

$sql = "SELECT * FROM my_table";
$resource = mysql_query($sql);

So eventually when you want to dump this “complex” query, or whatever query there is, you can simply “echo” it and see what’s its syntax.

// this query is WRONG because of the where clause
$sql = "SELECT * FROM my_table WHERE id = ";
 
// dump and debug the wrong query
die($sql);
 
// this line won't be executed
$resource = mysql_query($sql);

So far so good, but things appear to be a bit different when you start to work with Zend Framework. Higher levels of abstraction come with slightly more difficult ways to dump (debug) your SQL queries.

OK you’ve two options. Using Zend_Db_Select or … not.
Continue reading How to Dump the Generated Zend_Db SQL Query

Fetching Rows With Zend_Db fetch()

Fetching the Entire Row Set

Rows

What is really handy in Zend Framework is that you can fetch the entire row set with the fetchAll() method. It comes with some parameters that you can use for limiting the result or ordering, but in general you can use it without specifying parameters. Let say this is the model:

<?php
class User extends Zend_Db_Table
{
 
	public function listAll()
	{
		$query = "SELECT * FROM user";
 
		// exec query
		$rs = $this->getAdapter()->query($query);
 
		return $rs->fetchAll();
 
	}
 
}

You can simply return the row set with the fetchAll() method as described in the example, but what if you have to loop through the rows and to modify somehow the values?

Fetching a Row

By simply change the code like so:

class User extends Zend_Db_Table
{
 
	public function listAll()
	{
		$query = "SELECT * FROM user";
 
		// exec query
		$rs = $this->getAdapter()->query($query);
 
		// fetch
		$list = array();
		while ($row = $rs->fetch()) {
			// removing the password column value
			$row['password'] = '';
 
		    $list[] = $row;
		}
 
		return $list;
	}
}

you can modify the rows and you’ll have the same result.

Which Model Should Contain That Method?

Let’s say you have two models – each one modeling a database table – Users & Article. Here there’s nothing to deal with Zend Framwork, but you can think of them as typical models in a ZF application.

What happens if you have to write a getUserArticles method? Where would you put it? Whether this will be the User model or the Article model?Where?

Although technically you can put it in both models my advice is to look at the SQL query. If the FROM clause is containing the user table – than put the method in the User model, but here you’d have something like:

SELECT * FROM Article WHERE user_id = 1

I’d prefer to place it in the Article model!