Display mysqli result multiple times [duplicate] - php

This question already has answers here:
How can I use mysqli_fetch_array() twice?
(4 answers)
Closed 3 months ago.
I asked this recently but it got marked as duplicate and deleted.
Please look at my question before marking it down because it is not the same and I am struggling to figure this out.
I want to echo the 'company' and 'area' results from a MYSQLI query into my page, at separate points in the body of a php page.
Only the first echo will show. Please show my mistake.
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/Connections/****";
include_once($path);
$dbhandle=mysqli_connect($hostname_Demo, $username_Demo, $password_Demo, $database_Demo);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql_RS1="SELECT * from CompanyName where area = 1";
$result=mysqli_query($dbhandle,$sql_RS1);
?>
<head>
<title>TEST</title>
</head>
<body>
<?php
while($row = mysqli_fetch_assoc($result))
{
echo $row['company'];
}
?>
<?php
while($row = mysqli_fetch_assoc($result))
{
echo $row['area'];
}
?>
<?php $dbhandle->close(); ?>
</body>
</html>

You can use this code
<?php
while($row = mysqli_fetch_assoc($result))
{
$data_array[] = $row;
}
foreach ($data_array as $data) {
echo $data['company'];
}
foreach ($data_array as $data) {
echo $data['area'];
}
Do like this with.., You don't want to have to make more queries to your database.

You are fetching same row two different which is not correct.
Something like this should work
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/Connections/****";
include_once($path);
$dbhandle=mysqli_connect($hostname_Demo, $username_Demo, $password_Demo, $database_Demo);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql_RS1="SELECT * from CompanyName where area = 1";
$result=mysqli_query($dbhandle,$sql_RS1);
$row =array();
while ($x= mysqli_fetch_assoc($result))
$row[]=$x;
$i=0;
?>
<head>
<title>TEST</title>
</head>
<body>
<?php
echo $row[i]['company'];
?>
<?php
echo $row[i]['area'];
$i++;
?>

try merging them it looks like they are just the same
<?php
while($row = mysqli_fetch_assoc($result))
{
echo $row['company']."
".$row['area'];
}
?>

This is what I ended with. It works so I hope it is correct. Thanks for everyones help.
$sql_RS1="SELECT * from CompanyName where area = 1";
$result=mysqli_query($dbhandle,$sql_RS1);
$row =array();
while ($x= mysqli_fetch_assoc($result))
$row_RS1=$x;
?>
<?php
$sql_RS2="SELECT * from employees where EmployeeID = 79";
$result=mysqli_query($dbhandle,$sql_RS2);
$row_RS2 =array();
while ($x= mysqli_fetch_assoc($result))
$row_RS2=$x;
?>
<head>
<title>TEST</title>
</head>
<body>
<p>
<?php echo $row_RS1['company']; ?>
</p>
<p>
<?php echo $row_RS1['company']; ?>
</p>
<p>
<?php echo $row_RS1['area']; ?>
</p>
<p>
<?php echo $row_RS2['EmployeeID']; ?>
</p>
<p>
<?php echo $row_RS2['Surname']; ?>
</p>
<p>
<?php echo $row_RS2['EmployeeID']; ?>
</p>
<p>
<?php $dbhandle->close(); ?>
</p>
</body>

Related

How to echo a list into a textarea?

I'm trying to echo a list into a textarea (to allow for simple copy and paste), I've got the list echoing out as flat html and its appearing as it should but I'd like it to be inside a textarea, here's what I've done so far...
<?php
include (ABSPATH.'/connect_include.php');
$AvDates = "SELECT * FROM DB_Available_Dates";
$AvDates = mysql_query($AvDates) or die(mysql_error());
if(mysql_num_rows($AvDates) > 0){
?>
<input value=
<ul>
<?php
while ($row_AvDates = mysql_fetch_assoc($AvDates)){
?>
<li><?php echo htmlentities($row_AvDates['Month']).' - '. htmlentities ($row_AvDates['the_days']); ?></li>
<?php
}
?>
</ul> />
<?php
}
?>
This isn't working though - how can I make this work?
I guess this might help you:
<textarea>
<?php
$anyVar = "This\nis\na\nstring";
echo $anyVar;
?>
</textarea>
Update: Demo for your example
<?php
include (ABSPATH.'/connect_include.php');
$AvDates="SELECT * FROM DB_Available_Dates";
$AvDates=mysql_query($AvDates) or die(mysql_error());
if(mysql_num_rows($AvDates)>0)
{
?>
<textarea>
<?php
while ($row_AvDates=mysql_fetch_assoc($AvDates))
{
echo htmlentities($row_AvDates['Month']) . " - " . htmlentities ($row_AvDates['the_days']) . "\n";
}
?>
</textarea>
<?php
}
?>

Foreach for database

So I have the following code:
<?php
$post_id = 1;
$name_result = $wpdb->get_results("SELECT * FROM rh_names WHERE post_id = '$post_id' ");
?>
<?php if (!empty($name_result)) { ?>
<?php foreach ($name_result as $names) {?>
<div class="names">
<script type="text/javascript">
var nameXML = <?php echo json_encode($names); ?>;
document.write(nameXML.last_name);
</script>
</div>
<?php } ?>
<?php } ?>
In the database, I have two results that should show up, but I am only getting one.
Why am I only getting one result when two should show up? What am I missing?
Thanks!
document.write will replace all document element so you can see last result only try
document.getElementById('results').innerHTML = res_content;

How do I loop through 2 columns?

I've got a table called tickets. The Tickets table has 4 columns: user_id, ticket_id, subject, message. I want to loop the ticket_id and subject of the logged in user. I want to echo it with foreach, but I cant get it to work. This is what I've got:
Query:
$showTheTickets = array();
$user_id = $_SESSION['user_id'];
$getTickets = mysqli_query($mysqli,"SELECT * FROM tickets WHERE user_id = '$user_id'") OR die (mysqli_error($mysqli));
while($row = mysqli_fetch_assoc($getTickets)){
$showTicketID = $row['ticket_id'];
$showTicketSubject = $row['subject'];
}
Code in the page:
<?php foreach ($showTheTickets as $showTheTickets): ?>
<div class="preview">
<?php echo $showTicketID ?>
<?php echo $showTicketSubject ?>
</div>
<?php endforeach; ?>
Anyones willing to help me? Thanks.
Try this:
$showTheTickets = array();
$user_id = $_SESSION['user_id'];
$getTickets = mysqli_query($mysqli,"SELECT * FROM tickets WHERE user_id = $user_id") OR die (mysqli_error($mysqli));
while($row = mysqli_fetch_array($getTickets)){
$row = array( 'ticket_id' => $row['ticket_id'], 'subject' => $row['subject'] );
$showTheTickets[] = $row;
}
And then:
<?php foreach ($showTheTickets as $stt): ?>
<div class="preview">
<?php echo $stt['ticket_id'] ?>
<?php echo $stt['subject'] ?>
</div>
<?php endforeach; ?>
Hope it helps...
You are overwriting the values you get from the query and your $showTheTickets array remains empty.
You probably want something like:
$showTheTickets = array();
while($row = mysqli_fetch_assoc($getTickets)){
$showTheTickets[$row['ticket_id']] = $row['subject'];
}
and:
<?php foreach ($showTheTickets as $id => $subject): ?>
<div class="preview">
<?php echo $id; ?>
<?php echo $subject; ?>
</div>
<?php endforeach; ?>
Why not simply
while($row = mysqli_fetch_assoc($getTickets)){
?><div class="preview"><?
echo $row['ticket_id'];
echo $row['subject'];
?></div><?
}
you have to save $showTicketID and $showTicketSubject in a array (could be the same array or 2 arrays) and then in the view show the arrays, like this:
while($row = mysqli_fetch_assoc($getTickets)){
$array[$row['ticket_id']] = $row['subject'];
}
and the view:
<?php foreach ($showTheTickets as $ticketid => $ticketsubject): ?>
<div class="preview">
<?php echo $ticketid ?>
<?php echo $ticketsubject ?>
</div>
<?php endforeach; ?>

Make a foreach array from mysql SELECT

I have the following code:
$result = mysqli_query($con, "SELECT * FROM da_questions");
while($row = mysqli_fetch_array($result))
{
}
mysqli_close($con);
which selects everything from the table da_questions. Now this is in a file called config.php which file is required in all my pages of my website. Now I want to do the following on my external pages... let's say index.php
<?php foreach($results as $question): ?>
<p><?php echo $question['question_title']; ?></p>
<br />
<p><?php echo $question['question_text']; ?></p>
<br />
<?php endforeach; ?>
My file outlay is like shown below:
index.php
config/
config.php
How can I do this?
Why not just
$results = array(); while($row = mysqli_fetch_array($reult)) { $results[] = $row; }
or as #Phil pointed out:
$results = $result->fetch_all(MYSQLI_ASSOC)
With your code
$result = mysqli_query($con, "SELECT * FROM da_questions");
$results = array();
while($row = mysqli_fetch_array($result))
{
$results[] = $row;
}
mysqli_close($con);
in the index.php write as below
include('config/config.php');
<?php foreach($results as $question): ?>
<p><?php echo $question['question_title']; ?></p>
<br />
<p><?php echo $question['question_text']; ?></p>
<br />
<?php endforeach; ?>

when no rows retrieved from SQL database then dont display a div

i tried the empty selector in jquery but it doesnt work. the content is still being displayed. i am retrieving some rows from the SQL database.. if there is no database then i dont want to display that div.
<div id="scrollingText">
<div class="scrollWrapper">
<div class="scrollableArea">
<marquee behavior="scroll" direction="left">
<p>
<?php
$con = mysql_connect("localhost","fraptech_test","");
mysql_select_db("fraptech_test", $con);
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysql_select_db("fraptech_ndsnotice", $con);
$result = mysql_query("SELECT * FROM ndsnotice");
while($row = mysql_fetch_array($result))
{
echo $row['Notice'];
}
?>
</p>
</marquee>
</div>
</div>
</div>
Is that your whole code? Are you working with ajax? If you're using pure PHP without ajax just set the output div into your "if"-conditions?
If you want to use jQuery, try:
if ( ($("div.scrollableArea p").text()).length > 0 )
{
$("div.scrollableArea p").show();
}
else
{
$("div.scrollableArea p").hide();
}
But you can do it without jQuery, I guess.
You need to put the HTML inside your php if clause.
Simple example:
<div id="scrollingText">
<?php
$con = mysql_connect("localhost","fraptech_test","");
mysql_select_db("fraptech_ndsnotice", $con);
$result = mysql_query("SELECT * FROM ndsnotice");
if (mysqli_connect_errno($con)) {
<div class="scrollWrapper">
<div class="scrollableArea">
<marquee behavior="scroll" direction="left">
<p>
<?php
while($row = mysql_fetch_array($result))
{
echo $row['Notice'];
}
?>
</p>
</marquee>
</div>
</div>
<?php } else { ?>
<div class="errordiv">Display this on error</div>
<?php } ?>
</div>
thank you guys with ur help i made some change. the below code did it:
<?php
$con = mysql_connect("localhost","fraptech_test","");
mysql_select_db("fraptech_test", $con);
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysql_select_db("fraptech_ndsnotice", $con);
$result = mysql_query("SELECT * FROM ndsnotice");
if (mysql_num_rows($result) > 0)
{
?><div id="scrollingText">
<div class="scrollWrapper">
<div class="scrollableArea">
<marquee behavior="scroll" direction="left">
<p>
<?php while($row = mysql_fetch_array($result))
{
echo $row['Notice'];
}
?> </p>
</marquee>
</div>
</div>
</div>
<?php } ?>

Categories