Array is not working well - php

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'];
}

Related

Query within IF after a WHILE won't return any results [duplicate]

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.

While Loop Initiates Index as 1? [duplicate]

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.

mysqli ignoring the first row in a table

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.

Looping Through SQL Results in PHP - Not getting Entire Array

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.

MYSQL - Select specific value from a fetched array

I have a small problem and since I am very new to all this stuff, I was not successful on googling it, because I dont know the exact definitions for what I am looking for.
I have got a very simple database and I am getting all rows by this:
while($row = mysql_fetch_array($result)){
echo $row['id']. " - ". $row['name'];
echo "<br />";
}
Now, my question is: how do I filter the 2nd result? I thought something like this could work, but it doesnt:
$name2= $row['name'][2];
Is it even possible? Or do I have to write another mysql query (something like SELECT .. WHERE id = "2") to get the name value in the second row?
What I am trying to is following:
-get all data from the database (with the "while loop"), but than individually display certain results on my page. For instance echo("name in second row") and echo("id of first row") and so on.
If you would rather work with a full set of results instead of looping through them only once, you can put the whole result set to an array:
$row = array();
while( $row[] = mysql_fetch_array( $result ) );
Now you can access individual records using the first index, for example the name field of the second row is in $row[ 2 ][ 'name' ].
$result = mysql_query("SELECT * FROM ... WHERE 1=1");
while($row = mysql_fetch_array($result)){
/*This will loop arround all the Table*/
if($row['id'] == 2){
/*You can filtere here*/
}
echo $row['id']. " - ". $row['name'];
echo "<br />";
}
$counter = 0;
while($row = mysql_fetch_array($result)){
$counter++;
if($counter == 2){
echo $row['id']. " - ". $row['name'];
echo "<br />";
}
}
This While loop will automatically fetch all the records from the database.If you want to get any other field then you will only need to use for this.
Depends on what you want to do. mysql_fetch_array() fetches the current row to which the resource pointer is pointing right now. This means that you don't have $row['name'][2]; at all. On each iteration of the while loop you have all the columns from your query in the $row array, you don't get all rows from the query in the array at once. If you need just this one row, then yes - add a WHERE clause to the query, don't retrieve the other rows if you don't need them. If you need all rows, but you wanna do something special when you get the second row, then you have to add a counter that checks which row you are currently working with. I.e.:
$count = 0;
while($row = mysql_fetch_array($result)){
if(++$count == 2)
{
//do stuff
}
}
Yes, ideally you have to write another sql query to filter your results. If you had :
SELECT * FROM Employes
then you can filter it with :
SELECT * FROM Employes WHERE Name="Paul";
if you want every names that start with a P, you can achieve this with :
SELECT * FROM Employes WHERE Name LIKE "P%";
The main reason to use a sql query to filter your data is that the database manager systems like MySQL/MSSQL/Oracle/etc are highly optimized and they're way faster than a server-side condition block in PHP.
If you want to be able to use 2 consecutive results in one loop, you can store the results of the first loop, and then loop through.
$initial = true;
$storedId = '';
while($row = mysql_fetch_array($result)) {
$storedId = $row['id'];
if($initial) {
$initial = false;
continue;
}
echo $storedId . $row['name'];
}
This only works for consecutive things though.Please excuse the syntax errors, i haven't programmed in PHP for a very long time...
If you always want the second row, no matter how many rows you have in the database you should modify your query thus:
SELECT * FROM theTable LIMIT 1, 1;
See: http://dev.mysql.com/doc/refman/5.5/en/select.html
I used the code from the answer and slightly modified it. Thought I would share.
$result = mysql_query( "SELECT name FROM category;", db_connect() );
$myrow = array();
while ($myrow[] = mysql_fetch_array( $result, MYSQLI_ASSOC )) {}
$num = mysql_num_rows($result);
Example usage
echo "You're viewing " . $myrow[$view_cat]['name'] . "from a total of " . $num;

Categories