0

Possible Duplicate:
Output (echo/print) everything from a PHP Array

I have done a query from a database and the result is stored in a variable which is believe is an array. The output is only one row and column so i use:

echo result[0]; 

to output the result. However i get an error saying:

Notice: Array to string conversion in "C:/apache/htdocs...." array 

I tried to dump the variable using

var_dump result[0]; 

I then get this

array(1) { [0]=> array(1) { ["var_datain"]=> string(4) "hai!" } } 

So.... how do i get it to echo out the value hai! from that array?

In case it matters, here is my query

 $db = new PDO(DSN, DBUSER, DBPASS); $stmt = $db->prepare("CALL test(?)"); $parameter = 'hai!'; $stmt->bindValue(1, $parameter, PDO::PARAM_STR); $rs = $stmt->execute(); $result = $stmt->fetchAll(PDO::FETCH_ASSOC); var_dump($result); 
0

1 Answer 1

2

It's a multidimensional array.

echo $result[0]['var_datain']; 

It depends of the way you did your query (show us more code to explain you better) but it looks that in your case each line returned by your query is another array containing the columns that you selected. Even if you select only one col and only one row, it's still returned as a multidimensional array.

Got it?

Sign up to request clarification or add additional context in comments.

4 Comments

awesome! but i should be also able to do $result[0][0] right? coz im refering to its indexes but i get an error: Notice: Undefined offset: 0
@VidhuShresthBhatnagar You are doing an associative fetch via PDO::FETCH_ASSOC, so you will not receive numeric keys back, only the associative keys from column names. You must therefore use $result[0]['var_datain'].
Edit: If you want to use $result[0][0] put PDO::FETCH_NUM instead of PDO::FETCH_ASSOC. You can also use PDO::FETCH_BOTH that will return both $result[0][0] and $result[0]['var_datain']
hi, i just did array_values($result), i could then do $result[0][0] but ill change over to using PDO::FETCH_NUM. Thanks Phius

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.