Jump to content

Mysql Query With Two Forks.


Angelojoseph17

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/271347-mysql-query-with-two-forks/
Share on other sites

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.