从数据库检索时,PHP 显示奇怪的条目

标签 php mysql arrays

我已经编写了这段 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相反,并使用 PDOMySQLi - this article将帮助您决定选择哪个。如果选择 PDO,here is a good tutorial .

关于从数据库检索时,PHP 显示奇怪的条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30694330/

相关文章:

arrays - Bash比较数组值以找到最大值

php - 将 DropDown 值保存到 Php MySQL

php - 如何将 php 与 bash 脚本连接或将变量从 php 传递到 bash 脚本

php - 我应该在所有相关表中包含自动增量 id 吗?

mysql - 如何在查询中重用派生值

python - Numpy - 切片图像时的最小内存使用量?

php - 在 Linux 上的 Zend 配置 ini 文件中包含花括号内的变量

c# - 位于 linux 服务器上的 PHP 代码需要运行 windows .exe

mysql - Elasticsearch : Find All Records By Single Phrase

PHP:如何用键和值填充数组,以便 1 = 1、2 = 2、3 = 3 等