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);
}
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.
Try this, Need to add $rows = $query->fetch_array();
$query = $db->query("SELECT * FROM player WHERE playerID = '181783'") or die(mysqli_error($db));
$rows = $query->fetch_array();
foreach ($rows as $row) {
Your first code snippet $query only executes query :
You are missing $query->fetch_object();
Example:
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";
if ($result = $mysqli->query($query)) {
/* fetch object array */
while ($obj = $result->fetch_object()) {
printf ("%s (%s)
", $obj->Name, $obj->CountryCode);
}
/* free result set */
$result->close();
}