This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to iterate by row through a mysql query in php
After:
$result = mysql_query($query) or die("Error: Cant query database");
All Im trying to do is iterate through the result one by one and display the values separated by ";" in order from top to bottom, left to right;
I'm actually embarrassed I have to ask this "easy" question but Im not getting results lol. Might be the structure of the returned resource Im not getting, who knows.
I dont want to have to plugin in row or fields names, I just want to iterate through and display it, regardless of the result.
while($row = mysql_fetch_assoc($result)) {
// Your code goes here...
// OR
echo "<pre>"; print_r($row); echo "</pre>";
}
check: http://php.net/manual/de/function.mysql-query.php
If you want to display specific columns of the result:
while ($row = mysql_fetch_assoc($result)) {
echo $row['firstname'];
echo $row['lastname'];
echo $row['address'];
echo $row['age'];
}
or just the whole set of result:
$result_set = mysql_fetch_assoc($result) or die(mysql_error());
echo '<pre>';var_dump($result_set);echo '</pre>';
Here is a modified version of the answer I was given, that fits my specifications of being Comma delimited. Just in case their are others looking for the same thing.
$result = mysql_query($query) or die("Error: Cant query database");
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
foreach ($line as $col_value)
{
echo $col_value.";";
}
}
Related
This question already has answers here:
How do I iterate over the results in a MySQLi result set?
(2 answers)
Closed 5 months ago.
I'm very new to PHP and I'm trying to get some code that someone else has written to work. As the title says it is only showing the first result when it should be displaying many and I have no idea how to sort it (even though I imagine it's quite simple!). The code is below:
$result = mysqli_query($connection, ("SELECT id FROM details where publicposition like '%$trimvar%' or familyorgname like '%$trimvar%' or commercialorgind like '%$trimvar%' ORDER BY id "));
if (!$result)
{
die('Could not run query');
}
echo "<h1>Your staff conflict search on <font color='#ff0000'>'$trimvar'<font color='#000'> returned <font color='#ff0000'>$rows<font color='#000'> results</h1>";
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
{
echo "<table style='width: 20%; border='0';'>";
echo "<tr>";
echo "<th> Record ID : " . $row['id'] . "</th><br/>";
echo "</tr>";
}
echo "</table>";
Any help on this would be hugely appreciated!
mysqli_result::fetch_array -- mysqli_fetch_array — Fetch the next row
of a result set as an associative, a numeric array, or both
Try using mysqli_fetch_array inside a while loop to fetch one row at a time until the end is reached.
while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
echo "<tr>";
echo "<th> Record ID : " . $row['id'] . "</th>";
echo "</tr>";
}
I am trying to display results from a database onto my site. I have one entry in the database, and the result of that entry should be a "1". However, when I run the following code, it returns "1111111111111..." and continues to load ones. If there are 4 entries, I want the results to be displayed like:
1
2
3
4
I have looked around on this forum and others but cannot find anywhere where somebody had the same problem, and my code looks similar to the others. What did I do wrong?
functions.php:
$sqlgroup = mysql_query ("SELECT groupid FROM groups");
$grouprow = mysql_fetch_array ($sqlgroup);
Settings.php:
<?php while( ($row = $grouprow))
{
echo "<tr>";
echo "<td>".$row['groupid']."</td>";
echo "</tr>";
} ?>
First of all don't use mysql, use mysqli isntead:
$sqlgroup = mysqli_query ("SELECT groupid FROM groups");
while($row = mysqli_fetch_array($sqlgroup))
{
echo "<tr>";
echo "<td>".$row['groupid']."</td>";
echo "</tr>";
}
Can someone please point out what I'm sure is a stupidly obvious error in my code? The string "string" in my while loop is displaying the correct amount of times but not the results in row[0].
if (!isset($_GET['city']) & !isset($_GET['county'])) {
$getResults = "SELECT DISTINCT region FROM `locations` WHERE country = 'England'";
echo "No region or county set";
if ($result = $mysqli->query($getResults)) {
echo "Found results";
while ($row = $result->fetch_assoc()) {
echo "string";
echo $row[0];
}
}
}
To see the contents of the $row array dump it out like so var_dump($row).
I'm guessing you just need echo $row['region'] rather than $row[0]
You are using fetch_assoc() but you try to access the row using a index numbers.
Use fetch_row() instead.
All I need to do is echo array pocket $row[0].
while ($row = mysqli_result::fetch_array($result)) {
echo '<br>' . $row[1];
}
echo $row[0];
I have googled, but results haven't helped. I know I'm missing something stupid, but I'd appreciate the help!
I'm trying to echo the database column, 'ID'. An example of the data I want to echo would be '3'.
Copy $row[0] as you go.
$lastZero = "";
while ($row = mysqli_result::fetch_array($result)) {
echo '<br>' . $row[1];
$lastZero = $row[0];
}
echo $lastZero;
For debugging the whole row, write this:
var_dump($row)
To output the column ID, write this:
while ($row = mysqli_result::fetch_array($result)) {
var_dump($row); // This will output all values for each row as they're looped through
echo '<br>' . $row['ID'];
}
var_dump($row); // dumps the last row of the returned results, or false if no rows.
echo $row['ID']; // echos the ID of the last row. However if there are no rows, this will not work.
Be aware, as #Daedalus pointed out, $row is an array, so the var_dump line is really just for debugging. Because an array has multiple values, PHP doesn't know how you want to display it. You'll need to program how each element of $row is displayed.
Please give me a hand retrieving data from the database and showing it.
I'm learning and this is holding me from advancing :(
I have:
$result = mysql_query($sql);
$num_reg = mysql_num_rows($result);
echo $num_reg; // this shows 3
while($row = mysql_fetch_assoc($result))
{
foreach($row as $value)
{
//Now I need to operate with the rows values.
echo $row['PHONE'];
But this instead of printing the phone numbers, prints them 5 times each
What am I doing wrong?
Thanks a lot
Remove foreach loop
while($row = mysql_fetch_assoc($result))
{
echo $row['PHONE'];
echo $row['NAME'];
echo $row['OTHER_FIELD'];
}
you don't need foreach in that case
while($row = mysql_fetch_assoc($result))
{
echo $row['PHONE'];
}
Probably your $row has 2 elements, and inside foreach your echo $row['PHONE'] is called once for every element
do a print_r($row) in your while loop and a print_r($value) in your foreach loop.
Then ask you why do you echo $row in a loop on $row elements.
You don't need a second foreach() loop inside the while() loop to work on the values. What you're doing here is looping through the rows then looping through the values but if you already have access to the values via the $row variable you don't need to loop again. The fact you get the phone number 5 times suggests you have 5 columns in your table.
An example - remove the foreach() loop:
while($row = mysql_fetch_assoc($result)){
echo $row['PHONE'];
echo $row['NAME'];
echo "<br />";
}
I'm guessing you have a variable called "name" but if not just swap it for one you do have. The last echo just prints a new line to make it easier to read.