Hey usually I use mysqli_fetch_array to display the content of my database in my CMS, but recently someone told me I should use mysqli_fetch_assoc and push the results into an array so that database only runs once instead of running the database for each record.
But I'm not really sure how to display my fields without showing them all, usually I would echo $data['field_name'], but what I've noticed with mysqli_fetch_assoc is I can't just echo $value['field_name'], all I can do is echo $value and it displays all the results.
This is what I've done, hope that makes sense. Thanks in advance for any help!
PHP
$sql = "SELECT * FROM app_categories";
if($result = query($sql)){
$list = array();
while($data = mysqli_fetch_assoc($result)){
array_push($list, $data);
}
foreach($list as $i=>$row){
foreach($row as $column=>$value){
echo $value;
}
}
}
why you don't just
echo $row['field_name'];
Related
Hey I'm running php mysqli query:
<?php do { echo $row['depicao']; ?>-<?php } while ($row = mysqli_fetch_array($query)); ?>
The result I need should show the following
data-data2-data3-data4 etc.
However what I'm seeing is:
-data-data2-data3-data4
how can I get the "-" to not appear as the first result?
Ive tried this but I get the same result.
<?php do { echo $row['depicao']; echo'-'; ?><?php } while ($row = mysqli_fetch_array($query)); ?>
Thanks
In
do { echo $row['depicao']; ?>-<?php }
while ($row = mysqli_fetch_array($query))
$row gets defined after first iteration, and not before. That's why
$row['depicao'] outputs nothing. If you had error_reporting on - you would also see a notice.
So, first fix is to define $row first and then output it:
while ($row = mysqli_fetch_array($query)) {
echo $row['depicao'];
echo '-';
}
But in this case your output will be ended with -.
So, one of the solutions is to collect values in array and implode'em:
$values = [];
while ($row = mysqli_fetch_array($query)) {
$values[] = $row['depicao'];
}
echo implode('-', $values);
Use while loop instead of do...while
Because when you use do while loop it'll execute once always and then it'll check the condition and that's why you got the - at very first of your result.
So use while loop to check the condition, if true then code under loop will execute otherwise not.
I'll strictly recommend you to learn about loops.
Try using while loop instead of do-while.
<?php while($row = mysqli_fetch_array($query)) { echo $row['depicao'];echo'-';} ?>
The problem with do while is that it executes at least once even if the condition fails. Hence one "-" is printed in beginning even without evaluating the condition.
If the goal is just to remove the extra "-" then it can be done with-
<?php do { echo $row['depicao']; if($row['depicao']!=null) echo "-"; } while ($row = mysqli_fetch_array($query)); ?>
Am making a really simple php page which pulls data from a really simple database and displays it as charts for a wall monitor.
I've got the data from the db, no problem, but I seem to be struggling splitting that data up
For now i'm just echoing the data to make sure i have what i need.
My code looks like this:
<?php
mysql_connect("host", "user", 'password' or die(mysql_error());
mysql_select_db("databax") or die(mysql_error());
$dbdata = mysql_query("SELECT * FROM dbcpman_resources")
or die(mysql_error());
$column = mysql_fetch_array( $dbdata );
echo $column[0]."<br>";
echo $column[1]."<br>";
echo $column[2]."<br>";
echo $column[3]."<br>";
echo $column[4]."<br>";
echo $column[5]."<br>";
?>
Indeed, it works - it will echo data from the database, but as i've not specified the row anywhere, its just giving me the first row.
I need to be able to work with each row seperately.
There will only ever be 6 rows in this table.
So can anyone help me out with how I go about replicating this for rows 2,3,4,5 and 6?
Thanks in advance!! :)
You need to loop over the result set. The easiest way is to use a while loop as this automatically terminates at the end of the result set, like this.
while ( $row = mysql_fetch_array( $dbdata );
echo $row [0]."<br>";
echo $row [1]."<br>";
echo $row [2]."<br>";
echo $row [3]."<br>";
echo $row [4]."<br>";
echo $row [5]."<br>";
}
Also if you were to change the function that returns the resuilts to use mysql_fetch_assoc() you can reference each field with the name it has on the database so the code is easier to read, like this:
I dont know your field names so I made some up.
while ( $row = mysql_fetch_array( $dbdata );
echo $row ['name']."<br>";
echo $row ['date']."<br>";
echo $row ['time']."<br>";
echo $row ['value1']."<br>";
echo $row ['value2']."<br>";
echo $row ['value3']."<br>";
}
First of all, I'd rather use mysql_fetch_assoc() instead of mysql_fetch_array() since it doesn't srew up your result, if the table structure changes.
It would be even better if you used either mysqli or PDO instead of mysql_* functions, since they are marked deprecated already!
Second please note, that either function just fetches ONE record from your resultset at a time. To fetch all records try the following:
$records = array();
while($row = mysql_fetch_assoc($dbdata)) {
$records[] = $row;
}
You can do a print_r($records); to see what's inside $records after fetching all.
put this line $column = mysql_fetch_array( $dbdata ); in while loop like this
while($column = mysql_fetch_array( $dbdata ))
{
echo $column[0]."<br>";
echo $column[1]."<br>";
echo $column[2]."<br>";
echo $column[3]."<br>";
echo $column[4]."<br>";
echo $column[5]."<br>";
}
it's just fetching the first element from the table.
Table name is categories which contains 2 columns : id, category
I cant understand why is it fetching just first row from the table.
<?php
$sql = "SELECT category FROM categories";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
//print_r($row);
?>
You need to iterate through the result set in order to retrieve all the rows.
while($row = mysql_fetch_assoc($result)) {
print($row);
}
Also, stop using mysql_ functions. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which.
Use this in while loop :
while ($row = mysql_fetch_assoc($result)) {
print_r($row);
}
Just like you wrote mysql_fetch_assoc($result); will get only one row. You have to use loop to get it all.
If you call mysql_fetch_assoc just once, you'll get only the first row...
while(false !== $row = mysql_fetch_assoc($result)) {
print_r($row)
}
The function you are using only does one record.
Try. ..while($row = mysqli_fetch_array($result))
{
echo $row['id'] . " " . $row['category'];
echo "";
}
For retrieve result set Use loop as per your need
foreach
Use when iterating through an array whose length is (or can be) unknown.
as
foreach($row as $val)
{
echo $val;
}
for
Use when iterating through an array whose length is set, or, when you need a counter.
for(i=0;i<sizeof($row);i++)
{
echo $row[$i];
}
while
Use when you're iterating through an array with the express purpose of finding, or triggering a certain flag.
while($row=mysqli_fetch_array($query))
{
echo $row['flag'];
}
I have problem with Mysql query. I wrote this code
enter code here
$result = mysql_query("SELECT * FROM description");
while ($row = mysql_fetch_array($result)){
$data[] = $row;
foreach ($data as $row){
echo $row['name'];
}
}
enter code here
and my output is:
First description
First description
Second description
First description
Second description
Third description
I have 3 description in db (first, second, third) and I don't have a clue why he give me something like this.
Does anyone knows what is wrong?
You need to move this to after your while loop:
foreach ($data as $row) {
echo $row['name'];
}
First you get all your rows in your $data variable, and only then you start echoing them out.
mysql_fetch_array() returns a dual array: integer AND string keyed. If you'd done:
while ($row = mysql_fetch_array($result)){
var_dump($row);
}
You'd see both keys. Try using mysql_fetch_assoc() instead, which returns the string-keyed version only.
your foreach loop is inside of your while loop. move it outside and try.
$result = mysql_query("SELECT * FROM description");
while ($row = mysql_fetch_array($result)){
$data[] = $row;
}
foreach ($data as $row){
echo $row['name'];
}
include("conn.php");
$result = mysql_query("SELECT * FROM sggame");
while($row = mysql_fetch_assoc($result));
{
$id = $row['id'];
echo $id;
echo 'working?';
}
The above code simply doesn't return anything out of the db. The row name is correct and the loop runs, showing that there is something in the database. However the row is just not echoed out at all. This is code i have used a thousand time before and am rather perplexed as to why it has stopped now! Any help, as always, is much appreciated
replace
while($row = mysql_fetch_assoc($result));
with
while($row = mysql_fetch_assoc($result))
did this code give off a notice or warning when you where running it ? I am just curious being that the problem was the semicolon.