Here is the code I'm using to pull the data from the table:
require_once 'connect.php';
$sql = "SELECT * FROM `db-news`";
$result = $mysqli->query($sql);
$row = mysqli_fetch_assoc($result);
while ($row = $result->fetch_assoc()) {
printf ($row['pagename'].' - To edit this page click here<br>');
}
Always the first row is ignored. I'm not calling mysqli_fetch_assoc twice as with some other examples on SO. I've tried changing echo to printf in the while loop and still the first row is ignored in the DB.
I'm at a loss as to what I should try next?
mysqli not ignoring it but actually you are fetching first row before while loop
$row = mysqli_fetch_assoc($result); //remove this line
while ($row = $result->fetch_assoc()) {
....
}
The problem is the first row of the following excerpt:
$row = mysqli_fetch_assoc($result);
while ($row = $result->fetch_assoc()) {
The mysqli_fetch_assoc already gets the first row (and thus in the while loop you are already 1 step further).
You should either put that line instead of the $reslut->fetch_assoc part into the while statement or delete it. That should solve the problem.
mysqli_fetch_assoc() is the same as $result->fetch_assoc() and whenever you call this function it will advance an internal pointer to the next row. You are calling this function once just before the loop which means that you are reading the first row and ignoring the result. Remove that line.
If you need to fetch the first line and then still loop through the whole result use foreach instead of while. For example:
require_once 'connect.php';
$sql = "SELECT * FROM `db-news`";
$result = $mysqli->query($sql);
foreach($result as $row) {
printf($row['pagename'].' - To edit this page click here<br>');
}
Use do while instead of while as it will not skip the first step.
Related
Here is the code I'm using to pull the data from the table:
require_once 'connect.php';
$sql = "SELECT * FROM `db-news`";
$result = $mysqli->query($sql);
$row = mysqli_fetch_assoc($result);
while ($row = $result->fetch_assoc()) {
printf ($row['pagename'].' - To edit this page click here<br>');
}
Always the first row is ignored. I'm not calling mysqli_fetch_assoc twice as with some other examples on SO. I've tried changing echo to printf in the while loop and still the first row is ignored in the DB.
I'm at a loss as to what I should try next?
mysqli not ignoring it but actually you are fetching first row before while loop
$row = mysqli_fetch_assoc($result); //remove this line
while ($row = $result->fetch_assoc()) {
....
}
The problem is the first row of the following excerpt:
$row = mysqli_fetch_assoc($result);
while ($row = $result->fetch_assoc()) {
The mysqli_fetch_assoc already gets the first row (and thus in the while loop you are already 1 step further).
You should either put that line instead of the $reslut->fetch_assoc part into the while statement or delete it. That should solve the problem.
mysqli_fetch_assoc() is the same as $result->fetch_assoc() and whenever you call this function it will advance an internal pointer to the next row. You are calling this function once just before the loop which means that you are reading the first row and ignoring the result. Remove that line.
If you need to fetch the first line and then still loop through the whole result use foreach instead of while. For example:
require_once 'connect.php';
$sql = "SELECT * FROM `db-news`";
$result = $mysqli->query($sql);
foreach($result as $row) {
printf($row['pagename'].' - To edit this page click here<br>');
}
Use do while instead of while as it will not skip the first step.
I want to give some data from a MySQL database to a Android App via JSON.
So far i have written this php script to give me the JSON object:
$result = mysqli_query($con,"SELECT * FROM job");
$row = mysqli_fetch_assoc($result);
while($row = mysqli_fetch_assoc($result)){
$output[] = $row;
}
print(json_encode($output));
I have found out by accident that when I dont add the line "$row = mysqli_fetch_assoc($result);" before the while-loop it doesn`t return anything. But when I do add this line like in the example the JSON object doesnt contain the first element.
I believe its because of this line where $row already contains the first line.
Hopefully you can help me :)
With purpose to figure out what wrong with your query you have to do something like this:
if ($result = mysqli_query($db, $sql)) {
// Here you can see and check count of rows in your result set.
$numRows = mysqli_num_rows($result);
// ...
} else {
// Here you can see and check and understand error.
var_export(mysqli_error($db));
}
with purpose to traverse all your rows from result set you have to do:
while ($row = mysqli_fetch_assoc($result)) {
var_export($row);
}
and you must not have $row = mysqli_fetch_assoc($result) before while loop.
I am new in PHP. I have a code in which i use 2 sql commands. First command fetch 1st latest row and second command fetch 2nd latest row. This code is place in file sqlquery.php
here is code of sqlquery.php
<?php
include ("connection.php");
Problem of my code is my array print same record in all rows. But in Db there is different records. My code is print only first record in each row
I want to output of my code is like this
The problem is the double loop, now for every result from the first query you add an array item for each result of the second query duplicating the array items effectively, you can change this
while($row1 = mysql_fetch_assoc($result1)){
while($row2 = mysql_fetch_assoc($result2)){
To:
while($row1 = mysql_fetch_assoc($result1) && $row2 = mysql_fetch_assoc($result2))
{
It would be better to change your sql query though to incorporate all values in one request.
Another thing you can do, though is less nice:
while($row1 = mysql_fetch_assoc($result1))
{
$opinion[]= $row1['opinion'];
$action[]= $row1['atitle'];
$long_term[]= $row1['ltitle'];
$outlook[]= $row1['otitle'];
$rating_type[]= $row1['ttitle'];
$short_term[]= $row1['stitle'];
}
while($row2 = mysql_fetch_assoc($result2))
{
$p_long_term[]= $row2['ltitle'];
$p_short_term[]= $row2['stitle'];
}
Here is the code I'm using to pull the data from the table:
require_once 'connect.php';
$sql = "SELECT * FROM `db-news`";
$result = $mysqli->query($sql);
$row = mysqli_fetch_assoc($result);
while ($row = $result->fetch_assoc()) {
printf ($row['pagename'].' - To edit this page click here<br>');
}
Always the first row is ignored. I'm not calling mysqli_fetch_assoc twice as with some other examples on SO. I've tried changing echo to printf in the while loop and still the first row is ignored in the DB.
I'm at a loss as to what I should try next?
mysqli not ignoring it but actually you are fetching first row before while loop
$row = mysqli_fetch_assoc($result); //remove this line
while ($row = $result->fetch_assoc()) {
....
}
The problem is the first row of the following excerpt:
$row = mysqli_fetch_assoc($result);
while ($row = $result->fetch_assoc()) {
The mysqli_fetch_assoc already gets the first row (and thus in the while loop you are already 1 step further).
You should either put that line instead of the $reslut->fetch_assoc part into the while statement or delete it. That should solve the problem.
mysqli_fetch_assoc() is the same as $result->fetch_assoc() and whenever you call this function it will advance an internal pointer to the next row. You are calling this function once just before the loop which means that you are reading the first row and ignoring the result. Remove that line.
If you need to fetch the first line and then still loop through the whole result use foreach instead of while. For example:
require_once 'connect.php';
$sql = "SELECT * FROM `db-news`";
$result = $mysqli->query($sql);
foreach($result as $row) {
printf($row['pagename'].' - To edit this page click here<br>');
}
Use do while instead of while as it will not skip the first step.
I'm probably missing something easy, but I seem to be blocked here... I have a MySQL database with two tables and each table has several rows. So the goal is to query the database and display the results in a table, so I start like so:
$query = "SELECT name, email, phone FROM users";
Then I have this PHP code:
$result = mysql_query($query);
Then, I use this to get array:
$row = mysql_fetch_array($result);
At this point, I thought I could simply loop through the $row array and display results in a table. I already have a function to do the looping and displaying of the table, but unfortunately the array seems to be incomplete before it even gets to the function.
To troubleshoot this I use this:
for ($i = 0; $i < count($row); $i++) {
echo $row[$i] . " ";
}
At this point, I only get the first row in the database, and there are 3 others that aren't displaying. Any assistance is much appreciated.
You need to use the following because if you call mysql_fetch_array outside of the loop, you're only returning an array of all the elements in the first row. By setting row to a new row returned by mysql_fetch_array each time the loop goes through, you will iterate through each row instead of whats actually inside the row.
while($row = mysql_fetch_array($result))
{
// This will loop through each row, now use your loop here
}
But the good way is to iterate through each row, as you have only three columns
while($row = mysql_fetch_assoc($result))
{
echo $row['name']." ";
echo $row['email']." ";
}
One common way to loop through results is something like this:
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
print_r($row);
// do stuff with $row
}
Check out the examples and comments on PHP.net. You can find everything you need to know there.