I want to display all the values present in ftext field i want to
display all values into single variable like $spd.
When i print this variable $spd it displays only one (ftext)value.but ftext
contains multiple values.
so please help me how to display all values using $spd variable
Thanks in advance...!
$query = mysqli_query($link, "SELECT id, ftext from projectfield");
while($row = mysqli_fetch_array($query))
{
$spd = $row['ftext'];
}
Every time variable updates so it contains only last values. Below approaches resolve it:-
#Either print all values
$query = mysqli_query($link, "SELECT id, ftext from projectfield");
while($row = mysqli_fetch_array($query)){
echo $spd = $row['ftext'];
}
or
# or hold ftext in array and then use this
$spd = array();
$query = mysqli_query($link, "SELECT id, ftext from projectfield");
while($row = mysqli_fetch_array($query)){
$spd[] = $row['ftext'];
}
foreach($spd as $val){
echo $val;
}
you can use id as index
$spd = array();
$query = mysqli_query($link, "SELECT id, ftext from projectfield");
while($row = mysqli_fetch_array($query)){
$spd[$row['id']] = $row['ftext'];
}
foreach($spd as $key =>$value){
echo $key." ".$value;
}
You are getting a single value because you are running a loop on the row that came out in the query. and saving it in the $spd. so only the last ftext is saved as all other are rewritten every time the loop runs.
What you can do is append the data to $spd with a delimiter instead of assigning it.
$query = mysqli_query($link, "SELECT id, ftext from projectfield");
while($row =mysqli_fetch_array($query)){
$spd .= $row['ftext'].",";
}
and echo it.
Related
I am fetching records as follows:
$aResult = array();
$sql = "SELECT notes FROM table WHERE user_id = '$user_id'";
$result = $conn->query($sql);
while($row = mysqli_fetch_array($result)){
$aResult['query_result'] = $row;
}
This returns only the last record in the table. I need to return all records from the sql statement.
change you $aResult['query_result'] = $row; to $aResult['query_result'][] = $row;
You've override the result each time, so you just get one.
It seems your loop constantly overwrites the value and hence you will only ever seen the last row. I think you might see better results if you do something like:
while($row = mysqli_fetch_array($result))
{
$aResult[] = $row;
}
so that each new row gets appended to your array
Try with following code, currently You are initiating the values to the same array key :
$aResult = array();
$sql = "SELECT notes FROM table WHERE user_id = '$user_id'";
$result = $conn->query($sql);
while($row = mysqli_fetch_array($result)){
$aResult['query_result'][] = $row;
}
for more Detail On Array
I'm taking data from mysql database table. The table have ID and "POST" columns. I've ordered posts by id's from bottom so i always have the newest post on the first place. But when i want to echo specific post (eg. with id 5) i can't echo it with $col = mysqli_fetch_array($result); echo $col;. I've tried with foreach loop but it echo's all posts. So I thought if i could put them into array with foreach loop it would do the job.
$sql = "SELECT * FROM `post` ORDER BY `id` DESC";
$result = mysqli_query($con, $sql);
$col = mysqli_fetch_array($result);
foreach($col as $cols) {
}
I've tried a lot of things and spent a lot of time on research but still don't have idea how to do it.
Thanks for your ideas and help.
$sql = "SELECT * FROM `post` ORDER BY `id` DESC";
$result = mysqli_query($con, $sql);
$col = mysqli_fetch_array($result);
foreach($col as $cols) {
if($col['id'] == 5) {
print_r($col);
}
}
mysqli_fetch_array fetchs a result row as an associative, a numeric array, or both.
You need to specify the name of the column you want to print out.
Because you may have more than one row in your result set, you should use a loop (while) like so:
while ($row = mysqli_fetch_array($result)) {
echo $row['POST']; // 'POST' here is the name of the column you want to print out
}
Hope this helps!
UPDATED:
If you want to get a specific post, you have to change your SQL to something like this:
$sql = "SELECT * FROM `post` WHERE `id` = $wanted_post_id";
<?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.
I am trying to retrieve the data stored in an unknown number of mySQL database rows and display them using HTML. At the moment I can display data from one row.
Each row has a unique id number, I was planning to iterate through by comparing this number to the variable counter. But it would then leave me with the issue of displaying the results in HTML. At the moment I am just echoing variables that contain data from the rows. However what I want to create is a HTML list that increases in length depending on how many rows are in the table.
Here is my current PHP code for retrieving a row from the database is:
$sql = "SELECT * FROM project_tasks WHERE project_name='$proj_name' AND task_id='$counter'";
$query = mysqli_query($db_conx, $sql);
$row = $query->fetch_assoc();
$task_id = $row["task_id"];
$proj_name = $row["project_name"];
$task_name = $row["task_name"];
$task_importance = $row["task_importance"];
$task_description = $row["task_description"];
$task_deadline = $row["task_deadline"];
$task_members = $row["task_members"];
$task_budget = $row["task_budget"];
At the moment I am just displaying some of the results in HTML using this code:
<div id="inner_container">
<?php echo "$task_id $proj_name $task_name $task_deadline"; ?>
</div>
$sql = "SELECT * FROM project_tasks WHERE project_name='$proj_name' AND task_id='$counter'";
$query = mysqli_query($db_conx, $sql);
while($row = $query->mysqli_fetch_assoc()) {
$task_id = $row["task_id"];
$proj_name = $row["project_name"];
$task_name = $row["task_name"];
$task_importance = $row["task_importance"];
$task_description = $row["task_description"];
$task_deadline = $row["task_deadline"];
$task_members = $row["task_members"];
$task_budget = $row["task_budget"];
echo "$task_id $proj_name $task_name $task_deadline";
}
Since you have built an associative array using fetch_assoc all you need to do is loop through that array. The OO example on http://php.net/manual/en/mysqli-result.fetch-assoc.php should get you what you need. A quick example:
$sql = "SELECT * FROM project_tasks WHERE project_name='$proj_name' AND task_id='$counter'";
$query = mysqli_query($db_conx, $sql);
echo '<div id="inner_container">';
while ($row = $query->fetch_assoc()) {
$proj_name = $row["project_name"];
$task_name = $row["task_name"];
$task_deadline = $row["task_deadline"];
echo "$task_id $proj_name $task_name $task_deadline";
};
/* free result set */
$row->free();
echo '</div>;
$query = "SELECT * FROM table WHERE data = '$userinput'";
$row = mysql_query($query);
while ($row = mysql_fetch_array($row))
{
echo $row['data'];
}
Ok So my questions are:
What exactly does mysql_fetch_array return?
AND if my WHERE clause finds multiple matches, shouldn't the loop execute numerous times?
I am running a program and I can only get the first result to print
You're overwriting $row. Instead use a different variable for the query result.
$query = "SELECT * FROM table WHERE data = '$userinput'";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result))
{
echo $row['data'];
}
Try using mysql_fetch_assoc($row)
You should not use the same variable as the index and the loop variable.
$row = mysql_query
while( $row = ...
Replace the first $row with $result