0

I have tried to print values of my PHP object '$mon' which is having average of all months.....

and My code for data fetch is as

$result = mysql_query("SELECT b_year, sum(m1) as m1, sum(m2) as m2, sum(m3) as m3, sum(m4) as m4, sum(m5) as m5, sum(m6) as m6, sum(m7) as m7, sum(m8) as m8, sum(m9) as m9, sum(m10) as m10, sum(m11) as m11, sum(m12) as m12  
            from s_budgets as b INNER JOIN users as u on b.u_id = u.id WHERE b_year=YEAR( CURDATE( ) )
GROUP BY b_year " );
$mon=mysql_fetch_array($result)

and my code for Displaying those values are as below....

 <?php echo $mon->m4;?>,
 <?php echo $mon->m5;?>,
 <?php echo $mon->m6;?>,

how can I print the object '$mon' with its several properties?

1
  • If it is for testing purpose just use print_r($mon) or you need to loop and do. Commented Oct 22, 2012 at 9:29

4 Answers 4

2

You need to used mysql_fetch_object() rather than mysql_fetch_array() in order to access the data in an object format.

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

Comments

2

This $mon=mysql_fetch_array($result) fetch a result row as an associative array, a numeric array, or both..

echo $mon['m4'];

If you want result as object, then use following way

$mon = mysql_fetch_object($result)
$mon->m4

Please start to use Mysqli or PDO for new development.

Comments

1

$mon isn't an object because mysql_fetch_array fetches an array from a MySQL resource. You'll have to access your array like so:

<?php echo $mon['m4']; ?>,
<?php echo $mon['m5']; ?>,
<?php echo $mon['m6']; ?>,

You'll need to use mysql_fetch_object if you're wanting an object.

Comments

1

If you need it for debugging purposes, a simple

var_dump($mon);

gives you all properties of $mon.

Otherwise try iterating over the result array:

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
     echo $row['m4']; // etc.
}

1 Comment

you can also use "print_r", which is a great alternative for "var_dump"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.