选择没有结果

时间:2011-06-16 13:44:48

标签: php mysql

我已经试了几个小时。我可以连接到我的数据库并插入其中,但似乎无法从中获取任何内容。

// Declarations
    $connection = mysql_connect("xxxxxxx", "xxxxxxx", "xxxxxxxx");
    mysql_select_db("kettle_test1", $connection);
    if (!$connection)
        {
        die ('Cold not connect: ' . mysql_error());
        }
    else
    {
    echo "connected <br> <br>";
    }



// INSERT   

    mysql_query("INSERT INTO bob2 (Danumber)
    VALUES ('32234245')");

// QUERY AND DISPLAY

    $result = mysql_query("SELECT * FROM bob2");

    echo $result;

// if this is commented out I get the 'connected' message above. without it the page is blank,  
    while ($row = mysql_fetch_assoc($result)) {
    echo $row["Danumber"];
   
}
    
*/


    echo "<br> <br> <br>";

2 个答案:

答案 0 :(得分:2)

这里有很多可能性。例如,您可以尝试以下方法:

  • 使用mysql_num_rows检查已返回的行数
  • 使用print_r打印整行。
  • 使用isset检查“Danumber”键是否确实存在

总结:使用更多检查来确保您检索的内容确实存在。

答案 1 :(得分:0)

我首先测试插入是否真的没问题:

// INSERT   

    $result = mysql_query("INSERT INTO bob2 (Danumber)
    VALUES ('32234245')");
    if (!$result) {
        die('Invalid insert query: ' . mysql_error());
    }

否则您以后无法选择。但这可能不是你问题的原因。

但您也可以对SELECT查询执行相同的操作:

// QUERY AND DISPLAY
    $result = mysql_query("SELECT * FROM bob2");
    if (!$result) {
        die('Invalid select query: ' . mysql_error());
    }

这样做可以验证查询是否经历过无错误。

然后你应该确保你可以获取和显示数据:

// if this is commented out I get the 'connected' message above. without it the page is blank,
while ($row = @mysql_fetch_assoc($result)) {
    print_r(array_keys($row));
    print_r($row["Danumber"]);
}
相关问题