WordPress.org

Support

Support » How-To and Troubleshooting » Using MySQL with INNER JOIN in WP

Using MySQL with INNER JOIN in WP

  • phillbooth
    Member

    @phillbooth

    Hi, I am trying to do a query to a custom table using “Inner Join” but some some reason I cant seem to do it through the wordpress db connection, any idea why?

    Code that FAILS…

    global $wpdb;
    
    		if( !function_exists('get_option') )
    		{
    			require_once('wp-config.php');
    		}
    
    $result = $wpdb->get_results("SELECT t1.*, t2.coId, t2.coName FROM mytable1 AS t1 INNER JOIN mytable2 AS t2 ON t1.qsCoId = t2.coId WHERE t1.authorised='0' ORDER BY t1.qsDateAdded DESC");
    
    while($row=mysql_fetch_array($result))
    {
    $row["qsCoId"];
    }

    but this code DOES work…

    mysql_connect ("localhost", "mydb", "mypasswordn");
    @mysql_select_db("web102-csnbench") or die( "Unable to select database");
    
    $result = mysql_query("SELECT t1.*, t2.coId, t2.coName FROM mytable1 AS t1 INNER JOIN mytable12 AS t2 ON t1.qsCoId = t2.coId WHERE t1.authorised='0' ORDER BY t1.qsDateAdded DESC");
    
    while($row=mysql_fetch_array($result))
    {
    $row["qsCoId"];
    }
  • The topic ‘Using MySQL with INNER JOIN in WP’ is closed to new replies.