I'm trying to fetch all rows using this below coding. Here you can see echo $row['UserName']; and echo $row['Address']; i can get all the username and address from database. But when i use this coding ($uname = $row['UserName'];echo $uname; and $uaddress = $row['Address'];echo $uaddress;) outside of while loop it always fetch the last row values. My question is how to fetch all row values outside of while loop?
<?php
include('config.php');
try
{
$stmt = $conn->prepare('SELECT * FROM ebusers');
$conn->errorInfo();
$stmt->execute();
while($row = $stmt->fetch())
{
echo "<a target=_blank href='edit.php?id=". $row['UserID'] ."'>Edit</a>";
echo "<br>";
echo "<a target=_blank href='delete.php?id=". $row['UserID'] ."'>Delete</a>";
echo $row['UserName'];
$uname = $row['UserName'];
echo "<br>";
echo $row['Address'];
$uaddress = $row['Address'];
echo "<br>";
}
}
catch(PDOException $e)
{
'Error : '.$e->getMesssage();
}
echo $uname;
echo $uaddress;
?>
Just pass them into an array
while($row = $stmt->fetch())
{
echo "<a target=_blank href='edit.php?id=". $row['UserID'] ."'>Edit</a>";
echo "<br>";
echo "<a target=_blank href='delete.php?id=". $row['UserID'] ."'>Delete</a>";
echo $row['UserName'];
$uname[] = $row['UserName']; // An array of usernames...
echo "<br>";
echo $row['Address'];
$uaddress[] = $row['Address']; // An another array of addresses
echo "<br>";
}
//Below two statements prints all usernames and addresses
print_r($uname); // or you can access like echo $uname[2] to view the second username
print_r($uaddress); // similarly you can do like that as you did for $uname..
//or if you want to loop through individually , you can make use of a foreach construct
foreach($uname as $usernames)
{
echo $usernames."<br>";
}
During the while loop, the $row variable get overwritten each time it executed.
You may have a try on FetchAll which prepares the array for you.
Related
apparently i have issue with displaying the image as it only show me the name of the file, is there anyway for it to display on the webpage right away?
Below is my source code along with 2screenshots of my database holding the image and the place where i want it to display at:
<?php
error_reporting(E_ERROR);
include("global.php");
session_start();
$receipt = $_GET['receipt'];
$userid = $_SESSION ['userid'];
if (isset($_SESSION['userid']) == false)
{
header ("Location: login.php");
}
$mysqli = new mysqli(spf, dbuser, dbpw, db);
$stmt = $mysqli->prepare("SELECT receipt, date, time, pick, dropoff, userid, carno, cost, branch, area, image FROM items where receipt=?");
$stmt->bind_param("s", $receipt);
$stmt->execute();
$stmt->bind_result($receipt, $date, $time, $pick, $dropoff, $userid, $carno, $cost, $branch, $area, $image);
while ($stmt->fetch()) {
echo "<table border='1' style='width:40%'>";
echo "<td>";
echo "<b>Receipt ID: $receipt</b>";
echo "<br><br>";
echo "$image";
echo "<br><br>";
echo "<b>Date of Travel: $date</b>";
echo "<br><br>";
echo "<b>Time of Travel: $time</b>";
echo "<br><br>";
echo "<b>Pick Up Location: $pick</b>";
echo "<br><br>";
echo "<b>Drop Off Location: $dropoff</b>";
echo "<br><br>";
echo "<b>Area of DropOff: $area</b>";
echo "<br><br>";
echo "<b>Cost of Trip: $cost</b>";
echo "<br><br>";
echo "<b>User ID (NRIC): $userid</b>";
echo "<br><br>";
echo "<b>Branch of Officer: $branch</b>";
echo "</td>";
}
echo "</table>";
$stmt->close();
$mysqli->close();
?>
Preview:
Use <img> tag as below
echo "<img src=$image>"
Try this:
echo "<img src='$image'>";
Learn about img
I have been trying to work this out for a while.
I have created a form with a dropdown box that gets results from a database. from this i then $_POST that from to another page. From that second page i wish to get the ID number and then get the records and display them on screen.
I will then put them in a table to organise the results better.
can anyone help me in achieving this.
Here is the code for the form (which works and sends the $PlantID)
$sql = "SELECT DISTINCT * FROM PLANTS";
$result = mysqli_query($mysqli,$sql)or die(mysqli_error());
//********************* Botannical name drop down box
echo "<form name='selection' id='selection' action='profile.php' method='post'>";
echo "<select name='flower'>";
while($row = mysqli_fetch_array($result)) {
$plantid = $row['FlowerID'];
$plantname = $row['Botannical_Name'];
$plantcommon = $row['Common_Name'];
/* $plantheight = $row['Height'];
$plantav = $row['AV'];
$plantcolours = $row['Colours'];
$plantflowering = $row['Flower_Time'];
$plantspecial = $row['Special_Conditions'];
$plantfrost = $row['Frost_Hardy'];
$plantaspect = $row['Aspect'];
$plantspeed = $row['Growth_Speed'];*/
echo "<option value=".$plantid.">".$plantname." -> AKA -> ".$plantcommon."</option>";
}
echo "</select>";
echo "<br />";
//********************* End of form
echo "<input type='submit' name='submit' value='Submit'/>";
echo "</form>";
I have created this page to get the ID and display that ID on screen. AS you can tell i have probably doubled up on ways to try work this out.
$sql = "SELECT * FROM PLANTS";
$result = mysqli_query($mysqli,$sql)or die(mysqli_error());
if(isset($_POST['submit'])){
$selected_val = $_POST['Botannical_Name']; // Storing Selected Value In Variable
echo "You have selected :" .$selected_val; // Displaying Selected Value
}
echo "<br />";
echo "well:".$_POST["Botannical_Name"]."<br/>";
echo "now:".$plantquery."<br />";
echo $_POST;
echo "<table>";
foreach ($_POST as $key => $value) {
echo "<tr>";
echo "<td>";
echo $key;
echo "</td>";
echo "<td>";
echo $value;
echo "</td>";
echo "</tr>";
}
echo "</table>";
Any help would be greatly appreciated.
you should use following to get the selected value,
$selected_val = $_POST['flower'];
if(isset($_POST['submit'])){
$selected_val = $_POST['flower']; // Storing Selected Value In Variable
echo "You have selected :" .$selected_val; // Displaying Selected Value
$sql = "SELECT * FROM PLANTS WHERE FlowerID='.$selected_val.'";
$result = mysqli_query($mysqli,$sql)or die(mysqli_error());
while ($row=mysqli_fetch_assoc($result))
{
echo $row['Botannical_Name'];
}
}
echo "<br />";
print_r($_POST);
if(!empty(_POST)) {
echo "<table>";
foreach ($_POST as $key => $value) {
echo "<tr>";
echo "<td>";
echo $key;
echo "</td>";
echo "<td>";
echo $value;
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
I am currently working on a PHP and SQLite application. It does have HTML for the webpages being created. I am currently using PDO to connect the database to my online server. When I get to the webpage that allows me to type in what I am searching for then it will display what I have found in the echo statements below. I want to be able to have just the item name, that acts as a hyperlink; when it is clicked on it will go to another webpage (I believe) that will display the item's name, amount, and a short description. Is there a way to use PHP for this action or should I go with the HTML tagging?
if($_POST && isset($_POST['search'])) {
echo "<br>\n";
$query = $mysql->prepare('SELECT * FROM Items WHERE Name = :partname');
$subst = array ('partname' => $_POST['search']);
$query->execute($subst);
echo "<TABLE>";
echo "<tr>";
echo "<td>Name</td>";
echo "<td>Amount</td>";
echo "<td>Detail</td>";
echo "</tr>";
while ($row = $query->fetch()) {
//print_r($row);
echo "<tr>";
echo "<td>$row[Name]</td>";
echo "<td>$row[Amount]</td>";
echo "<td>$row[Detail]</td>";
echo "</tr>";
}
echo "</TABLE>";
} else echo "Item searched for was not found.";
from what i understand
if($_POST && isset($_POST['search'])) {
echo "<br>\n";
$query = $mysql->prepare('SELECT * FROM Items WHERE Name = :partname');
$subst = array ('partname' => $_POST['search']);
$query->execute($subst);
echo "<TABLE>";
echo "<tr>";
echo "<td>Name</td>";
echo "</tr>";
while ($row = $query->fetch()) {
//print_r($row);
$name = $row['Name'];
echo "<tr>";
echo "<td><a href='details.php?name={$name}'>{$name}</a></td>";
echo "</tr>";
}
echo "</TABLE>";
} else echo "Item searched for was not found.";
To use Associative Arrays within double quotes, you need to use curly braces:
echo "<td>{$row['Name']}</td><td>{$row['Amount']}</td><td>{$row['Detail']}</td>";
I am really confused, a script of mine works perfectly in wamp but when i try to run it in my ubuntu apache2 server i get a blank page as output, the same thing happens when i uploaded it to a webserver and executed the script.
I could pinpoint that the error is caused due to the presence of this line of code
while ($row = mysql_fetch_assoc($result))
even if i comment it out the problem persists, only deleting this line will allow me to get any kind of output. Even a simple echo wont work if this line is present.
I am posting the whole script, below may be you people can help me understand the reason behind this strange behavior.
<?php
session_start(); // start up the PHP session!
//Connect to mysql
include 'conn.php';
// Table name
$tbl_name = "tailor.employee";
//Get and initialize variables
$op = $_POST['op'];
$cusid = $_POST['cusid'];
if($op=="2")
{
$sql = "SELECT * FROM tailor.order WHERE cusid ='$cusid'";
$result = mysql_query($sql);
// Mysql_num_row is counting table row
$count = mysql_num_rows($result);
if($count!=0)
{
$order = array();
$oid = array();
$day = array();
$month = array();
$year = array();
$quan = array();
$i=0;
while ($row = mysql_fetch_assoc($result)) {
$cusid = $row['cusid'];
$cname = $row['cname'];
$phone = $row['phone'];
$order[$i] = $row['type'];
$oid[$i] = $row['oid'];
$day[$i] = $row['day'];
$month[$i] = $row['month'];
$year[$i] = $row['year'];
$quan[$i] = $row['quan'];
$i=$i+1;
}
echo "<br><br><br><br><br><br>";
echo "<pre><p><strong>Customer ID : </strong>".$cusid."</pre></p>";
echo "<pre><p><strong>Customer Name : </strong>".$cname."</pre></p>";
echo "<pre><p><strong>Phone Number : </strong>".$phone."</pre></p>";
echo '<table border="1">';
echo "<tr>";
echo "<td>Quantity</td>";
echo "<td>Order ID</td>";
echo "<td>Particulars</td>";
echo "<td>Delivery Date</td>";
echo "</tr>";
echo "<tr><td>";
$icount = count($oid);
for($k=0;$k<$icount;$k++)
{
echo $quan[$k];
echo "<br>";
}
echo "</td>";
echo "<td>";
for($k=0;$k<$icount;$k++)
{
echo $oid[$k];
echo "<br>";
}
echo "</td>";
echo "<td>";
for($j=0;$j<$icount;$j++)
{
echo $order[$j];
echo "<br>";
}
echo "</td>";
echo "<td>";
for($l=0;$l<$icount;$l++)
{
echo $day[$l]."/".$month[$l]."/".$year[$l];
echo "<br>";
}
echo "</td>";
echo "</tr>";
echo "</table>";
echo '<br><br><p ALIGN = "center">Place New Order</p>';
}
unset($_SESSION['cusid']);
unset($_SESSION['oid']);
unset($_SESSION['type']);
unset($_SESSION['cname']);
unset($_SESSION['phone']);
unset($_SESSION['address']);
}
if($op=="1")
{
header('location:order1.html');
}
?>
The app is live and can be accessed by visiting http://www.techb.wilips.com/login.html
The login user name is modern and password is modern#123
To view the error page, just login, add an order and follow the steps then on the 3rd step where you have the Operation drop down menu, select Print Order
I have this simple bit of PHP that should be showing a table of invites:
// connect to the database
$host = '###';
$username = '###';
$pass = '###';
mysql_connect($host,$username,$pass);
mysql_select_db("###");
// select everything from the news table
$query = "SELECT * FROM creathive_applications";
$result = mysql_query($query);
echo "<table>";
echo "<tr>";
while( ($row = mysql_fetch_array($result)))
{
echo "<td>".$row['firstname']."</td>";
echo "<td>".$row['lastname']."</td>";
echo "<td>".$row['email']."</td>";
echo "<td>".$row['url']."</td>";
}
echo "</tr>";
echo "</table>";
// disconnect from the database
mysql_close();
However it's not working? Any ideas why and how to find out. THANKS :)
Just to confirm, the <table> and <tr> are being outputted and the table name is creathive_applications with a H
Did your query work? You're not checking if $result is false after the query call. The table name sort of appears to have typo in it, possibly it should be "creative_applications" (no h)?
Okay just realised that the damn table was empty! :(
Sorry for wasting people's time!
You should try something less dependant of your database structure :
For example :
echo '<pre>';
print_r($result);
echo '</pre>';
and check if the result is null, or return a completely different set of rows
I think your problem is its not showing up as expected in a 1 row per record layout.
That's because you have your <TR></TR> tags outside your loop through the records.
That section of code should look like the following
while( ($row = mysql_fetch_array($result)))
{
echo "<tr>";
echo "<td>".$row['firstname']."</td>";
echo "<td>".$row['lastname']."</td>";
echo "<td>".$row['email']."</td>";
echo "<td>".$row['url']."</td>";
echo "</tr>";
}
<table>
<?php
$host = '###';
$username = '###';
$pass = '###';
$con = mysql_connect($host,$username,$pass);
$db = '###';
mysql_select_db($db,$con);
$query = "SELECT * FROM creathive_applications";
$querycon = mysql_query($query,$con);
while($row = mysql_fetch_row($querycon)){
echo "<tr>"
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "</tr>"
}
?>
</table>
use this this will work