PHP Script not returning db rows - php

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.

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.

Getting specified rows from an array

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>";
}

while loop is not working upon selecting multiple colums from a table

i have a table with 5 columns like TABLE(a,b,c,d,e).
now i want to select all the columns from TABLE.but the script is not workling once the while loop starts.
my php code is:
include_once $_SERVER['DOCUMENT_ROOT'].'/include/db.inc.php' ;
$sql="select * from TABLE";
$result = mysqli_query($link,$sql );
if (!$result)
{
include_once "wall.html.php";
echo'<tr><td align="center"> OOOOPPPPPSSS!!!SORRY,UNABLE TO DISPLAY LATEST 25 TOPICS</td></tr>';
exit();
}
while ($row = mysqli_fetch_array($result))
{
$as[]=$row['a'];
$bs[]=$row['b'];
$cs[]=$row['c'];
$ds[]=$row['d'];
$es[]=$row['e'];
}
foreach($as as $x)
{
$name=$x;
}....................and so on
i have checked and debuged in many ways.what i noticed the script is not executing after while loop.
you forgot to write $ before variables:
$as[]=$row['a'];
^------------
bs[]=$row['b'];
cs[]=$row['c'];
ds[]=$row['d'];
es[]=$row['e'];
What about using array_push()? Also, use print_r() for $as[] and all the arrays you got and paste the output here so we get a clearer idea of what the issue exactly is.
Try this and tell what print_r() function is printing so that we can identify the problem.
while ($row = mysqli_fetch_array($result))
{
$as[]=$row['a'];
$bs[]=$row['b'];
$cs[]=$row['c'];
$ds[]=$row['d'];
$es[]=$row['e'];
}
print_r($as);
foreach($as as $x)
{
$name=$x;
}
First remove the brackets from $as[], the result of $row['a'] will not be an array.
And that's the reason the foreach is not working, you're not getting an array back.
If the resulting $as is empty, but there is something in your database, make sure the rownames are valid.
Use $row[0] instead of $row['A'].
Debug your code using print_r($row) to the see the contents of the results.

mysqli_fetch_assoc

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

Mysql database retrieve multiple rows

I use a mysql database. When I run my query I want to be able to put each row that is returned into a new variable. I dont know how to do this.
my current code:
<?php
$result=mysql_query("SELECT * FROM table WHERE var='$var'");
$check_num_rows=mysql_num_rows($result);
while ($row = mysql_fetch_assoc($result))
{
$solution=$row['solution'];
}
?>
The thing is that check num rows can return a row of an integer 0-infinity. If there are more solutions in the database how can I assign them all a variable. The above code works fine for 1 solution, but what if there are more? Thanks.
You can't give each variable a different name, but you can put them all in an array ... if you don't know how this works I suggest looking at a basic tutorial such as http://www.w3schools.com/php/php_arrays.asp as well as my code.
A very simple way (obviously I haven't included mysql_num_rows etc):
$solutions = array()
while($row = mysql_fetch_assoc($result)) {
$solutions[] = $row['solution'];
}
If you have three in your result solutions will be:
$solutions[0] -> first result
$solutions[1] -> second
$solutions[2] -> third
<?php
$result=mysql_query("SELECT * FROM table WHERE var='$var'");
$solution = array();
$check_num_rows=mysql_num_rows($result);
while ($row = mysql_fetch_assoc($result))
{
$solution[]=$row['solution'];
}
?>

Categories