我已经编写了这段 PHP 代码,但它给出了一些奇怪的结果。片段是
$queryIsbnArr = mysql_query("select ISBN from quotation_master") or die(mysql_error());
$row = mysql_fetch_array($queryIsbnArr) or die(mysql_error());
print_r($row);
$_SESSION['isbnArr'] = $row;
查询给出的输出是..
Array ( [0] => 12121 [ISBN] => 12121 )
当在数据库上运行时查询的输出是
12121
56
哪个是正确的,那么这有什么问题呢?
最佳答案
阅读the manual !
Returns an array of strings that corresponds to the fetched row, or FALSE if there are no more rows. The type of returned array depends on how result_type is defined. By using MYSQL_BOTH (default), you'll get an array with both associative and number indices.
mysql_fetch_array()
获取每行数据的数值数组和关联数组。您需要使用 或 另一个来获取实际数据,因为 $row
包含作为数组的整个数据集,即使它只有一个值也是如此。
$queryIsbnArr = mysql_query("select ISBN from quotation_master") or die(mysql_error());
$row = mysql_fetch_assoc($queryIsbnArr) or die(mysql_error());
print_r($row['ISBN']);
$_SESSION['isbnArr'] = $row;
仅供引用,you shouldn't use mysql_*
functions in new code .它们不再维护 and are officially deprecated .查看red box ?了解 prepared statements相反,并使用 PDO或 MySQLi - this article将帮助您决定选择哪个。如果选择 PDO,here is a good tutorial .
关于从数据库检索时,PHP 显示奇怪的条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30694330/