简体   繁体   English

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

[英]PHP-showing weird entries when retrieved from database

I have written this PHP code but it is giving some weird result. 我已经编写了此PHP代码,但给出了一些奇怪的结果。 The snippet is 片段是

$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;

The output which the query is giving is.. 查询给出的输出是..

Array ( [0] => 12121 [ISBN] => 12121 ) 

Where as the output which is query is giving when run on the database is 在数据库上运行时查询所给出的输出在哪里

12121
56

Which is correct.What is the problem with this then? 哪个是正确的,那这是什么问题?

Read the manual ! 阅读手册

Returns an array of strings that corresponds to the fetched row, or FALSE if there are no more rows. 返回与获取的行相对应的字符串数组;如果没有更多行,则返回FALSE。 The type of returned array depends on how result_type is defined. 返回数组的类型取决于result_type的定义方式。 By using MYSQL_BOTH (default), you'll get an array with both associative and number indices. 通过使用MYSQL_BOTH(默认),您将获得一个具有关联索引和数字索引的数组。

mysql_fetch_array() fetches both a numerical and associative array of each row's data. mysql_fetch_array()提取每行数据的数字数组和关联数组。 You need to use or the other to get the actual data as $row contains the entire dataset as an array even if it is only one value. 您需要使用或另一个来获取实际数据,因为$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;

FYI, you shouldn't use mysql_* functions in new code . 仅供参考, 您不应在新代码中使用mysql_*函数 They are no longer maintained and are officially deprecated . 它们不再维护,已正式弃用 See the red box ? 看到红色框了吗? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. 相反,要了解准备好的语句 ,并使用PDOMySQLi- 本文将帮助您确定哪一个。 If you choose PDO, here is a good tutorial . 如果您选择PDO, 这是一个很好的教程

It's perfectly fine, when you use mysql_fetch_array it will get you value in both places, by index and column name 很好,当您使用mysql_fetch_array ,它将通过索引和列名在两个地方为您带来价值

and for multiple rows you have to loop through like:- 对于多行,您必须像这样遍历:

while($row = mysql_fetch_array($queryIsbnArr)){
    print_r($row);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM