douqiang7462 2014-01-04 17:43
浏览 10
已采纳

'foreach'查询适用于Localhost但不适用于Server - PHP,MySQLi

This query below on localhost (PHP/5.4.22)

<?php
global $db;    
$query = $db->query("SELECT * FROM player WHERE playerID = '181783'") or die(mysqli_error($db));                
foreach ($query as $row) {
    $fName = $row['fName'];
    echo "First Name: " . $fName;    
}           
?>

How ever on my webhost (PHP/5.3.28)

this query returns

First Name: First Name: First Name: First Name: First Name:

There is currently only 1 result that returns where playerID = 181783, and it should echo the name 'Mario'

When I run the query like below on my webhost it works and echos Mario.

$query2 = $db->query("SELECT * FROM player WHERE playerID = '181783'");                             
$result = $query2->fetch_object();                
echo $result->fName;

So this must mean the db connection is set up correctly, as query2 is on the same page and working, just for some reason with query one it doesn't echo the result, but shows 'First Name:' 5 times.

It is like this for all foreach() functions I have used throughout my site, so I cannot just query 2.

To summaries, both queries work on localhost, when uploading to my webhost only query 2 works.

There are no errors despite error_reporting(E_ALL); being enabled

What should I do? Thanks.

Edit- db config

//MySQLI configuration
$_CONFIG['mysql']['hostname'] = 'localhost';
$_CONFIG['mysql']['username'] = '#';
$_CONFIG['mysql']['password'] = '#';
$_CONFIG['mysql']['database'] = '#';

//Initialize MySQLi connection
$db = new mysqli($_CONFIG['mysql']['hostname'], $_CONFIG['mysql']['username'], $_CONFIG['mysql']['password'], $_CONFIG['mysql']['database']);
if ($db->connect_errno) {
    die("MySQLi error: ".$db->connect_error);
}
  • 写回答

3条回答 默认 最新

  • dsfsad089111 2014-01-04 18:16
    关注

    The problem is simply that mysqli::query returns an instance of the mysqli_result class, which didn't implement the Traversable interface until PHP 5.4 came along (see the man pages under change log).
    Your webhost, by your own admission is running 5.3: "webhost (PHP/5.3.28)", so your code won't work, because the value of $query (which isn't a query but a resultset) isn't iterable.

    Just change the code to what we're all used to see:

    $result = $db->query('Your query here');
    if (!$result) throw new Exception('Query Failed!');
    while($row = $result->fetch_assoc())
    {
        echo 'First name: ', $row['fName'];
    }
    

    Job done.

    Notes:
    This fixed the problem, true enough, but you'd be best served with an upgrade of your server, or at least a test environment that replicates the production environment!

    I also noticed the or die in your code. Please, don't do that, especially not in a production/live environment. Google "or die must die". It's an out-dated construct. It's code-smell and doesn't really do anything, except for showing the clients the system's error messages. That's not nice.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装