从数据库中获取结果时警告非法字符串偏移量

时间:2017-11-14 16:11:00

标签: php

我使用以下代码查找database

中的数据
echo $query = "select * from `oc_order` where `store_url`='' ORDER BY `order_id` DESC LIMIT 5";
$mysqlq = mysqli_query($connection,$query) or die(mysqli_error($connection));
$mysqlw = mysqli_fetch_array($mysqlq);
echo "<pre>";
print_r($mysqlw);
foreach($mysqlw as $mysqlw)
{
    echo $mysqlw['telephone'];
}
?>

我得到以下数组

Array
(
    [0] => 73
    [order_id] => 73
    [1] => 0
    [invoice_no] => 0
    [2] => 
    [invoice_prefix] => 
    [3] => 0
    [store_id] => 0
    [4] => 
    [store_name] => 
    [5] => 
    [store_url] => 
    [6] => 9
    [customer_id] => 9
    [7] => 1
    [customer_group_id] => 1
    [8] => T****
    [firstname] => ****
    [9] => 
    [lastname] => 
    [10] => ********
    [email] => *******
    [11] => *****
    [telephone] => *****
    [12] => 
    [fax] => 
    [13] => 364ce6e2003bb1404f34
    [custom_field] => 364ce6e2003bb1404f34
    [14] => 
    [payment_firstname] => 
    [15] => 
    [payment_lastname] => 
    [16] => 
    [payment_company] => 
    [17] => 
    [payment_address_1] => 
    [18] => 
    [payment_address_2] => 
    [19] => 
    [payment_city] => 
    [20] => 
    [payment_postcode] => 
    [21] => 
    [payment_country] => 
    [22] => 0
    [payment_country_id] => 0
    [23] => 
    [payment_zone] => 
    [24] => 0
    [payment_zone_id] => 0
    [25] => 
    [payment_address_format] => 
    [26] => 
    [payment_custom_field] => 
    [27] => 
    [payment_method] => 
    [28] => 
    [payment_code] => 
    [29] => 
    [shipping_firstname] => 
    [30] => 
    [shipping_lastname] => 
    [31] => 
    [shipping_company] => 
    [32] => 
    [shipping_address_1] => 
    [33] => 
    [shipping_address_2] => 
    [34] => 
    [shipping_city] => 
    [35] => 
    [shipping_postcode] => 
    [36] => 
    [shipping_country] => 
    [37] => 0
    [shipping_country_id] => 0
    [38] => 
    [shipping_zone] => 
    [39] => 0
    [shipping_zone_id] => 0
    [40] => 
    [shipping_address_format] => 
    [41] => 
    [shipping_custom_field] => 
    [42] => 
    [shipping_method] => 
    [43] => 
    [shipping_code] => 
    [44] => 1,19
    [comment] => 1,19
    [45] => 100.0000
    [total] => 100.0000
    [46] => 0
    [order_status_id] => 0
    [47] => 0
    [affiliate_id] => 0
    [48] => 0.0000
    [commission] => 0.0000
    [49] => 0
    [marketing_id] => 0
    [50] => 
    [tracking] => 
    [51] => 0
    [language_id] => 0
    [52] => 0
    [currency_id] => 0
    [53] => INR
    [currency_code] => INR
    [54] => 1.00000000
    [currency_value] => 1.00000000
    [55] => 
    [ip] => 
    [56] => 
    [forwarded_ip] => 
    [57] => 
    [user_agent] => 
    [58] => 
    [accept_language] => 
    [59] => 0000-00-00 00:00:00
    [date_added] => 0000-00-00 00:00:00
    [60] => 0000-00-00 00:00:00
    [date_modified] => 0000-00-00 00:00:00
)

但是当我尝试打印echo $mysqlw['telephone'];的值时,它会抛出警告Warning: Illegal string offset 'telephone' in

我在这段代码中犯了什么错误?

2 个答案:

答案 0 :(得分:2)

您正试图对您在帖子中显示的阵列进行预测。这意味着你要循环遍历数组中的每个项目。通过尝试访问$mysqlw['telephone'],您希望数组中的项目是包含密钥“telephone”的另一个数组。

我相信你打算这样做:

while ($mysqlw = mysqli_fetch_array($mysqlq) {
   echo $mysqlw['telephone'];
}

但是,你真的应该读一下如何以更现代的方式做MySQL。当你开始在查询中涉及用户输入时,以你在这里使用它的方式使用像mysqli_query这样的东西会打开你注入攻击。

答案 1 :(得分:1)

请注意,您的foreach语句在'as'

的两侧都有相同的变量名称
foreach($mysqlw as $mysqlw)

如果要循环并访问数组的每个值,请执行以下操作:

foreach($mysqlw as $value)
{
    echo $value;
}