Solarpitch Posted January 6, 2009 Share Posted January 6, 2009 Hi, Can someone clear this up for me please? I'm trying to get the value of the query by "echo $row;" but this just prints out an Object ID#4 or something. Is it because row contains both the value and the reference to it ie. the Object ID? How do I correctly access the value queried in $row? <?php $query = $this->client->query("SELECT SUM(price) FROM sales WHERE ticketnumber = ".$ticketnumber.""); $row = $query->row(); ?> Link to comment https://forums.phpfreaks.com/topic/139667-solved-small-question-regarding-database-results/ Share on other sites More sharing options...
trq Posted January 6, 2009 Share Posted January 6, 2009 Wouldn't know because you appear to be using some third party db abstraction layer. One would assume however that your query method is returning a result resource (same as mysql_query does) and needs to be passed to something like mysql_fetch_assoc in order to actually be usefull. Link to comment https://forums.phpfreaks.com/topic/139667-solved-small-question-regarding-database-results/#findComment-730739 Share on other sites More sharing options...
trq Posted January 6, 2009 Share Posted January 6, 2009 A clearer (maybe) example using php's standard mysql extension without any abstraction (and a clearer query). $sql = "SELECT SUM(price) AS price FROM sales WHERE ticketnumber = $ticketnumber LIMIT 1"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { $row = mysql_fetch_assoc($result); echo $row['price']; } } Link to comment https://forums.phpfreaks.com/topic/139667-solved-small-question-regarding-database-results/#findComment-730740 Share on other sites More sharing options...
trq Posted January 6, 2009 Share Posted January 6, 2009 Actually after reading your post again it would appear query simply returns another object. Where did you get this abstraction layer from and what do the docs say is returned by query? Link to comment https://forums.phpfreaks.com/topic/139667-solved-small-question-regarding-database-results/#findComment-730742 Share on other sites More sharing options...
Solarpitch Posted January 6, 2009 Author Share Posted January 6, 2009 I'm using the framework Codeigniter so thats why it appears as a third party db abstraction. Below is an example of a function I have that works perfect with the same idea. <?php function credit_sales() { //Using Codeigniter this will produce select sum(price) as price_total from sales where type = 'credit' $this->client->select_sum('price', 'price_total'); $query = $this->client->get_where('sales', array('type' => 'credit'); $row = $query->row(); return $row->price_total; //THIS WILL OUTPUT THE CORRECT RESULT: 899.99 or whatever } ?> But when I apply the same context ($row->price_total;) to the query I'm trying to use, it doesnt seem to work. (Edit: When I say doesnt work, I mean it prints out "Object ID#4") Link to comment https://forums.phpfreaks.com/topic/139667-solved-small-question-regarding-database-results/#findComment-730744 Share on other sites More sharing options...
trq Posted January 6, 2009 Share Posted January 6, 2009 Im sure if you used the query I posted above you would get the same result. The problem is you don't define the alias price_total anywhere in your query. I assume (never used Codeignitor) that thats what the second argument to select_sum does (defines an alias). Link to comment https://forums.phpfreaks.com/topic/139667-solved-small-question-regarding-database-results/#findComment-730745 Share on other sites More sharing options...
Solarpitch Posted January 6, 2009 Author Share Posted January 6, 2009 It does yeah. Not to worry, I'll play around with it here anyway. Your query works fine but ill try and get it to work using the Codeigniter way. It may be because I have a similar query in the same function and somewhere along the lines its getting messed up. Thanks for your help. Link to comment https://forums.phpfreaks.com/topic/139667-solved-small-question-regarding-database-results/#findComment-730748 Share on other sites More sharing options...
trq Posted January 6, 2009 Share Posted January 6, 2009 I'm sure my query would also work fine with the code you posted in your original post. eg; <?php $query = $this->client->query("SELECT SUM(price) AS price FROM sales WHERE ticketnumber = $ticketnumber LIMIT 1"); $row = $query->row(); echo $row->price; ?> Link to comment https://forums.phpfreaks.com/topic/139667-solved-small-question-regarding-database-results/#findComment-730749 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.