Angelojoseph17 Posted November 29, 2012 Share Posted November 29, 2012 public function ListProduct($Order) { $sql = "SELECT `Order`.`OrderNumber` , `Despatch`.`OrderNumber` , `Stock`.`ProductCode` , `Stock`.`Description` , `Stock`.`ProductGroup` , `Stock`.`Size` , `Stock`.`IPICODE` FROM `Stock` , `Despatch` ,`Order` WHERE `Order`.`OrderNumber` = '" . $Order. "' AND`Order`.`OrderNumber` = `Despatch`.`OrderNumber` AND`Despatch`.`Product`= `Stock`.`ProductCode` "; // echo $sql; mysql_select_db(DB_DATABASE_NAME, $this->conn); $result = mysql_query($sql, $this->conn); //$num_rows = mysql_num_rows($result); //echo "Number of rows : $num_rows"; while($row = mysql_fetch_array($result)) { if($row[0] != "") { $result = mysql_query($sql, $this->conn); $num_rows = mysql_num_rows($result); //echo "Number of rows : $num_rows"; } else { $sql = "SELECT `Order`.`OrderNumber` , `Despatch`.`OrderNumber` , `Stock`.`ProductCode` , `Stock`.`Description` , `Stock`.`ProductGroup` , `Stock`.`Size` , `Stock`.`IPICODE` FROM `Stock` , `Despatch` ,`Order` WHERE `Order`.`OrderNumber` = '" . $Order. "' AND`Order`.`ProductCode`= `Stock`.`ProductCode` "; mysql_select_db(DB_DATABASE_NAME, $this->conn); $result = mysql_query($sql, $this->conn); $num_rows = mysql_num_rows($result); //echo "Number of rows : $num_rows"; while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $return[] = $row; } return $return; return $num_rows; } } I have the query above which check if any rows are returned from the first sql statement and if the rows are empty, the aim is to run the second query and returns results. However i am going wrong somewhere. Can someone have a look. Perhaps i am over complicating this? Quote Link to comment https://forums.phpfreaks.com/topic/271347-mysql-query-with-two-forks/ Share on other sites More sharing options...
NomadicJosh Posted November 29, 2012 Share Posted November 29, 2012 You might need to change the first one to $sql1 and then do an "if $sql1 is false" then run $sql2. Quote Link to comment https://forums.phpfreaks.com/topic/271347-mysql-query-with-two-forks/#findComment-1396231 Share on other sites More sharing options...
trq Posted November 29, 2012 Share Posted November 29, 2012 What is your question / problem? Quote Link to comment https://forums.phpfreaks.com/topic/271347-mysql-query-with-two-forks/#findComment-1396232 Share on other sites More sharing options...
Christian F. Posted December 1, 2012 Share Posted December 1, 2012 First order of business is to use the [code][/code] tags around your code, and proper intending. The latter can be a bit tricky on this forum, unfortunately, but just use the "Paste as plain text" button (second from the right, top row) and it'll work. Second order of business is never to run queries inside loops; Nothing kills performance like this. Depending upon your exact intentions you'll either want to use JOINs, or a loop to construct the secondary query/queries. If you still require some assistance, please follow at least the first step. Also, f you could post a more thorough explanation of what, exactly, you want to do, then I'm sure someone here can help you. Quote Link to comment https://forums.phpfreaks.com/topic/271347-mysql-query-with-two-forks/#findComment-1396619 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.