iSun
iSun

Reputation: 1754

UNION statement in query

I wrote a query but I got following error, Any ideas ?!

Error :

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in E:\AppServ\www\admin\index.php on line 545

Code :

                    require_once '../_db/databaseConnect.php';
                    $db = new databaseConnect();
                    $db->connect();
                    $queryResult = mysql_query("(SELECT * FROM tabelcomments WHERE publishStatus = 2) UNION (SELECT appTitleFa FROM tableapps WHERE appID = '$appID');");
                    for ($dataCnt = 0; $dataCnt < mysql_num_rows($queryResult); $dataCnt++) 
                      {
                         //codes...
                      }
                    $db->close();

Upvotes: 1

Views: 96

Answers (1)

Lieven Keersmaekers
Lieven Keersmaekers

Reputation: 58431

Both parts of a UNION should return the same amount and same type of columns.

Most likely, your tabelComments table contains more columns than the single column returned from the tableApps table in the second part of your union.

You can fix this by explicitly selecting the appropriate column(s) from tableComments or add dummy columns to the UNIONpart.

Example

SELECT NULL as dummycol1, comment FROM tablecomment
UNION SELECT appTitleFa, NULL as dummycol2 FROM tableapps

Upvotes: 3

Related Questions