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条)

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计