PHP - Trying To Merge 2 Queries Using Union
The queries merge ok and results are correct except I get a notice: "Notice: Use of undefined constant UNION - assumed 'UNION' in...."
But is UNION a constant or is the context wrong?
$query = $query1." ".UNION." ".$query2; $result = mysqli_query($dbcon, $query) or die('Error getting data'); $num_rows = mysqli_num_rows($result); Similar TutorialsThe following union query is supposed to return the total number of alerts and the total number of messages, however a dump of the returned array shows only the alerts(num_alerts). $sql = "SELECT COUNT(a.a_aid) as num_alerts FROM ".Asf_Db::$prefix."alerts a UNION SELECT COUNT(m.m_mid) as num_messages FROM ".Asf_Db::$prefix."messages m"; $sth = Asf_Core::$db->prepare($sql); $sth->execute() or die(Asf_Core::$db->error($sth, $sql)); $results = $sth->fetch(PDO::FETCH_ASSOC); dump($results); anyone see where im going wrong? Here's what i've been attempting to do . a. I have a table "travelexpense" in 2 DBs. All columns are identical. b. Based on a set of parameters, a user can have a record in this table in one or both of the databases. c. When i want to output all the entries pertaining to that user in both these tables, I use Select Union. So far so good. d. However, the query references customers pertaining to those expenses . The customer table is present in both databases but is populated with different records. e. Now when the SELECT UNION runs, it picks up only the customer in the first database and not the second, even if there is an entry pertaining only to the second database. f. Odlly though the other data - that is the expenses themselves are selected correctly. Its only the customers entries that are wrong Code snippet below Code: [Select] $sqlZ= " SELECT * FROM $dbname2.`Travelexpense` WHERE Userid='$user' && MONTH(`Enddate`)=$month && YEAR(`Enddate`)=$year UNION select * from $dbname3.`Travelexpense` WHERE Userid='$user' && MONTH(`Enddate`)=$month && YEAR(`Enddate`)=$year "; $resultZ = mysql_query($sqlZ) or die (mysql_error()); $numofrows = mysql_num_rows($resultZ); for($i = 0; $i < $numofrows; $i++) { $myrowZ = mysql_fetch_array($resultZ); $coid = $myrowZ["CID"]; $s1 = "SELECT * FROM $dbname2.`Customers` WHERE `CID` = $coid UNION select * from $dbname3.`Customers` WHERE `CID` = $coid"; $result1 = mysql_query($s1); $myrow2 = mysql_fetch_array($result1); $Name=$myrow2["Company"]; //here's the problem . If say $coid = 150 , then it will look only at record 150 in Table Customers in dbname2 and NOT in Table Customers in dbname3 even if only dbname3 has a relevant entry. blah blah } Am i using UNION incorrectly esp in the query $s1? Appreciate any help . Thanks ! Swati So say I have a union sql statement like this: (SELECT id, card_id, ordered FROM Match_1 WHERE card_id='$card_id') UNION (SELECT id, card_id, ordered FROM Match_2 WHERE card_id='$card_id') UNION (SELECT id, card_id, ordered FROM Match_3 WHERE card_id='$card_id') ORDER BY ordered ASC ordered is an INT, but yet when I order the results its like this 1 11 2 11 should obviously be last but it isn't, what am I doing wrong? $sql=mysql_query("SELECT * FROM `buds` WHERE `level`<='$user_level' UNION SELECT * FROM `buds`, `unlocked_buds` WHERE buds.`id` = unlocked_buds.`bud_id` ORDER BY buds.`id` ASC") or die("A MySQL error has occurred.<br />Your Query: " . $sql . "<br /> Error: (" . mysql_errno() . ") " . mysql_error()); I have been trying to learn about UNION select statements. I ran the query above and got this response: Quote Error: (1222) The used SELECT statements have a different number of columns I think I know what the problem is, but not sure how to fix it. There is two columns, one is "buds" which holds the flowers seeds info. The seconds is "unlocked_buds" which just links the "id" from buds to "user_id" to the user table. Both buds and unlocked_buds are both 8 columns. What do I need to learn? Hi I'm trying to find the max value in 3 columns, new_date, reply_date and finalized_date in my table: Code: [Select] $test = safe_query("SELECT MAX(new_date) FROM (SELECT new_date AS new_date FROM ".PREFIX."cup_challenges UNION SELECT reply_date FROM ".PREFIX."cup_challenges UNION SELECT finalized_date FROM ".PREFIX."cup_challenges) AS maxval"); Did some research and have no idea what is wrong/if the above is correct? The page breaks below this code. This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=308424.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=321649.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=306274.0 i have this query and i'm trying to order all of the results by `created` ascending. I've tried putting the later query before the first but the single row from the first query(the way it is right now) gets stuck as the last row in the results. How can i order the results of both queries? Code: [Select] ( SELECT * FROM Account_activity WHERE DATE( created ) < '2011-08-01' AND username = '40' ORDER BY created DESC LIMIT 1 ) UNION ( SELECT * FROM Account_activity WHERE MONTH( created ) = '08' AND YEAR( created ) = '2011' AND username = '40' ORDER BY created ASC ) Is there a cleaner way to do this? $query = "SELECT branches.Language FROM eua_users, branches WHERE eua_users.AssignedBranch = branches.country" ; include("dbconnectlocal.php") ; $result = mysql_query($query) ; $row = mysql_fetch_object($result) ; $usrlang = $row->Language ; $query = "SELECT UserName, Email FROM eua_users WHERE UserName = '$user'" ; include("dbconnectlocal.php") ; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_object($result) ; $branchmail = $row->Email ; $query = "SELECT $usrlang FROM autoreplies WHERE ReplyID = '0'" ; include("dbconnectlocal.php") ; $result = mysql_query($query) or die(mysql_error()) ; $row = mysql_fetch_object($result) ; $message = $row->$usrlang ; $query = "SELECT $usrlang FROM autoreplies WHERE ReplyID = '1'" ; include("dbconnectlocal.php") ; $result = mysql_query($query) or die(mysql_error()) ; $row = mysql_fetch_object($result) ; $url = $row->$usrlang ; $query = "SELECT $usrlang FROM autoreplies WHERE ReplyID = '2'" ; include("dbconnectlocal.php") ; $result = mysql_query($query) or die(mysql_error()) ; $row = mysql_fetch_object($result) ; $subject = $row->$usrlang ; This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=332593.0 Hi, I'm trying to make my game navigation that involves switching div images to navigate in the game world. It is browser based. The first script, the user clicks on the world map to go to "Teardrop Ocean". The second script lets the user press the "further" button or "back" button to navigate within "Teardrop Ocean". My problem is I'm trying to combine the image map navigation with the "further" and "back" navigation. The problem is, I don't know if it's possible since the first world map script involves javascript. Here is the code, any help greatly appreciated Thanks. Derek This is the code that uses javascript to process a hidden form to use php to output. This is our "world map" link, that should load the "teardrop ocean" image inside the div, which it doesn't do yet here. Code: [Select] <?php $zone=''; if(isset($_POST['checker']) && $_POST['checker'] == 'checked') { echo "it worked !"; $zone="<img src='teardrop.jpg'/>"; } if(isset($_POST['back'])) { $zone=''; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <form action="aradia.php" method="post" name="mapform" id="mapform"> <input type="hidden" name="checker" value="checked" /> <div align="left"> <img src="aradia.jpg" width="256" height="328" border="0" usemap="#Map" /></div> </form> <map name="Map" id="Map"> <area shape="rect" coords="5,176,81,249" href="javascript: void(0);" onclick="javascript: document.getElementById('mapform').submit();" /> </map> <div align="center"><?php echo $zone;?></div> <form action="aradia.php" method="post" > <input type = "submit" name="back" value="back" /></form> </body> </html> and here is the "Teardrop ocean" navigation, which uses php to navigate forward and backwards in the zone, but can't go back to the world map, or there is no way to yet. Code: [Select] /////////////////////////////GAME NAVIGATION AND MONSTER SEARCH CODE NOT FINISHED////////////////////////////////// if(( !isset($_SESSION['current_background']) && !isset($_SESSION['currentMonster'])) OR (! $_POST)) { $_SESSION['current_monster'] = 0; $_SESSION['current_background'] = 0; } if (!isset($_SESSION['background']) && !isset($_SESSION['monster'])) { $_SESSION['background'] = array ( "<img src='sundragon_environments/ocean/ocean1_FRAME.jpg'/>", "<img src='sundragon_environments/ocean/ocean1_FRAME2.jpg'/>", "<img src='sundragon_environments/ocean/ocean1_FRAME3.jpg'/>", "<img src='sundragon_environments/ocean/ocean1_FRAME4.jpg'/>", "<img src='sundragon_environments/ocean/ocean1_FRAME5.jpg'/>" ); $_SESSION['monster'] = array ( "<img src='sundragon_monsters_source/water/goldfish/goldfish.png'/>", "<img src='sundragon_monsters_source/water/eel/eel_transp_FRAME.png '/>", "<img src='sundragon_monsters_source/water/shark/shark_transp_FRAME.png'/>", "<img src='sundragon_monsters_source/water/octalisk/octalisk_transp_FRAME.png'/>", "<img src='sundragon_monsters_source/water/teardrop_ocean_protector/teardrop_ocean_protector.png'/>" ); } if(!isset($_SESSION['current_background']) && !isset($_SESSION['current_monster'])) { $_SESSION['current_monster']=0; $_SESSION['current_background'] = 0; } if(isset($_POST['further'])) { $_SESSION['current_monster'] = isset($_SESSION['monster'][$_SESSION['current_monster'] + 1]) ? ($_SESSION['current_monster'] + 1) : 0; $_SESSION['current_background'] = isset($_SESSION['background'][$_SESSION['current_background'] + 1]) ? ($_SESSION['current_background'] + 1) : 0; } elseif(isset($_POST['back'])) { $_SESSION['current_monster'] = isset($_SESSION['monster'][$_SESSION['current_monster'] - 1]) ? ($_SESSION['current_monster'] - 1) : count($_SESSION['monster'])-1; $_SESSION['current_background'] = isset($_SESSION['background'][$_SESSION['current_background'] - 1]) ? ($_SESSION['current_background'] - 1) : count($_SESSION['background'])-1; } $currentBackground=$_SESSION['background'][$_SESSION['current_background']]; $currentMonster=$_SESSION['monster'][$_SESSION['current_monster']]; and I echo out $currentBackground and $currentMonster in the main game div. Whats the array function to merge two or more similar patterns in a single array. $a = array('red','pink','red','green','blue'); Basically i need to combine "red" so the output will be red, pink, green, blue I have loaded two similar xml files by simplexml_load_file, and I want to merge them. They have similar structure and I want to put them into a single xml. As a matter of fact, I have parse two similar xml files with foreach, and now I want to put them together and use one foreach for the merged xml. I hope to find a function like array_merge() i have this list of arrays Code: [Select] Array ( [0] => LeanBiz ) Array ( [0] => Boutique ) Array ( [0] => Feather ) Array ( [0] => Aggregate )i want to create one single array containing the values from x arrays. How can I merge the array, but I keep in mind that I only need to merge userlocation and the usercity arrays. Current output... Code: [Select] Array ( [0] => Array ( [0] => userlocation [1] => 19 ) [1] => Array ( [0] => credentials [1] => ) [2] => Array ( [0] => specialties [1] => ) [3] => Array ( [0] => usercity [1] => place1 ) ) Desired output... Code: [Select] Array ( [0] => Array ( [0] => userlocation [1] => 19 [2] => usercity [3] => place1 ) [1] => Array ( [0] => credentials [1] => ) [2] => Array ( [0] => specialties [1] => ) ) How can this be done? The array is dynamic. Hello, I've created a PHP file to merge XML files by ReferenceID(product_print_id) but i don't get the result that i want. I think i must use cloneNode and DOMNode::insertBefore but I think i'm lost. The first file is prodInfo.xml: <?xml version="1.0" encoding="utf-8"?> <PRODUCTINFORMATION> <PRODUCTS> <PRODUCT> <PRODUCT_NUMBER>53-03</PRODUCT_NUMBER> <PRODUCT_PRINT_ID>42</PRODUCT_PRINT_ID> <PRODUCT_NAME>ProductFirst</PRODUCT_NAME> <COLOR_CODE>03</COLOR_CODE> </PRODUCT> </PRODUCTS> </PRODUCTINFORMATION> and the second file is printInfo.xml: <?xml version="1.0" encoding="utf-8"?> <PRINTINGINFORMATION> <PRODUCTS> <PRODUCT> <PRODUCT_PRINT_ID>42</PRODUCT_PRINT_ID> <PRINTING_POSITIONS> <PRINTING_POSITION> <ID>TOP BOX</ID> <PRINTING_TECHNIQUE> <ID>DL</ID> </PRINTING_TECHNIQUE> <PRINTING_TECHNIQUE> <ID>L2</ID> </PRINTING_TECHNIQUE> <PRINTING_TECHNIQUE> <ID>P4</ID> </PRINTING_TECHNIQUE> </PRINTING_POSITION> </PRINTING_POSITIONS> </PRODUCT> </PRODUCTS> </PRINTINGINFORMATION> The php file i created is the following: <?php header ("Content-Type:text/xml"); $target = new DOMDocument(); $target->preserveWhiteSpace = FALSE; $target->load('prodInfo.xml'); $targetXpath = new DOMXpath($target); $source = new DOMDocument(); $source->load('printInfo.xml'); $sourceXpath = new DOMXpath($source); foreach ($targetXpath->evaluate('//PRODUCT') as $PRODUCTNode) { $PRODUCT_PRINT_ID = $targetXpath->evaluate('string(PRODUCT_PRINT_ID)', $PRODUCTNode); foreach ($sourceXpath->evaluate('//PRODUCT[PRODUCT_PRINT_ID="'.$PRODUCT_PRINT_ID.'"]/*[not(self::PRODUCT_PRINT_ID)]') as $node) { $PRODUCTNode->appendChild( $target->importNode($node, TRUE) ); } } $target->formatOutput = TRUE; echo $target->saveXml(); ?> The output/result i get is this: <?xml version="1.0" encoding="utf-8"?> <PRODUCTINFORMATION> <PRODUCTS> <PRODUCT> <PRODUCT_NUMBER>53-03</PRODUCT_NUMBER> <PRODUCT_PRINT_ID>42</PRODUCT_PRINT_ID> <PRODUCT_NAME>ProductFirst</PRODUCT_NAME> <COLOR_CODE>03</COLOR_CODE> <PRINTING_POSITIONS> <PRINTING_POSITION> <ID>TOP BOX</ID> <PRINTING_TECHNIQUE> <ID>DL</ID> </PRINTING_TECHNIQUE> <PRINTING_TECHNIQUE> <ID>L2</ID> </PRINTING_TECHNIQUE> <PRINTING_TECHNIQUE> <ID>P4</ID> </PRINTING_TECHNIQUE> </PRINTING_POSITION> </PRINTING_POSITIONS> </PRODUCT> </PRODUCTS> </PRODUCTINFORMATION> But what i want to achieve is this: <?xml version="1.0" encoding="utf-8"?> <PRODUCTINFORMATION> <PRODUCTS> <PRODUCT> <PRODUCT_NUMBER>53-03</PRODUCT_NUMBER> <PRODUCT_PRINT_ID>42</PRODUCT_PRINT_ID> <PRODUCT_NAME>ProductFirst</PRODUCT_NAME> <PRINTING_POSITIONS> <PRINTING_POSITION> <ID>TOP BOX</ID> <PRINTING_TECHNIQUE> <ID>DL</ID> </PRINTING_TECHNIQUE> <COLOR_CODE>03</COLOR_CODE> </PRINTING_POSITION> <PRINTING_POSITION> <ID>TOP BOX</ID> <PRINTING_TECHNIQUE> <ID>L2</ID> </PRINTING_TECHNIQUE> <COLOR_CODE>03</COLOR_CODE> </PRINTING_POSITION> <PRINTING_POSITION> <ID>TOP BOX</ID> <PRINTING_TECHNIQUE> <ID>P4</ID> </PRINTING_TECHNIQUE> <COLOR_CODE>03</COLOR_CODE> </PRINTING_POSITION> </PRINTING_POSITIONS> </PRODUCT> </PRODUCTS> </PRODUCTINFORMATION> So as you can see i want to repeat the field PRINTING POSITION ID for every PRINTING TECHNIQUE and also i want to repeat the field COLOR CODE for every PRINTING POSITION. Anyone can help me with this? Thanks in advance. i want to preserve the numeric keys. When i print the array this is the result Code: [Select] Array ( [0] => one [1] => two [2] => three ) it should be Code: [Select] Array ( [0] => two [1] => one [2] => three ) Code: [Select] $test1[2] = "one"; $test2[1] = "two"; $test2[3] = "three"; $test = array_merge($test1,$test2); print_r($test); How do i solve this? Hi, Have a question regarding array handling / changes. I have the below posted array. I need to consolidation the "categoryId" & "categoryName" where the "id" have the same value e.g. 66, then remove the duplicates but keep the merged data. The new merged data could be stored under a key e.g. [categories] => Array ( [categoryId] = 28 [categoryName] = Game Drive [1] [categoryId] = 29 [categoryName] = Game Drive etc. Code: [Select] Array ( [0] => Array ( [id] => 66 [name] => Person Name [description] => Test Desc [webUrl] => www.nish.co.za [emailAddress] => abc@abc.co.za [telNumber] => 123123 [provinceId] => 3 [cityId] => 2 [creationDate] => 2010-09-27 17:42:55 [activated] => Y [categoryId] => 28 [provinceName] => Gauteng [cityName] => Johannesburg [categoryName] => Game Drives ) [1] => Array ( [id] => 66 [name] => Person Name [description] => Test Desc [webUrl] => www.nish.co.za [emailAddress] => abc@abc.co.za [telNumber] => 123123 [provinceId] => 3 [cityId] => 2 [creationDate] => 2010-09-27 17:42:55 [activated] => Y [categoryId] => 29 [provinceName] => Gauteng [cityName] => Johannesburg [categoryName] => Nature Reserve ) [2] => Array ( [id] => 66 [name] => Person Name [description] => Test Desc [webUrl] => www.nish.co.za [emailAddress] => abc@abc.co.za [telNumber] => 123123 [provinceId] => 3 [cityId] => 2 [creationDate] => 2010-09-27 17:42:55 [activated] => Y [categoryId] => 34 [provinceName] => Gauteng [cityName] => Johannesburg [categoryName] => Rehabilitation Centre ) [3] => Array ( [id] => 65 [name] => Person Name [description] => This is the true test [webUrl] => http://www.klil.com [emailAddress] => abc@abc.co.za [telNumber] => 12311231 [provinceId] => 3 [cityId] => 2 [creationDate] => 2010-09-26 19:33:14 [activated] => Y [categoryId] => 28 [provinceName] => Gauteng [cityName] => Johannesburg [categoryName] => Game Drives ) [4] => Array ( [id] => 65 [name] => Test Multiple Categ [description] => This is the true test [webUrl] => http://www.klil.com [emailAddress] => abc@abc.co.za [telNumber] => 12311231 [provinceId] => 3 [cityId] => 2 [creationDate] => 2010-09-26 19:33:14 [activated] => Y [categoryId] => 29 [provinceName] => Gauteng [cityName] => Johannesburg [categoryName] => Nature Reserve ) [5] => Array ( [id] => 65 [name] => Person Name [description] => Description Text [webUrl] => http://www.test.com [emailAddress] => abc@abc.co.za [telNumber] => 12311231 [provinceId] => 3 [cityId] => 2 [creationDate] => 2010-09-26 19:33:14 [activated] => Y [categoryId] => 34 [provinceName] => Gauteng [cityName] => Johannesburg [categoryName] => Rehabilitation Centre ) [6] => Array ( [id] => 64 [name] => Person Name [description] => Description Text [webUrl] => www.tree.com [emailAddress] => abc@abc.co.za [telNumber] => 1123123123 [provinceId] => 3 [cityId] => 1 [creationDate] => 2010-09-26 12:10:56 [activated] => Y [categoryId] => 28 [provinceName] => Gauteng [cityName] => Alexandra [categoryName] => Nature Walks ) [7] => Array ( [id] => 64 [name] => Person Name [description] => Description Text [webUrl] => www.tree.com [emailAddress] => abc@abc.co.za [telNumber] => 1123123123 [provinceId] => 3 [cityId] => 1 [creationDate] => 2010-09-26 12:10:56 [activated] => Y [categoryId] => 29 [provinceName] => Gauteng [cityName] => Alexandra [categoryName] => Nature Reserve ) [8] => Array ( [id] => 64 [name] => Person Name [description] => Description Text [webUrl] => www.tree.com [emailAddress] => abc@abc.co.za [telNumber] => 1123123123 [provinceId] => 3 [cityId] => 1 [creationDate] => 2010-09-26 12:10:56 [activated] => Y [categoryId] => 34 [provinceName] => Gauteng [cityName] => Alexandra [categoryName] => Rehabilitation Centre ) [9] => Array ( [id] => 63 [name] => Person Name [description] => Description Text [webUrl] => www.test.com [emailAddress] => abc@abc.co.za [telNumber] => 123123 [provinceId] => 5 [cityId] => 3 [creationDate] => 2010-09-26 09:58:41 [activated] => Y [categoryId] => 29 [provinceName] => Limpopo [cityName] => Bandelierkop [categoryName] => Nature Reserve ) ) Thanks in advance, Peter |