I have a table to fetch entries from my database however the table is only outputting the first result that it finds and not the rest of the entries in the database.
$query = "SELECT * FROM aliases where client_id='$userid'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$timesused=$row['num_used'];
$alias=$row['alias'];
$firstused=$row['time_add'];
$firstused = date('d M Y H:i:s', $firstused);
$lastused=$row['time_edit'];
$lastused = date('d M Y H:i:s', $lastused);
echo "<table border=1>";
echo "<tr>";
echo "<th>Alias</th>";
echo "<th>Times Used</th>";
echo "<th>First Used</th>";
echo "<th>Last Used</th>";
echo "</tr>";
echo "<tr align=center>";
echo "<td>$alias</td>";
echo "<td>$timesused</td>";
echo "<td>$firstused</td>";
echo "<td>$lastused</td>";
echo "</tr>";
echo "</table>";
mysql_fetch_assoc function returns one row from the result (you even call the variable you assign its returned value $row). You need to iterate in a loop on all results to get all rows, something like:
while ($row = mysql_fetch_assoc($result)) {
$timesused=$row['num_used'];
$alias=$row['alias'];
$firstused=$row['time_add'];
$firstused = date('d M Y H:i:s', $firstused);
$lastused=$row['time_edit'];
$lastused = date('d M Y H:i:s', $lastused);
echo "<table border=1>";
echo "<tr>";
echo "<th>Alias</th>";
echo "<th>Times Used</th>";
echo "<th>First Used</th>";
echo "<th>Last Used</th>";
echo "</tr>";
echo "<tr align=center>";
echo "<td>$alias</td>";
echo "<td>$timesused</td>";
echo "<td>$firstused</td>";
echo "<td>$lastused</td>";
echo "</tr>";
echo "</table>";
}
Try this:
$query = "SELECT * FROM aliases where client_id='$userid'";
$result = mysqli_query($query);
$timesused=$row['num_used'];
$alias=$row['alias'];
$firstused=$row['time_add'];
$firstused = date('d M Y H:i:s', $firstused);
$lastused=$row['time_edit'];
$lastused = date('d M Y H:i:s', $lastused);
echo "<table border=1>";
echo "<tr>";
echo "<th>Alias</th>";
echo "<th>Times Used</th>";
echo "<th>First Used</th>";
echo "<th>Last Used</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr align=center>";
echo "<td>$alias</td>";
echo "<td>$timesused</td>";
echo "<td>$firstused</td>";
echo "<td>$lastused</td>";
echo "</tr>";
}
echo "</table>";
Just be aware that mysql_* functions are deprecated. Instead, use the MySQLi or PDO_MySQL extensions.
use while loop to display all records form table while (mysql_fetch_assoc($result)
it will display all records.
Related
I need a little script so when I get my data output from my Mysql table and the data I receive is -1 then it should display Expired if it is anything other than -1 it should just display the data.
This is what I currently have. I have tried some stuff but my PHP and MySQL is 0.
$sql = "SELECT ban_id, timestamp, perp_steamid, perp_name, admin_steamid, admin_name, bantime, timeleft, reason FROM nomercyg_CTBan_Log";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>". $row["ban_id"]. "</td><td>". date('m/d/Y h:i:s', $row["timestamp"]). "</td><td>". $row["perp_name"]. "</td><td>". $row["admin_name"]."</td><td>". $row["bantime"]." min". "</td><td>". $row["timeleft"]." min"."</td><td>". $row["reason"]. "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
If I understood you right, you can just add an if statement inside your while loop to decide if you should display 'Expired' or the data you caught itself.
while($row = $result->fetch_assoc()) {
if($row['band_id] == -1) {
echo '<tr><td>Expired</td></tr>; //Or add as many columns as you need here
} else {
echo "<tr><td>". $row["ban_id"]. "</td><td>". date('m/d/Y h:i:s', $row["timestamp"]). "</td><td>". $row["perp_name"]. "</td><td>". $row["admin_name"]."</td><td>". $row["bantime"]." min". "</td><td>". $row["timeleft"]." min"."</td><td>". $row["reason"]. "</td></tr>";
}
}
Maybe like this way but i think its not the best solution
$sql = "SELECT ban_id, timestamp, perp_steamid, perp_name, admin_steamid, admin_name, bantime, timeleft, reason FROM nomercyg_CTBan_Log";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
//echo . . . . . . . .. .. . ... . ;
echo "<tr><td>";
if ($row["ban_id"] <=0){
$row["ban_id"] = 'EXPIRED!!!';
echo $row["ban_id"];
}else {
echo $row["ban_id"];
}
echo "</td><td>";
if(date('m/d/Y h:i:s', $row["timestamp"]) <=0){
$row["ban_id"] = 'EXPIRED!!!';
echo date('m/d/Y h:i:s', $row["timestamp"]);
}else {
echo date('m/d/Y h:i:s', $row["timestamp"]);
}
echo "</td><td>";
if ($row["perp_name"]<=0){
$row["perp_name"] = 'EXPIRED!!!';
echo $row["perp_name"];
}else{
echo $row["perp_name"];
}
echo "</td><td>";
if ($row["admin_name"]<=0){
$row["admin_name"] = 'EXPIRED!!!';
echo $row["admin_name"];
}else{
echo $row["admin_name"];
}
echo "</td><td>";
if ($row["bantime"]<=0){
$row["bantime"] = 'EXPIRED!!!';
echo $row["bantime"];
}else{
echo $row["bantime"];
}
echo " min";
echo "</td><td>";
if ($row["timeleft"] <=0){
$row["timeleft"] = 'EXPIRED!!!';
echo $row["timeleft"];
}else{
echo $row["timeleft"];
}
echo " min";
echo "</td><td>";
if ($row["reason"]<=0){
$row["reason"] = 'EXPIRED!!!';
echo $row["reason"];
}else{
echo $row["reason"];
}
echo "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
I've made a script for a search mysql table, and instead of one table with more rows, he return me on results page one table for each person.
This is my results code by id from the search script. and returns table for each person
$id = $_GET['id'];
$sql = "select * from wp_certificari WHERE id = '{$id}'";
$rst = mysql_query($sql);
while($a_row = mysql_fetch_assoc($rst)) {
echo "<center>\n";
echo "<b>Detalii Certificat:</b>";
echo "<table border='1'>";
echo"<thead>";
echo "<th>Denumire certificare</th>";
echo "<th>Serie si numar certificare</th>";
echo "<th>Data certificarii</th>";
echo "<th>Valabilitate certificare</th>";
echo "<th>Sector Financiar</th></tr>";
echo"</thead>";
echo "<td class='lalign'>{$a_row['nume']}</td>" ;
echo "<td class='lalign'>{$a_row['serie_numar']}</td>" ;
echo "<td class='lalign'>{$a_row['data']}</td>" ;
echo "<td class='lalign'>{$a_row['valabilitate']}</td>" ;
echo "<td class='lalign'>{$a_row['sector_financiar']}</td></tr>" ;
echo"</table>";
echo "</center>\n";}
You need to write your table and table head html outside your while loop Add mysql_num_rows to check empty result and add mysql_real_escape_string to Escapes special characters in a string for use in an SQL statement
$id = $_GET['id'];
$id=mysql_real_escape_string($id);
$sql = "select * from wp_certificari WHERE id = '{$id}'";
$rst = mysql_query($sql);
$row=mysql_num_rows($rst);
if($row>0){
//outside while loop
echo "<center>\n";
echo "<b>Detalii Certificat:</b>";
echo "<table border='1'>";
echo"<thead>";
echo "<th>Denumire certificare</th>";
echo "<th>Serie si numar certificare</th>";
echo "<th>Data certificarii</th>";
echo "<th>Valabilitate certificare</th>";
echo "<th>Sector Financiar</th>";
echo"</thead>";
while($a_row = mysql_fetch_assoc($rst)) {
echo "<tr><td class='lalign'>{$a_row['nume']}</td>" ;
echo "<td class='lalign'>{$a_row['serie_numar']}</td>" ;
echo "<td class='lalign'>{$a_row['data']}</td>" ;
echo "<td class='lalign'>{$a_row['valabilitate']}</td>" ;
echo "<td class='lalign'>{$a_row['sector_financiar']}</td></tr>" ;
}
//outside while loop
echo"</table>";
echo "</center>\n";
}
Note:- Don't use mysql because it is deprecated instead use mysqli or
PDO
To prevent sql injection check this link How can I prevent SQL-injection in PHP?
You have to get table part outside from while loop.
$id = $_GET['id'];
$sql = "select * from wp_certificari WHERE id = '{$id}'";
$rst = mysql_query($sql);
echo "<center>\n";
echo "<b>Detalii Certificat:</b>";
echo "<table border='1'>";
echo "<thead>";
echo "<tr>";
echo "<th>Denumire certificare</th>";
echo "<th>Serie si numar certificare</th>";
echo "<th>Data certificarii</th>";
echo "<th>Valabilitate certificare</th>";
echo "<th>Sector Financiar</th></tr>";
echo "</thead><tbody>";
while($a_row = mysql_fetch_assoc($rst)) {
echo "<tr>";
echo "<td class='lalign'>{$a_row['nume']}</td>" ;
echo "<td class='lalign'>{$a_row['serie_numar']}</td>" ;
echo "<td class='lalign'>{$a_row['data']}</td>" ;
echo "<td class='lalign'>{$a_row['valabilitate']}</td>" ;
echo "<td class='lalign'>{$a_row['sector_financiar']}</td></tr>";
}
echo"</tbody></table>";
echo "</center>\n";
Basically I have 4 fields in a form. I want to the user search for books in a library by either title, or by author or both. I also want the user to set the length of the list of items and from the starting point, these are not restrictions though the user does not have to specify.
Here is the code:
require_once __DIR__.'/config.php';
session_start();
$dbh = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_USERNAME, DB_USERNAME, DB_PASSWORD);
$title = $_GET["title"];
$authors = $_GET["authors"];
$st = $_GET["start"];
$ln = $_GET["length"];
$stmt = $dbh->prepare("SELECT title, authors, description, price FROM books WHERE title = :title LIMIT :length OFFSET :start");
$stmt->execute(array(':title' => $title,':start' => $st,':length' => $ln));
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$title = $row['title'];
$authors = $row['authors'];
$description = $row['description'];
$price = $row['price'];
}
echo "<table>";
echo "<tr>";
echo "<td>Title</td>";
echo "<td>$title</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Authors</td>";
echo "<td>$authors</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Description</td>";
echo "<td>$description</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Price</td>";
echo "<td>$price</td>";
echo "</tr>";
echo "</table>";
So far it literally just returns me what I have typed in the input - so nothing much at all! Does anyone know how I can do this?
Adapt your code this way:
echo "<table>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$title = $row['title'];
$authors = $row['authors'];
$description = $row['description'];
$price = $row['price'];
echo "<tr>";
echo "<td>Title</td>";
echo "<td>$title</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Authors</td>";
echo "<td>$authors</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Description</td>";
echo "<td>$description</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Price</td>";
echo "<td>$price</td>";
echo "</tr>";
}
echo "</table>";
to print all the rows returned by the query. Your code was printing just the last row.
I am trying to execute this query but i got error " Undefined index:
lname".I want to count row from one column(fname) from table a and
select column(lname) from other table b. so please help me.
$result = mysql_query("SELECT COUNT(fname),lname FROM a,b");
while ($row = mysql_fetch_array($result))
{
echo "<tr><td>";
echo $row['lname'];
echo "</td>";
echo "<td>";
echo $row['COUNT(fname)'];
echo "</td></tr>";
}
If you still get an error you can try to fetch both separately:
$result = mysql_query("SELECT COUNT(fname) FROM a");
while ($row = mysql_fetch_array($result))
{
echo "<tr><td>";
echo $row['COUNT(fname)'];
echo "</td></tr>";
}
$result1 = mysql_query("SELECT lname FROM b");
while ($row = mysql_fetch_array($result1))
{
echo "<tr><td>";
echo $row['lname'];
echo "</td></tr>";
}
You need to use an alias. Use this:
$result = mysql_query("SELECT COUNT(fname) AS countfname,lname FROM a,b");
while ($row = mysql_fetch_array($result))
{
echo "<tr><td>";
echo $row['lname'];
echo "</td>";
echo "<td>";
echo $row['countfname'];
echo "</td></tr>";
}
Try this code:
$result = mysql_query("SELECT COUNT(a.fname) as fname,b.lname as lname FROM a,b");
while ($row = mysql_fetch_array($result))
{
echo "<tr><td>";
echo $row['lname'];
echo "</td>";
echo "<td>";
echo $row['COUNT(fname)'];
echo "</td></tr>";
}
I've a html form which is insert data to mysql database and then get those data with following php code (From supplier_jv table)
<?php
include("include/address2.php");
include("include/menu.php");
$uname_ad = $_SESSION['uname_ad'];
$id = $_GET['id'];
$sql = mysql_query("SELECT * FROM supplier_jv WHERE jv_id = '$id'");
$num = mysql_num_rows($sql);
if($num == 0)
{
echo "<p><font color=red>Accounts is emtpy</font></p>";
}
else
{
$re_name = mysql_fetch_array($sql);
echo "<center><h2>";
echo "<strong>Accounts of </strong>";
echo $re_name['jv_name'];
echo "</h2></center>";
echo "<center>";
echo "<table>";
echo "<table border='0' cellpadding='5' cellspacing='5' width='1000'>";
echo "<tr/>";
echo "<td><strong>Date</strong></td>";
echo "<td><strong>Particular</strong></td>";
echo "<td><strong>Folio(C)</strong></td>";
echo "<td><strong>Folio(J)</strong></td>";
echo "<td><strong>Debit</strong></td>";
echo "<td><strong>Credit</strong></td>";
echo "<td><strong>Balance</strong></td>";
echo "</tr>";
while($re= mysql_fetch_array($sql))
{
$day = $re['day'];
$month $re['month'];
$year = $re['year'];
$parti = $re['particulars'];
$folio = $re['folio'];
$folio2 = $re['folio2'];
$debit = $re['debit'];
$credit = $re['credit'];
$balance = $re['balance'];
$b = $debit - $credit;
$total_debit = mysql_query("SELECT SUM(debit) FROM supplier_jv");
$re_t = mysql_fetch_array($total_debit);
$t_d = $re_t['SUM(debit)'];
$total_credit = mysql_query("SELECT SUM(credit) FROM supplier_jv");
$re_t2 = mysql_fetch_array($total_credit);
$t_c = $re_t2['SUM(credit)'];
$b = $t_d - $t_c;
echo "<tr>";
echo "<td>$day/$month/$year</td>";
echo "<td>$parti</td>";
echo "<td>$folio</td>";
echo "<td>$folio2</td>";
echo "<td>";
echo number_format($debit);
echo "</td>";
echo "<td>";
echo number_format($credit);
echo "</td>";
echo "<td></td>";
echo "</tr>";
}
echo "<tr bgcolor='#f3f3f3'>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td><strong>Total Balance-</strong></td>;
echo "<td><strong>";
echo number_format($b);
echo "</strong></td>";
echo "</tr>";
echo "</table>";
echo "</center>";
}
?>
Well, After insert data then it's show:
Notice: Undefined variable: b in E:\xampp\htdocs\Accounts\admin\content
\supplier_account.php on line 108
But if i insert data in second time then it's OK!!.
Any idea or solution.
Thanks
shibbir.
You need to use isset to see if it is set, not null and also avoid the Notice: Undefined variable error:
if (isset($b))
{
// your code here
}
Where you have:
$b = $t_d - $t_c;
Make sure that there is some value coming up for $t_d and $t_c