我已经试了几个小时。我可以连接到我的数据库并插入其中,但似乎无法从中获取任何内容。
// 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>";
答案 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"]);
}