Ronaldmi Posted July 27, 2016 Share Posted July 27, 2016 I having an issue with a query output where is only outputing one product instead of all the products matching WHERE shelf='KDR'. Here is the code <?php include_once('../../header.php'); ?> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> <script src="https://code.highcharts.com/highcharts.js"></script> <script src="https://code.highcharts.com/highcharts-3d.js"></script> <script src="https://code.highcharts.com/modules/exporting.js"></script> <script type="text/javascript"> $(function () { $('#KDR').highcharts({ chart: { type: 'pie', options3d: { enabled: true, alpha: 45 } }, title: { text: 'Current Inventory' }, plotOptions: { pie: { innerSize: 100, depth: 45 } }, series: [{ name: 'In Stock', data: [ <?php $query_products = mysql_query("SELECT * FROM product, product_amount WHERE shelf='KDR'"); while($list_products = mysql_fetch_assoc($query_products)) { $products['id'] = $list_products['id']; $products['status'] = $list_products['status']; $products['code'] = $list_products['code']; $products['name'] = $list_products['name']; $products['amount'] = $list_products['amount']; $dump = array(); } $result1= $products['name']; $result2= $products['amount'] ?> ['<?php echo $result1?>', <?php echo $result2?>], ] }] }); }); </script> <div class="row"> <H1>KDR</H1> <div id="KDR" style="width:50%; height:400px;"> </div> </div> <?php include_once('../../footer.php'); ?> he querry appears to work, but when I echo the result I get:['product 1 name', 'product 1 amount' ],instead of :['product 1 name', 'product 1 amount' ],['product 2 name', 'product 2 amount' ],['product 3 name', 'product 3 amount' ],['product 4 name', 'product 4 amount' ],['product 5 name', 'product 5 amount' ],['product 6 name', 'product 6 amount' ], attached are images of the tables structure any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/301658-mysql-query-output/ Share on other sites More sharing options...
cyberRobot Posted July 27, 2016 Share Posted July 27, 2016 Every time the loop executes, the $products variable is being overridden. So the output will only contain the last record. You could try something like this: $products = array(); while($list_products = mysql_fetch_assoc($query_products)) { $products[]['id'] = $list_products['id']; $products[]['status'] = $list_products['status']; $products[]['code'] = $list_products['code']; $products[]['name'] = $list_products['name']; $products[]['amount'] = $list_products['amount']; $dump = array(); } And then use a foreach loop to output the products. Quote Link to comment https://forums.phpfreaks.com/topic/301658-mysql-query-output/#findComment-1535161 Share on other sites More sharing options...
ginerjm Posted July 27, 2016 Share Posted July 27, 2016 You need to look at your code. You move the fetched array into another array. And Repeat. Why? If you want to output the results, then output them instead of just copying them to the same array over and over and over again... Quote Link to comment https://forums.phpfreaks.com/topic/301658-mysql-query-output/#findComment-1535162 Share on other sites More sharing options...
cyberRobot Posted July 27, 2016 Share Posted July 27, 2016 You need to look at your code. You move the fetched array into another array. And Repeat. Why? If you want to output the results, then output them instead of just copying them to the same array over and over and over again... ...assuming that's all the OP wants to do. But good point. Quote Link to comment https://forums.phpfreaks.com/topic/301658-mysql-query-output/#findComment-1535165 Share on other sites More sharing options...
benanamen Posted July 27, 2016 Share Posted July 27, 2016 You are using obsolete Mysql_* code that has been completely removed from Php. You need to use PDO. https://phpdelusions.net/pdo 1 Quote Link to comment https://forums.phpfreaks.com/topic/301658-mysql-query-output/#findComment-1535169 Share on other sites More sharing options...
Ronaldmi Posted July 27, 2016 Author Share Posted July 27, 2016 So, what would be the recommend way to execute the query and output? Quote Link to comment https://forums.phpfreaks.com/topic/301658-mysql-query-output/#findComment-1535176 Share on other sites More sharing options...
ginerjm Posted July 27, 2016 Share Posted July 27, 2016 There are two other main interfaces that are part of PHP. The mysqlI one and PDO. Many people prefer PDO which has a small learning curve but is easy once you've used it a couple fo times, IMHO. As told, the MySQL interface is so old that PHP has stopped supporting it since php 7. Use it at your own risk because once your host moves to that level your code will no longer work. Quote Link to comment https://forums.phpfreaks.com/topic/301658-mysql-query-output/#findComment-1535177 Share on other sites More sharing options...
mac_gyver Posted July 27, 2016 Share Posted July 27, 2016 the OP has already been told all of these things and more, such as what the sql join query needs to be to retrieve the correct data, on another help site, but hasn't made use of any of the information. save your time responding. Quote Link to comment https://forums.phpfreaks.com/topic/301658-mysql-query-output/#findComment-1535182 Share on other sites More sharing options...
ginerjm Posted July 29, 2016 Share Posted July 29, 2016 He needs to give up on programming. Maybe get a McJob? Quote Link to comment https://forums.phpfreaks.com/topic/301658-mysql-query-output/#findComment-1535283 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.