Jump to content

How to access database fields as an array variables ?


linux1880

Recommended Posts

Hi friends,

a quick print_r() shows the following array while connecting to my database. How to get them as individual items and display on the website ?

Array
(
    [0] => Array
        (
            [id] => 52
            [document_name] => xyz
            [document_ext] => gif
            [download_file_name] => ktm_impact.gif
            [upload_dt] => 2010-08-11
        )

)

view file var is

print_r($document);

 

coltroller file is

$this->page_content['document']=$this->Model->showPics();

 

model is

function showPics(){
	$sql="select * from document";

	$query=$this->db->query($sql);
	return $query->result_array();
}

Your still being quite vague so I'll just give you a simple example.

 

foreach ($document as $doc) {
  echo $doc['id'] . '<br />';
  echo $doc['document_name'] . '<br />';
  echo $doc['document_ext'] . '<br />';
  echo $doc['download_file_name'] . '<br />';
  echo $doc['upload_dt'] . '<br />';
}

Thank you thorpe, that's using foreach, i want to access some fields without using loops, for example if i access

 

 echo $document['document_name'];

directly, it's giving me error.

 

A PHP Error was encountered

Severity: Notice

Message: Undefined index: document_name

Filename: libraries/Loader.php(673) : eval()'d code

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.