我真的很喜欢一些帮助,下面的MySQL/PHP的问题(也许错误?)首先返回值是为“空”时,它有一个值
我试图检索和使用MySQL/PHP从我的数据库显示数据数组,但是当我回显数组时,它将第一个值返回为'null'。
所以,即使数据库具有以下信息:
"Example 1", "Example 2", "Example 3"...
PHP的回声了:
"null", "Example 2, "Example 3"
我可以想象这将是一个共同的问题,但我没有管理在互联网上的其他地方找到所需的信息,所以我希望你们友善的帮助。
我的PHP
/* If connection to database, run sql statement. */
if ($conn) {
$fetch = mysql_query("SELECT column FROM table WHERE approved = '1' ");
// declare empty array to fill later
$result = array();
// make sure the MySQL pointer is looking at the first row
mysql_data_seek($fetch, 0);
while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {
foreach ($row as $value) {
$row_array = $value;
// push info into new array with just the value
array_push($result, $row_array);
}
}
}
/* Free connection resources. */
mysql_close($conn);
/* Toss back results as json encoded array. */
echo json_encode($result);
UPDATE
的Mark B新代码礼貌:
if ($conn)
{
$sql = "SELECT column_name FROM table WHERE comment_approved = '1' ";
$query = mysql_query($sql) or die(mysql_error());
$result = array();
while ($row = mysql_fetch_assoc($query)) {
$result[] = $row['column_name'];
}
}
/* Free connection resources. */
mysql_close($conn);
/* Toss back results as json encoded array. */
echo json_encode($result);
注:
作为显得无能为力mysql_data_seek($fetch, 0);
:
的“空”问题仍然存在有或无。
任何帮助将是伟大的!
解决
感谢Mark B谁指出,这个问题很可能在数据库中,而不是PHP,原来有字符'潜伏那里应该是一个”。这导致信息显示为“空”。
你为什么使用seek?它在开始时已经是0了?是的,使用'或死(mysql_error());' – 2011-05-16 19:47:20
我不认为你需要mysql_data_seek()函数,因为mysql_query()应该返回结果集的指针已经在0。如果在迭代完成后重复使用相同的数据集(例如在while循环中),则应该只需要这样做。 – 2011-05-16 19:47:26