从数据库自定义表获取价值在Wordpress中不起作用

时间:2019-01-28 15:15:04

标签: database wordpress get

我用wp_compare_post创建了一个wordpress表,其中带有以下列ID,user_id,post_id,post_type。我已附上数据库的图像。 enter image description here

以下是用于查看数据库中数据的代码

Fatal error: Uncaught Doctrine\ORM\Query\QueryException: SELECT r,m,s FROM Requirement r JOIN r.Manufacturer m JOIN r.Section s WHERE r.id=1 in ___/vendor/doctrine/orm/lib/Doctrine/ORM/Query/QueryException.php:43 Stack trace: #0 ___/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php(491): Doctrine\ORM\Query\QueryException::dqlError('SELECT r,m,s FR...') #1 ___/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php(1060): Doctrine\ORM\Query\Parser->semanticalError('line 0, col 52 ...') #2 ___/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php(1766): Doctrine\ORM\Query\Parser->JoinAssociationPathExpression() #3 ___/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php(1700): Doctrine\ORM\Query\Parser->JoinAssociationDeclaration() #4 ___/vendor/doctrine/orm/lib in ___/vendor/doctrine/orm/lib/Doctrine/ORM/Query/QueryException.php on line 65

但是出现以下错误。

function compare_counting(){
    global $wpdb;
    //get user id
    $user_ID = get_current_user_id();

    $sql = "SELECT * FROM $wpdb->compare_post WHERE user_id = '$user_ID '";
    $results = $wpdb->get_results($sql) or die(mysql_error());
    foreach( $results as $result ) {
            echo $result->post_id.',';
        }
    }

1 个答案:

答案 0 :(得分:1)

我认为可能是因为您需要使用$ wpdb->前缀

尝试一下:

$sql = "SELECT * FROM ".$wpdb->prefix."compare_post WHERE user_id = '$user_ID'";