doushi3202 2014-02-13 07:14
浏览 138
已采纳

wpdb在'where子句'错误中的SQL未知列'username'

I've been trying to wrap my head around why my query doesn't return any results from my table. My table name is wp_user_orders. The column user_name is in that table. Both prepare and query return empty arrays. The get result with the query inside returns the database error.

PHP:

<?php 
    $dirName1 = 'C:/wamp/www/c2c/wp-content/themes/flawless-v1-01';
    $dirName2 = 'C:/wamp/www/c2c';
    require_once($dirName1.'/config/setup.php');
    require_once($dirName2.'/wp-config.php');
    require_once($dirName2.'/wp-load.php');
    require_once($dirName1.'/include/function/encryption-class.php');
    require_once($dirName1.'/include/function/price-calculations.php');

    define('WP_DEBUG', true);
    $wpdb->show_errors();
    $tableName = $wpdb->prefix . "user_orders";
    $user = wp_get_current_user();
    $userId = $user->ID;
    $userName = $user->user_login;
    echo $tableName . ':  ' . $userId . ': '. $userName; 
    echo var_dump($tableName); 
    echo var_dump($userId); 
    echo var_dump($userName); 
    $myQuery = $wpdb->prepare("SELECT * FROM $tableName WHERE user_name = %s", $tableName, $userName);
    $results = $wpdb->get_results($wpdb->prepare("SELECT * FROM %s WHERE user_name = %s", $tableName, $userName), ARRAY_A);
    $results2 = $wpdb->get_results($wpdb->query("SELECT * FROM $tableName WHERE user_name = $userName"));
    print_r($results2);
    echo var_dump($results2);
    print_r($results);
    echo var_dump($results);

    $wpdb->print_error;


?>

My php displays:

 wp_user_orders: 2: kronus

string 'wp_user_orders' (length=14)

int 2

string 'kronus' (length=6)

WordPress database error: [Unknown column 'kronus' in 'where clause']
SELECT * FROM wp_user_orders WHERE user_name = kronus

null

Array ( )

array (size=0)
  empty

I have a feeling I'm just missing something obvious, but I'm stumped.

UPDATED:

Updated my incorrect prepared statement.

Previous:

$wpdb->prepare("SELECT * FROM $tableName WHERE `user_name` = %s", $tableName, $userName)

New:

$wpdb->prepare("SELECT * FROM %s WHERE `user_name` = %s", $tableName, $userName)

New Error from change:

WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''wp_user_orders' WHERE `user_name` = 'kronus'' at line 1]
SELECT * FROM 'wp_user_orders' WHERE `user_name` = 'kronus'
  • 写回答

1条回答 默认 最新

  • dongzhila3786 2014-02-13 07:16
    关注

    You are not either quoting your username or preparing your query with a variable, I would suggest doing it this way

    $results = $wpdb->get_results($wpdb->prepare("SELECT * FROM $tableName WHERE user_name = %s",$username));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line