为什么这段代码不输出结果?

时间:2011-08-19 11:52:54

标签: php mysql

我收到任何错误或警告。我的表称为产品,该字段称为名称。我想找到第一和第二列之间的意见。

为什么我在这里失踪?

<?php

mysql_connect("localhost"," "," ");
mysql_select_db(" ");
mysql_query('set names utf8');

$query = mysql_query("SELECT t1.name AS first FROM products t1");
$row = mysql_fetch_assoc($query);
if (!$query) {
    die('Invalid query: ' . mysql_error());
}

do {
    $first = $row['first'];
    $query2 = mysql_query("SELECT t1.name AS first,
                                 t2.name AS second,
                                 (MATCH (t2.name) AGAINST ('$first')) AS relevance
FROM products t1, products t2
                          WHERE t1.name = '$first'
                          ORDER BY relevance DESC");
if (!$query2) {
    die('Invalid query: ' . mysql_error());
}
    $most_similar = mysql_fetch_assoc($query2);
} while ($row = mysql_fetch_assoc($query));
?>

2 个答案:

答案 0 :(得分:1)

您的查询中没有t2,但在您的选择中,您有t2.name as second

实际上,你根本没有FROM条款......

修改

这是对你想要实现的内容的随机猜测......我真的不知道,但你应该能够根据需要编辑查询。

$query2 = mysql_query("SELECT '$first' AS first,
                             t.name AS second,
                             (MATCH (t.name) AGAINST ('$first')) AS relevance
                      FROM products t
                      ORDER BY relevance DESC");

答案 1 :(得分:1)

也许您应该检查$ query2是否为FALSE,否则显示mysql_error()错误。另请参阅http://www.php.net/manual/en/function.mysql-query.php上的示例。