Firstly, sorry about the long question... but this is doing my head in. Any help would be greatfully accepted.
I've written the following function to return data from a mysql database:
function injuryTable()
{
# get all players that are injured and their injuires...
$sql = "SELECT players.id, players.injury_id, players.pname, injuries_date.name, start_date, end_date
FROM players INNER JOIN injuries_date
ON injury_id = injuries_date.id";
$result = sqlQuery($sql);
return $result;
}
The sqlQuery function is as follows:
function sqlQuery($sql)
{
$products = array();
$link = dbConnect('localhost', 'root', '', 'umarrr');
$result = mysqli_query($link, $sql);
while ($row = mysqli_fetch_array($result))
{
$products[] = $row;
}
# return each row:
return $products;
#mysqli_close($link);
}
It's all connected to the database, and everything works fine. However when I try to iterate through the results: it only returns one row:
$injury_table = injuryTable();
// make it more readable:
foreach ($injury_table as $table);
{
echo $table['pname'];
echo $table['name'];
echo $table['start_date'];
echo $table['end_date'];
}
The sql statement I wrote above works perfectly in mysql query browser, so does anyone know what the problem here might be?
Output of print_r($injury_table)
Array ( [0] => Array ( [0] => 1 [id] => 1 [1] => 6 [injury_id] => 6 [2] => person [pname] => person [3] => wrist [name] => wrist [4] => 2008-11-21 [start_date] => 2008-11-21 [5] => 2010-11-11 [end_date] => 2010-11-11 ) [1] => Array ( [0] => 2 [id] => 2 [1] => 5 [injury_id] => 5 [2] => woman [pname] => woman [3] => neck [name] => neck [4] => 2009-11-12 [start_date] => 2009-11-12 [5] => 2010-09-09 [end_date] => 2010-09-09 ) [2] => Array ( [0] => 3 [id] => 3 [1] => 4 [injury_id] => 4 [2] => girl [pname] => girl [3] => groin [name] => groin [4] => 2010-11-27 [start_date] => 2010-11-27 [5] => 2010-12-01 [end_date] => 2010-12-01 ) [3] => Array ( [0] => 4 [id] => 4 [1] => 1 [injury_id] => 1 [2] => boy [pname] => boy [3] => achilles [name] => achilles [4] => 2010-02-01 [start_date] => 2010-02-01 [5] => 2010-03-23 [end_date] => 2010-03-23 ) [4] => Array ( [0] => 5 [id] => 5 [1] => 2 [injury_id] => 2 [2] => man [pname] => man [3] => toe [name] => toe [4] => 2010-01-01 [start_date] => 2010-01-01 [5] => 2010-02-02 [end_date] => 2010-02-02 ) )
Some things to check:
mysqli_query()
. You assume the query succeeds, and there's far too many reasons for a query to fail to NOT check each time. It will return boolean FALSE on failure.sqlQUery()
function if you'd like and reuse it. There aren't many situations where you'd want (or need) multiple handles at the same time, or a brand new sparkling clean handle each time.mysqli_num_rows()
to see how many the PHP version of the query is returning?Why are you opening a new connection to the database for each call? This is a very inefficient way of executing queries. I would pass the database connection as a parameter, or since you are using mysqli, just pass the $link
as a parameter.
As to why your code is not working, I do not know, but you can try some basic error reporting with the following:
$result = mysqli_query($link, $sql) or
trigger_error('Query Failed: ' . mysqli_error($link));
I would also add the MYSQL_ASSOC
to the fetching function, as you are not using the index-based array, this will make your script that much more efficient.
Hope this helps you out.
Really bad (and maybe insulting) answer...
foreach ($injury_table as $table);
{
echo $table['pname'];
echo $table['name'];
echo $table['start_date'];
echo $table['end_date'];
echo '<br/>'; // HTML new line
}
Or
foreach ($injury_table as $table);
{
echo $table['pname'];
echo $table['name'];
echo $table['start_date'];
echo $table['end_date'];
echo "
"; // Console new line
}