Fetch array returns only one row - php

<?php
function comment($postid,$db_con)
{
$commentdiv='';
$sql="SELECT userid,time,comment FROM comments WHERE postid='$postid' LIMIT 3";
$query = mysqli_query($db_con, $sql);
while($row = mysqli_fetch_array($query,MYSQLI_ASSOC)){
$uid=$row["userid"];
$timecomment=$row["time"];
$comment=$row["comment"];
$sql="SELECT username,photo FROM users WHERE id='$uid'";
$query = mysqli_query($db_conx, $sql);
$row = mysqli_fetch_array($query,MYSQLI_ASSOC);
$username=$row["username"];
$photo=$row["photo"];
$userphoto='<img src="xxx/'.$username.'/'.$photo.'">';
if($photo== NULL){
$userphoto = '<img src="xxx/default.png">';
}
$commentdiv.='<div class="xxxxxxx"><div class="yyyyyy">'.$userphoto.'</div><div class="zzzzz">'.$username.'</div><div class="vvvvv">'.$comment.'</div></div>';
}
return $commentdiv;
}
?>
I am new to PHP, I am trying to return 3 comments from above PHP code, but above code returns only 1 row from database, why does fetch array return only 1 row when there is more then 1 row?

Try,I use $query2 = mysqli_query($db_conx, $sql); for your second query,Because it will reset first loop
<?php
function comment($postid,$db_con)
{
$commentdiv='';
$sql="SELECT userid,time,comment FROM comments WHERE postid='$postid' LIMIT 3";
$query = mysqli_query($db_con, $sql);
while($row = mysqli_fetch_array($query,MYSQLI_ASSOC)){
$uid=$row["userid"];
$timecomment=$row["time"];
$comment=$row["comment"];
$sql="SELECT username,photo FROM users WHERE id='$uid'";
$query2 = mysqli_query($db_conx, $sql); // added new variable
$row = mysqli_fetch_array($query2,MYSQLI_ASSOC);
$username=$row["username"];
$photo=$row["photo"];
$userphoto='<img src="xxx/'.$username.'/'.$photo.'">';
if($photo== NULL){
$userphoto = '<img src="xxx/default.png">';
}
$commentdiv.='<div class="xxxxxxx"><div class="yyyyyy">'.$userphoto.'</div><div class="zzzzz">'.$username.'</div><div class="vvvvv">'.$comment.'</div></div>';
}
return $commentdiv;
}
?>

You are overwriting the mysql resource variable that gives the result inside the while loop.
while($row = mysqli_fetch_array($query,MYSQLI_ASSOC)){
^^^^^^ Original resource variable
and inside the while loop again you are using the query variable
$query = mysqli_query($db_conx, $sql);
^^^^^^ Overwriting $query inside the while loop
One suggestion would be to rename the variable inside the loop to something else.

Related

Select query for getting a single value is not working

$sql = "SELECT temail FROM teacherusers WHERE tfullname='$teachername' limit 1";
$result = mysql_query($sql);
$value = mysql_fetch_object($result);
$teacheremail2 = $value->temail;
echo $teacheremail2;
echo $teacheremail2 returns nothing.
$teachername is valid and i have checked multiple times.
It should be a two-dimensional array , you need
$value[0]->temail
The result of mysql_fetch_object($result) is an object(stdClass).
The explanation of object(stdClass) ican be found at this link
$sql = "SELECT temail FROM teacherusers WHERE tfullname='$teachername' limit 1";
$result = mysql_query($sql);
while ($value = mysql_fetch_object($result))
{
$teacheremail2 = $value->temail;
echo $teacheremail2;
}
First off, you'll want to run the query directly against your database to ensure that the query returns some kind of result.
Secondly, if that works, you'll want to echo $value directly to check that you are getting results back on the webpage.
Then you can check if temail is a field of $value
$sql = "SELECT temail FROM teacherusers WHERE tfullname='$teachername' limit 1";
$result = mysql_query($sql);
while ($value = mysql_fetch_object($result))
{
$teacheremail2 = $value->temail;
echo $teacheremail2;
}
hope this help

Loop until it gets to empty database field

this code works:
$i=1;
require('db.php');
$query = "SELECT own".$i." FROM users WHERE username='".$_SESSION['username']."'";
$result = mysqli_query($con, $query);
$row = mysqli_fetch_assoc($result);
So i try to put it into loop but it is not working. I get only the first result.
Is it even possible to do it that way?
while (isset($row['own'.$i])) {
require('db.php');
$query = "SELECT own".$i." FROM users WHERE username='".$_SESSION['username']."'";
$result = mysqli_query($con, $query);
$row = mysqli_fetch_assoc($result);
echo $row['own'.$i];
$i++;
}
Also I use the above code to make first check before the loop
You are always selecting only SELECT own1.
You need foreach instead of while
require('db.php');
$query = "SELECT * FROM users WHERE username='".$_SESSION['username']."'"; // SQL Injection HERE!
$result = mysqli_query($con, $query);
$rows = mysqli_fetch_assoc($result);
foreach ($rows as $row)) {
var_dump($row);
}

How to get total number of row by using this function php mysql?

How to get total number of row by using this function php mysql ?
i use this code for display data from mysql. It's work good,
Buy i want to know can i get total number of row by using this code ?
<?PHP
include("connect.php");
$query = "SELECT * FROM table";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
}
?>
Try this mysql_num_rows ($result)
Use this line of code
<?PHP
include("connect.php");
$query = "SELECT * FROM table";
$result = mysql_query($query) or die(mysql_error());
$number_of_rows = mysql_num_rows($result); // this will return the number of rows found by the $result query.
echo $number_of_rows;
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
}
?>

how to get the number of rows of mysql db with php

i would like to get the number of rows of a mysql database table with one single statement or a function
include "opendatabase.php"; //opens database
while (NUMBEROFROWS > 0){
//do something
}
the NUMBEROFROWS should be replaced with the statement that returns the number of rows
i already tried to create a function
function getRowNumber(){
$query = "SELECT COUNT(*) FROM `votes`";
$result = mysql_query($query, $connect);
list($length) = mysql_fetch_row($result);
return $length;
}
but it does not work if i dont put the include "opendatabase.php"; in it.
what am i doing wrong
$result = mysql_query("SELECT * FROM tablename");
if (mysql_num_rows($result) > 0) {
// rows found..
}
the problem is that include "opendatabase.php"; runs in another scope like described here
http://www.php.net/manual/en/language.variables.scope.php
there is a global $connect missing within the function
Here you go:
function num(){
$data = mysql_query("SELECT * FROM table");
if(mysql_num_rows($data) > 0){
while($row = mysql_fetch_assoc($data)){
// do something with your data..
}
}
}
would this work for you ?
function getRowNumber()
{
$query = "SELECT COUNT(*) as counts FROM `votes`";
$result = mysql_query($query, $connect);
$row = mysql_fetch_array($result);
$counts = $row['counts'];
return $counts;
}

php mysql_fetch_array

Hy,
I'm new in php and I'm having some problems with mysql_fetch_array().
$sql = "SELECT mandant_kurz FROM pu_mandant ORDER BY mandant_kurz ASC";
$result = mysql_query($sql);
$list = mysql_fetch_array($result);
There are more than 100 entries in the database but the mysql_fetch_array() delivers only one. When I'm trying it with a while-loop it isn't working either.
Here it goes with my while-loop
$sql = "SELECT mandant_kurz FROM pu_mandant ORDER BY mandant_kurz ASC";
$result = mysql_query($sql);
while($list = mysql_fetch_array($result));
Update:
You are not doing anything inside your loop:
while($list = mysql_fetch_array($result));
Try:
while($list = mysql_fetch_array($result){
echo $list['mandant_kurz'];
}
Also try running your query in MySQL client to make sure it actually returns 100 rows.
You will have to use loop:
while($row = mysql_fetch_array($result)){
echo $row['mandant_kurz'];
}
This echoes just first row.
$list = mysql_fetch_array($result);
echo $list['mandant_kurz'];
Moves pointer to first row and echoes all rows
mysql_data_seek($result,0);
while( $list = mysql_fetch_array($result) ) {
echo $list['mandant_kurz'];
}

Categories