简体   繁体   English

Wordpress中的SQL查询问题

[英]trouble with sql query in wordpress

I'm using the exec php plugin to have php code inside my pages. 我正在使用exec php插件在页面中添加php代码。 I've imported a table into the wordpress db and copied my original query over into a page but I can't seem to get it working. 我已经将表格导入wordpress db中,并将原始查询复制到了页面中,但似乎无法正常工作。

I can't tell what's wrong because there is no sql error thrown onto the page, all it does is stop executing once it reached the query. 我不知道出什么问题了,因为没有sql错误抛出到页面上,它要做的就是一旦到达查询就停止执行。

$query = "SELECT * FROM 'classSchedule' ORDER BY 'age' ASC";
$result = mysql_query ($query, $link) or die (mysql_error());

$num_rows = mysql_num_rows ($result);
if ($num_rows != "0")
{
    echo "Select a Class";
    while ($row = mysql_fetch_array($result))
    {
        $day = $row['day'];
        $time = $row['time'];
        $class = $row['class'];
        $age = $row['age'];
        $level = $row['level'];

        echo "(Age: $age) $day $time $class Level: $level";
    }
}

I've also tried testing with example code in the wpdb codex but even that isn't working. 我也尝试过在wpdb Codex中使用示例代码进行测试,但即使这样也不起作用。

$mylink = $wpdb->get_row("SELECT * FROM $wpdb->wp_links WHERE link_id = 2");
echo $mylink->link_id;

Someone please help! 有人请帮忙! I'm using wordpress 3.1. 我正在使用wordpress 3.1。

EDIT Okay I've solved it by adding global $wpdb to the top of the code. 编辑好的,我已经通过在代码顶部添加全局$ wpdb解决了它。

Try removing the ticks around classSchedule and age . 尝试消除classScheduleage周围的滴答声。 Those look like normal apostrophes ('). 那些看起来像普通的撇号(')。 If you want to encase table and column names in MySQL (which is optional), use backticks (`). 如果要在MySQL中包含表名和列名(这是可选的),请使用反引号(`)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM