I have a problem iterating through an sql query:
$result = mysql_query("SELECT * FROM transactions");
while($row = mysql_fetch_array($result)) {
// this returns 3 rows
foreach ($row as $values)
{
//fputcsv($a_csv, $values;
echo $values;
}
}
The script iterates fine but it appears to be going through each row twice. So what I receive in output is the following:
value1value1value2value2value3value3
I'm not sure why this is - could anyone explain please?
Thankyou
mysql_fetch_array fetches both the named & the numerical keys. Use either mysql_fetch_assoc or mysql_fetch_row.
$result = mysql_query("SELECT * FROM transactions");
//return an associative array
while($row = mysql_fetch_assoc($result)) {
// this returns 3 rows
$values = "{$row["name_of_column1"]}, {$row["name_of_column2"]}, {$row["name_of_column3"]}";
//fputcsv($a_csv, $values;
//print the whole row array
print_r($row);
//echo value in format value1, value2, value3
echo $values;
}
You need to access $row like this $row[0] And $row shouldn't be in a foreach() itself unless it too is some kind of array you need to iterate through.
$result = mysql_query("SELECT * FROM transactions");
while($row = mysql_fetch_row($result))
{
echo $row[0];
echo $row[1];
// ... etc.
}
Related
Can someone please help? I'm new to PHP and struggling to make this bit of code to work. For example I have a sql database table with the following schema and data:
Type....rent_price
a..........100
b..........200
c..........300
I want to be able to echo say, "a", in one section and "200" in another. The following code will display "a" but then I can't seem to get it to display anything from the rent_price column using a second array.
$result = $mysqli->query("SELECT * FROM dbc_posts ORDER BY ID ASC limit 3");
for ($set = array (); $row = $result->fetch_assoc(); $set[] = $row['type']);
for ($set1 = array (); $row = $result->fetch_assoc(); $set1[] =$row['rent_price']);
?>
<?php echo $set[0];?>
<?php echo $set1[1];?>
You loop through the results twice, without resetting. Try to loop only once:
$result = $mysqli->query("SELECT * FROM dbc_posts ORDER BY ID ASC limit 3");
$set = array ();
$set1 = array ();
while ($row = $result->fetch_assoc())
{
$set[] = $row['type'];
$set1[] =$row['rent_price'];
}
?>
<?php echo $set[0];?>
<?php echo $set1[1];?>
Depending on what you mean by '"a" in one section and "200" in another', you may be able to forgo creating the intermediate arrays and just print the values from your query as you fetch them. Two cells in a table row, for example:
while ($row = $result->fetch_assoc()) {
echo "<tr><td>$row[type]</td><td>$row[rent_price]</td></tr>";
}
your data is in the first element of array
$set1[0]
but youre probably better off maintaining the naming throughout
$results = array();
while ($row = $result->fetch_assoc()){
$results[] = $row;
}
foreach ($results as $result){
echo $result['type'];
echo $result['rent_price'];
}
OR
$results = array();
while ($row = $result->fetch_assoc()){
$results['types'][] = $row['type'];
$results['rent_prices'][] = $row['rent_price'];
}
foreach ($results['types'] as $type){
echo $type;
}
foreach ($results['rent_prices'] as $rent_price){
echo $rent_price;
}
I want to put the results in variables seperately but for that I will need to be able to call them separate. Right now I have this query that retrieves 6 results (tested with mysqli_num_rows).
But when I print it, it will only shows the first row from my 6 results.
$result = mysqli_query ($con, $query) or die("FOUT: " . mysqli_error($con));
echo "<p>mysqli_num_rows($result)</p>";
$row = mysqli_fetch_row($result);
print_r($row);
mysqli_free_result($result);
Results from
print_r($row) =
Array ( [0] => Iran [1] => 28 )
To get all rows you will need to do something like:
$rows = array();
while($row = mysqli_fetch_row($result)) {
$rows[] = $row;
}
// $rows will now contain all rows in the result set
Your function, mysqli_fetch_row(), only returns a single row result:
http://php.net/manual/en/mysqli-result.fetch-row.php
Try looping through like this:
while ($row = mysqli_fetch_row($result) {
// Do something
}
Thanks,
Andrew
You should use while
while ($row = mysqli_fetch_row($result)){
print_r($row);
//do more stuff...
}
You can use fetch_assoc instead of fetch_row to have logical array keys
Try this it will work :
$result = mysqli_query ($con, $query) or die("FOUT: " . mysqli_error($con));
echo "<p>mysqli_num_rows($result)</p>";
$rows = array();
$row = mysqli_fetch_row($result);
do
{
$rows[] = $row;
}while($row = mysqli_fetch_row($result))
mysqli_free_result($result);
I have an existing database which has a table called PERSON with a field called NAME. What I’m trying to do is select all of the rows in the table where the NAME is "bill". And then I want the result to be stored in an array that I can step through at a later point.
Now, the problem is my code will only select the FIRST row with the name "bill" and ignore the rest of the rows where the NAME is "bill". At least that’s how it appears when I print out the array contents with print_r(). My code below:
<?php
$getAllPreview = "SELECT * from PERSON where NAME = 'bill'";
$getAllResult = #mysql_query( $getAllPreview );
$getAllRows = #mysql_fetch_assoc( $getAllResult );
print "<pre>";
print_r($getAllRows);
print "</pre>";
?>
<?php
$getAllPreview = "SELECT * from PERSON where NAME = 'bill'";
$getAllResult = #mysql_query( $getAllPreview );
while ($row = #mysql_fetch_assoc( $getAllResult ) ) {
$getAllRows[] = $row;
}
print "<pre>";
print_r($getAllRows);
print "</pre>";
?>
while($row = mysql_fetch_array($getAllResult, MYSQL_ASSOC)) {
$data[] = $row;
}
You just keep looping over mysql_fetch_assoc until no further rows are returned. If you want to output or process them, just do so in each iteration of the loop, as it's more efficient than placing it in an array first. But here you go anyway:
$allRows = array ();
while ($row = mysql_fetch_assoc( $getAllResult)) $allRows [] = $row;
<?php
$getAllPreview = "SELECT * from PERSON where NAME = 'bill'";
$getAllResult = #mysql_query( $getAllPreview );
$num_rows = mysql_num_rows($getAllResult);
while ($row = #mysql_fetch_assoc($getAllResult))
{
for($i=0;$i<$num_rows;$i++)
{
$array[] = $row;
}
}
?>
How do I return all the row values for a given id?
require('dbc.php');
mysql_select_db($db);
$result = mysql_query("SELECT * FROM about WHERE id=".$_GET['id']);
$row = mysql_fetch_assoc($result);
echo $row['content']; // return all values instead of just content
You can use mysql_fetch_row and then implode the array:
echo implode(",",mysql_fetch_row($result));
The following will work:
while ($row = mysql_fetch_assoc ($result) ) {
foreach($row as $column => $value){
echo "Row: $column - $value<br />";
}
}
But, I would suggest you move to MySQLi, for security and performance purposes. Once you do, you can use:
$all_rows = mysqli_fetch_all ($result, MYSQLI_ASSOC);
You could loop through this or call implode on it to return the values.
When I query my database with the following in my file, search.php, it only returns the first result it comes across.
$qry = "SELECT business_id FROM business WHERE zip like '%91326%'";
$rs = mysql_query($qry);
$rec = mysql_fetch_array($rs);
echo $session->showContents($rec);
showContents is just a utility function...
function showContents($array)
{
echo "<pre>";
print_r($array);
echo "</pre>";
}
showContents returned this:
Array
(
[0] => 3
[business_id] => 3
)
The crazy thing is, when I put the same query in sqlbuddy it gives me:
business_id
3
5
6
I am at a loss
mysql_fetch_array fetches only a single row. You want to use it several times to build an array with the entire result set:
$rec = array();
while(($row = mysql_fetch_array($rs)) !== FALSE) {
$rec[] = $row;
}
If you just want the ID's you want to select the ID:
$rec = array();
while(($row = mysql_fetch_array($rs)) !== FALSE) {
$rec[] = $row[0];
}
Try this:
$qry = "SELECT business_id FROM business WHERE zip like '%91326%'";
$rs = mysql_query($qry);
while ($rec = mysql_fetch_array($rs)) {
echo $session->showContents($rec);
}
That's because mysql_fetch_array only fetches a single row from the result set.
Typically you use it like this (from the manual):
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
printf("ID: %s Name: %s", $row[0], $row[1]);
}