I have a database (SQL) with the table "Staff" with two records in it. I am required to display the contents of these records on a web page using PHP.
<html>
<head>
<title>CES Staff details</title>
</head>
<body>
**code emitted**
<?php
$row = mysql_fetch_array($result) ;
$looping = 1;
$i = $row.length;
while ($looping <= $i)
{
echo $row["Name"].", Room ".$row["Room"].", Tel ".$row["Telephone"] ;
$looping++;
}
?>
</body>
</html>
How would I change the while loop correctly so that it will display both records on the page.
Thanks!
mysql_fetch_array() only retrieves a single row from the database. You need to call it inside your while loop to retrieve all rows. The $looping increment is unnecessary here, since mysql_fetch_array() returns false when no more rows are available.
while ($row = mysql_fetch_array($result))
{
echo $row["Name"].", Room ".$row["Room"].", Tel ".$row["Telephone"] ;
}
I'll do...
while ($row = mysql_fetch_assoc($result)) {
// print $row;
}
while ($row = mysql_fetch_array($result))
{
echo $row["Name"].", Room ".$row["Room"].", Tel ".$row["Telephone"] ;
}
<?php
$qry = mysql_query($result);
while ($row = mysql_fetch_array($qry))
{
echo $row["Name"].", Room ".$row["Room"].", Tel ".$row["Telephone"] ;
}
?>
PHP has great documentation with examples. Check out the example for mysql_fetch_array().
Your code should look like this:
<?php
while($row = mysql_fetch_array($result)) {
echo $row["Name"].", Room ".$row["Room"].", Tel ".$row["Telephone"] ;
}
?>
Use this
while($row = mysql_fetch_array($result))
{
echo $row["Name"].", Room ".$row["Room"].", Tel ".$row["Telephone"] ;
}
In php there is no such thing like $row.length; the "." is an operator for string concatenation. Read more on mysql_fetch_array at http://php.net/manual/en/function.mysql-fetch-array.php.
<?php
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
echo $row["Name"].", Room ".$row["Room"].", Tel ".$row["Telephone"] ;
}
?>
Related
Trying to create a list by selecting booking references from the database, sorting them into an array and then having each array value passed into each item on the list. e.g. first booking reference on the first item, second reference on the second item etc...
Here is the code.
<html>
<head>
<title>Manage booking</title>
<link rel="stylesheet" type="text/css" href="style1.css">
</head>
<body>
<h3>Oadby Granville Tennis Club Manage Booking</h3>
<br><br><br>
<?php
include "dbconnect.php";
session_start();
$login_ID = $_SESSION['id'];
$query = "SELECT Booking_ID
FROM `booking`
WHERE Login_ID = '$login_ID';";
$bookingArray = array();
$result = $mysqli -> query($query);
while ($row = $result->fetch_assoc()){
printf ("%s<br>", $reference = $row["Booking_ID"]);
array_push($bookingArray, $reference . "<br>");
}
print_r ($bookingArray);
?>
<table border="1" style="width:50%">
<caption>Please select a booking to edit:</caption>
<tr>
<td>
<ul style="list-style-type:circle">
<?php
$arrayNo = 1;
while ($row = $result->fetch_assoc()){
printf ("<li><a href='UpdateBooking.php'>Booking ID:%s</a>" . $bookingArray[$arrayNo] . "</li><br>");
$arrayNo = $arrayNo + 1;
}
?>
<li>Booking ID</li>
</ul>
</td>
</tr>
</body>
</html>
Your first while ($row = $result->fetch_assoc()) {..} transfers everything into $bookingArray until the result set is exhausted. With your second while ($row = $result->fetch_assoc()) {..} you don't get anything more. Additionally, your use of printf is weired.
Try this instead your second loop:
foreach($bookingArray as $Booking)
{
echo "<li><a href='UpdateBooking.php'>Booking ID:$Booking</a></li><br>";
}
However, UpdateBooking.php wont know which booking should be changed. So you should consider to append the BookingId to the URL.
Edit 2016-03-02:
To append the BookingId to the URL you should try:
foreach($bookingArray as $Booking)
{
echo "<li><a href='UpdateBooking.php?BookingId=" . urlencode($Booking) . "'>Booking ID:$Booking</a></li><br>";
}
I have a table which displays information from a database. I added a column where I want to display a message according to row number displayed.
<form name="afisare1" method="POST" >
<input type="submit" name="opt1" value="OK"/>
<?php
if (isset($_POST['opt1'])) {
$loc=mysql_query("SELECT loc FROM program WHERE den='Option'");
//$loc result is a number
$sql=mysql_query("SELECT Col1, Col2
FROM date WHERE Opt_1='Option' OR Opt_2='Option'
ORDER BY Col2 DESC");
?>
<table>
<thead>
<tr>
<th>Col1</th> <th>Col2</th> <th>OK/NOT OK</th>
</tr>
</thead>
<?php
$num_rows = 0;
while ($row = mysql_fetch_assoc($sql)) {
$num_rows++;
if ($num_rows <= $loc) {
echo"<tr>
<td>".$row['Col1']."</td><td >".$row['Col2']."</td><td>OK</td>
</tr>";
break;
}
if ($num_rows > $locuri_buget) {
//here i have a problem because i don't know how to display something like this :
//echo"<tr><td>".$row['Col1']."</td><td >".$row['Col2']."</td><td>NOT OK</td></tr>";
}
}
} ?>
</table>
</form>
For example if the result of $loc=2 i want to echo for the first 2 rows OK and for extra rows i want to echo NOT OK
To retrieve loc from MySQL, your original code does it like this:
$loc = mysql_query("SELECT loc FROM program WHERE den='Option'");
But you might want to change it like this:
$loc = 0; # default value in case of error in a query
$result = mysql_query("SELECT loc FROM program WHERE den='Option'");
while($row = mysql_fetch_assoc($result)){
$loc = $row['loc'];
}
Or if you want retrieve only one record from your databse, you can also write a SQL with LIMIT like this:
$loc = 0; # default value in case of error in a query
$result = mysql_query("SELECT loc FROM program WHERE den='Option' LIMIT 1");
if($row = mysql_fetch_assoc($result)){
$loc = $row['loc'];
}
Hope this helps.
You can try with following inside while loop
if($num_rows<=$loc){
echo"<tr><td>".$row['Col1']."</td><td >".$row['Col2']."</td><td>OK</td></tr>";
}else{
echo"<tr><td>".$row['Col1']."</td><td >".$row['Col2']."</td><td>NOT OK</td></tr>";
}
$num_rows = 0;
while($row = mysql_fetch_assoc($sql)){
$num_rows++;
if($num_rows<=$loc){
echo"<tr><td>".$row['Col1']."</td><td >".$row['Col2']."</td><td>OK</td></tr>";
}else {
echo"<tr><td>".$row['Col1']."</td><td >".$row['Col2']."</td><td>OK</td></tr>";
}
}
I think your problem is with break,
break ends execution of the current for, foreach, while, do-while or switch structure.
Break
I'm trying to get this information out of the database:
$query = "SELECT * FROM station_control WHERE station_no > 0 ORDER BY station_ord";
it then says it expects parameters in these two lines:
$station_result= mysqli_query($query);
$num= mysqli_fetch_array($station_result);
this is what I want to output, basically to pull every station name from the database called station_ord and the names are under station_name:
$i=0;while ($i < $num) {
$station_name1=mysqli_result($station_result,$i,"station1_name");
$station2_name= mysqli_result($station_result, $i,"station2_name");
$station3_name= mysqli_result($station_result, $i,"station3_name");
$station4_name= mysqli_result($station_result, $i,"station4_name");
$station5_name= mysqli_result($station_result, $i,"station5_name");
echo "<b>
$station1_name $station2_name2</b> <br>
$station3_name<br>
$station1_name4_name<br>
$station5_name<hr> <br>";
$i++;
}
I haven't put in the station_name yet as im confused to where and how I configure that.
Any ideas how to help?
Not sure what you try to do, but try
<?php
$station_result= mysqli_query($query);
$records = mysqli_fetch_array($station_result);
foreach ($records as $record) :?>
<b><?php echo $record['station_name']; ?></b><br/>
<?php endforeach; ?>
ref : http://ch1.php.net/manual/en/mysqli.query.php
and http://www.php.net/manual/en/mysqli-result.fetch-array.php for examples
Something like this should work.
$station_result= mysqli_query($query);
$results= mysqli_fetch_array($station_result);
foreach ($results as $row) {
// echo whatever you want here
echo $row['station_name'];
}
I'm trying to display information from a table in my database in a loop, but for certain information, I'm referencing other tables. When I try to get data from other tables, any data following will disappear. here is the code I am using:
`
//Below is the SQL query
$listing = mysql_query("SELECT * FROM Musicians");
//This is displaying the results of the SQL query
while($row = mysql_fetch_array($listing))
{
?>
...html here...
<? echo $row['name']; ?>
<? echo $row['Town']; ?>
<?
$CountyRef = $row['CountyId'];
$county = mysql_query("SELECT * FROM County WHERE CouInt='$CountyRef'");
while($row = mysql_fetch_array($county))
{
echo $row['CouName'];
}
?>
<?php echo $row['instrument']; ?>
<?php echo $row['style']; ?>`
My problem is that everything after the second while loop is not displaying. Anyone have any suggestions?
Thanks
Second loop should say $row2. $row is being overwritten. Both variables should be named different from each other.
You can acomplish that with a one single query:
SELECT *,
(SELECT CouName FROM County WHERE CouInt=mus.CountyId) as Country
FROM Musicians mus;
You final code should looks like:
<?php
$listing = mysql_query("SELECT *,
(SELECT CouName FROM County WHERE CouInt=mus.CountyId) as Country
FROM Musicians mus;");
//This is displaying the results of the SQL query
while($row = mysql_fetch_assoc($listing))
{
echo $row['name'];
echo $row['Town'];
echo $row['Country']; //Thats all folks xD
echo $row['instrument'];
echo $row['style'];
} ?>
Saludos ;)
And that?:
while($row2 = mysql_fetch_array($county)) {
echo $row2['CouName'];
}
I have a code that I have used over and over again before and now it's messing up. All I want to do is list information from the database into the table on the page, but now it will only show one result, instead of all the results it has found.
<table>
<tr><td style="background-color:#009745; color:#FFFFFF"><center><strong>Address Book</strong></center></td></tr>
<tr>
<?php
$getids = mysql_query("SELECT id, first_name, last_name FROM accounts WHERE s1='$id' ORDER BY id DESC", $db);
if (mysql_num_rows($getids) > 0) {
while ($gids = mysql_fetch_array($getids)) {
$ab_id = $gids['id'];
$ab_fn = $gids['first_name'];
$ab_ln = $gids['last_name'];
}
?>
<td><?= $ab_id ?> - <?= $ab_fn . " " . $ab_ln ?></td>
<?php
} else {
?>
<td><center>No Contacts</center></td>
<?php
}
?>
</tr>
</table>
please help me with this.
Thank You for your help :)
I love this site!! I can always get answers when I need them.
I saw two thing wrong
you are using mysql_fetch_array and later you are using string indexes to print the result
print the things in loop it is overriding values and just storing last row
if (mysql_num_rows($getids) > 0) {
while ($gids = mysql_fetch_assoc($getids)) {
$ab_id = $gids['id'];
$ab_fn = $gids['first_name'];
$ab_ln = $gids['last_name'];
echo '<td>'.$ab_id.' -'. $ab_fn.''.$ab_ln.' </td>';
}
In this messy code you're closing the while loop too early:
while ($gids = mysql_fetch_array($getids)) {
$ab_id = $gids['id'];
$ab_fn = $gids['first_name'];
$ab_ln = $gids['last_name'];
}
Only the last retrieved row is used later on. Also, don't use mysql_fetch_array if you're not accessing the numeric indeces of your result. Use mysql_fetch_assoc instead.