ExoSkeleton321 ExoSkeleton321 - 1 year ago 149
PHP Question

Print array value PHP

I'm having a little problem when trying to print certain values from my database. OK so I have a table in my database called

where I save the site name, phone and email. I have a query that returns the following array:

[0] => Array
[text] => My Store
[0] => My Store
[column_key] => site_name
[1] => site_name

[1] => Array
[text] => (123) 456 7890
[0] => (123) 456 7890
[column_key] => site_phone
[1] => site_phone

[2] => Array
[text] => email@yahoo.com
[0] => email@yahoo.com
[column_key] => site_email
[1] => site_email


I would like to print out the site details using the following code:

//Print out site name
//$site_details is the array being returned from the database
<?php echo $site_details['site_name']; ?>

This returns an

Undefined index: site_name error

. Anyone know how I could go about this? Any help is greatly appreciated.


Here's the code i use to return the site details:


public function getSiteDetails(){
global $pdo;

$getDetails = $pdo->prepare("
FROM site_details

return $getDetails->fetchAll();

This is where I call the function:


require 'res/php/Functions.php';
$obj = new Functions();

//Get site details
$site_details = $obj->getSiteDetails();

Database image:
Database Image

Answer Source

For your given array the printing mechanism is as follows:-


foreach($site_details as $site_detail){

   echo $site_detail['column_key'].' is:- '.$site_detail['text'];


Note:- fetch_assoc will be better objective.

Also every column value comes separately which shows you did something extra in your code, which is actually not needed

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download