I have this script here that shows a list of results. How to do l say "No results" if no results are found. I believe it's the else statement but couldn't quiet get it to work.
<?php
$result = mysql_query("SELECT * FROM emailquotes order by id desc")
or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
echo "<tr height='25px' valign='center'>";
echo '<td valign="middle"><p><img src="../../Images/Icons/table-delete.png"/></p></td>';
echo '<td><p>' . $row['ssp'] . '</p></td>';
echo '<td><p>' . $row['ssp'] . '#someonewhere.com</p></td>';
echo '<td><p>' . $row['surname'] . '</p></td>';
echo '<td><p>Car</p></td>';
echo '<td><p>Show Prices</p></td>';
echo "</tr>";
}
?>
You can try with mysql_num_rows function:
$count = mysql_num_rows($result);
if ($count > 0) {
// loop rows
} else {
// no result
}
Put your while loop in a if-statement and check if there is any results before running the loop. Then you echo "No results" in the else.
It should be like this:
<?php
$result = mysql_query("SELECT * FROM emailquotes order by id desc")
or die(mysql_error());
if( mysql_num_rows($result)) {
while($row = mysql_fetch_array( $result )) {
echo "<tr height='25px' valign='center'>";
echo '<td valign="middle"><p><a href="delete.php?id=' . $row['id'] . '"><img
src="../../Images/Icons/table-delete.png"/></a></p></td>';
echo '<td><p>' . $row['ssp'] . '</p></td>';
echo '<td><p>' . $row['ssp'] . '#someonewhere.com</p></td>';
echo '<td><p>' . $row['surname'] . '</p></td>';
echo '<td><p>Car</p></td>';
echo '<td><p>Show Prices</p></td>';
echo "</tr>";
}
}
else {
echo "No Result";
}
?>
Related
I would like to retrieve my results from my DB in this format using Bootstrap.
Below is my PHP code that I'm currently using, the first entry I want the image to be bigger then the rest.
<?php
$article = mysqli_connect("localhost", "root", "", "blog");
// Check connection
if($article === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt select query execution
$sql = "SELECT * FROM news";
if($result = mysqli_query($article, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<container>";
echo "<row>";
echo "<th>id</th>";
echo "<th>title</th>";
echo "<th>body</th>";
echo "<th>image</th>";
echo "</div>";
while($row = mysqli_fetch_array($result)){
echo "<row>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['body'] . "</td>";
echo "<td>" . $row['image'] . "</td>";
echo "</row>";
}
echo "</div>";
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($article);
}
// Close connection
mysqli_close($article);
?>
I think you should have the condition to check for the first data in your loop to apply a bigger image size.
You can simply add condition:
$resultNum = 1;
while($row = mysqli_fetch_array($result)){
if($resultNum == 1) {
// TODO: show bigger image
} else {
// usual image
echo "<row>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['body'] . "</td>";
echo "<td>" . $row['image'] . "</td>";
echo "</row>";
}
$resultNum++;
}
Just make a counter and then do a condition using the first number of the counter
Your code simplyfied:
$query = mysqli_query($article, "SELECT * FROM news");
$n = 1; //start the counter
if(mysqli_num_rows($query) > 0){ //detect if have rows
foreach ($query as $key => $value) {
if($n == 1){
//print the big image
echo $value["id"];
}else{
//print the little image
echo $value["id"];
}
$n++;
}
}else{
echo "No data founded";
}
I wrote this code to retrieve some rows form database
session_start();
$con = mysqli_connect('localhost', 'root', '');
if(!$con)
{
die("not ok");
}
mysqli_select_db($con,"uoh");
$q = " SELECT * FROM student WHERE id = " . $_SESSION['user_id'] ." and password = " . $_SESSION['user_pass'];
$result = mysqli_query($con , $q ) ;
if($row = mysqli_fetch_array($result))
{
echo "this academic transcripts for " . $row["name"];
echo " and the id is " . $row["id"];
}
$q1 = " SELECT student_record.course,student_record.grade,student_record.term,coe_courses.crd
FROM student_record INNER JOIN coe_courses ON student_record.course_number = coe_courses.course_number
where student_record.id = ".$_SESSION['user_id'] ;
$result = mysqli_query($con , $q1 ) ;
if($row = mysqli_fetch_array($result))
{
echo "<br />";
echo "<table border=\"1\" style=\"width:500\">";
echo "<tr>";
echo "<th>coe_courses</th>";
echo "<th>terms</th>";
echo "<th>Grades</th>";
echo "<th>CRD</th>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row["course"]. "</td>";
echo "<td>" . $row["term"]. "</td>";
echo "<td>" . $row["grade"]. "</td>";
echo "<td>" . $row["crd"]. "</td>";
echo "</tr>";
echo "</table>";
}
The problem is that only shows the first row while I have three rows in phpMyAdmin.
enter image description here
You need to call fetch_* repeatedly to retrieve all rows from your result set; each time you call it it retrieves the next row in the result set.
In your sample code above, you would replace
if ($row = mysqli_fetch_array($result))
{
with
while ($row = mysqli_fetch_array($result))
{
This will loop until fetch_array tries to read beyond the last record in $result, at which point fetch_array returns false and the loop exits.
I'm trying to make a pagination, i've got next code but i cant get it working.
<?php
$link = mysqli_connect("localhost", "", " ", "");
if($link === false) { die('<span class="rosu">EROARE:</span> Nu s-a putut realiza conexiunea la baza de date.<br/><br/>Va rog verificati conexiunea pentru baza de date.<br/>' . mysqli_connect_error()); }
$num_rec_per_page=10;
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $num_rec_per_page;
$sql = "SELECT * FROM My_Contracte WHERE contract_sters='nu' ORDER BY id_contract DESC LIMIT $start_from, $num_rec_per_page";
$rs_result = mysql_query ($sql); //run the query
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo '<table id="contracte">';
echo "<tr>";
echo "<th>EDT</th>";
echo "<th>Numar Contract</th>";
echo "<th>C</th>";
echo "<th>Data Realizare</th>";
echo "<th>Nume Firma</th>";
echo "<th>Nume Locatie</th>";
echo "<th>Zona Judet</th>";
echo "<th>Servicii</th>";
echo "<th>Suma Plata</th>";
echo "<th>PER. CTR.</th>";
echo "<th>Nume Contact</th>";
echo "<th>Telefon</th>";
echo "<th>E</th>";
echo "<th>T</th>";
echo "<th>W</th>";
echo "<th>DEL</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td class='editbutton'><a href='modifica.php?ID=" . $row['id_contract'] . "'><img src='images/edit.png'></img></a></td>";
echo '<td>' . $row['numar_contract'] . ' <img src="images/link.png" width="14px"></img></td>';
echo "<td><a href='pdf/" . $row['link_contract_pdf'] . "' target='_blank'><img src='images/pdf.png' width='20px'></img></a></td>";
echo "<td>" . $row['expirare_contract'] . "</td>";
echo "<td>" . $row['nume_firma'] . "</td>";
echo "<td>" . $row['nume_locatie'] . "</td>";
echo "<td>" . $row['zona_judet_oras'] . "</td>";
echo "<td>" . $row['servicii_oferite'] . "</td>";
echo "<td>" . $row['suma_de_plata'] . "</td>";
echo "<td>" . $row['perioada_contract'] . "</td>";
echo "<td>" . $row['nume_contact'] . "</td>";
echo "<td>" . $row['telefon_contact'] . "</td>";
echo '<td><img src="images/email.png"></img></td>';
echo '<td><img src="images/turvirtual.png"></img></td>';
echo '<td><img src="images/website.png"></img></td>';
echo "<td class='deletebutton'><img src='images/delete.png'></img></td>";
echo "</tr>";
}
echo "</table>";
mysqli_free_result($result);
} else { print ('<span class="rosu">EROARE!</span> Nu am gasit contracte in baza de date.'); }
} else { print ('<span class="rosu">EROARE!</span> Nu s-a putut executa comada de listare a contractelor.<br/><br/><b>Motivul pentru care nu s-a putut accesa tabelul: </b>') . mysqli_error($link); print '.'; }
?>
<?php
$sql = "SELECT * FROM My_Contracte WHERE contract_sters='nu'";
$total_records = mysql_num_rows($sql); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
echo "<a href='lista.php?page=1'><div class='pagina'>1</div></a> "; // Goto 1st page
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='lista.php?page=".$i."'><div class='pagina'>".$i."</div></a> ";
};
echo "<a href='lista.php?page=$total_pages'><div class='pagina'>$total_pages</div></a> "; // Goto last page
?>
The problem is that i get it paginated but it dosnt show right links. so i think the problem is in the next code:
<?php
$sql = "SELECT * FROM My_Contracte WHERE contract_sters='nu'";
$total_records = mysql_num_rows($sql); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
echo "<a href='lista.php?page=1'><div class='pagina'>1</div></a> "; // Goto 1st page
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='lista.php?page=".$i."'><div class='pagina'>".$i."</div></a> ";
};
echo "<a href='lista.php?page=$total_pages'><div class='pagina'>$total_pages</div></a> "; // Goto last page
?>
Thank you!
=
EDIT:
[02-Oct-2015 12:18:57 Europe/Bucharest] PHP Warning: mysql_num_rows() expects parameter 1 to be resource, string given in /lista.php on line 81
I get this error in error_log.
81 line is:
$total_records = mysql_num_rows($sql);
Try this i guess you already put link for first and last page and again you are doing that in loop.
for ($i=2; $i<$total_pages; $i++) {
echo "<a href='lista.php?page=".$i."'><div class='pagina'>".$i."</div> </a> ";
};
I got it working like this:
<?php
include("db.php");
$sql = mysql_query("SELECT COUNT(id_contract) FROM My_Contracte WHERE contract_sters = 'nu'");
$total_records = mysql_result($sql, 0);
$total_pages = ceil($total_records / $num_rec_per_page);
echo "<a href='lista.php?page=1'><div class='pagina'>1</div></a> ";
for ($i=2; $i<=$total_pages; $i++) {
echo "<a href='lista.php?page=".$i."'><div class='pagina'>".$i."</div></a> ";
};
?>
How do I align and put div tags around the displayed data? I am very new to php and hope you guys can help!
Thanks!
James
<?php
$conn = mysql_connect("", "", "");
if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}
{
$search = "%" . $_POST["search"] . "%";
$searchterm = "%" . $_POST["searchterm"] . "%";
}
if (!mysql_select_db("")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}
$sql = "SELECT name,lastname,email
FROM test_mysql
WHERE name LIKE '$search%' AND lastname LIKE '$searchterm'";
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
while ($row = mysql_fetch_assoc($result)) {
echo $row["name"];
echo $row["lastname"];
echo $row["email"];
}
mysql_free_result($result);
?>
<?php echo $row["name"];?>
<br>
<?php echo $row["lastname"];?>
<br>
<?php echo $row["email"];?>
I think you mean something like this:
while ($row = mysql_fetch_assoc($result)) {
echo '<div class="data">';
echo '<label>' . $row["name"] . '</label>';
echo '<label>' . $row["lastname"] . '</label>';
echo '<label>' . $row["email"] . '</label>';
echo '</div>';
}
Anyway I don't understand the need to have this:
<?php echo $row["name"];?>
<br>
<?php echo $row["lastname"];?>
<br>
<?php echo $row["email"];?>
which will never echo nothing (will always echo the empty string) because when the script reaches this code, $row = false.
I want to print mysql_query result in a table. I know how to do it but I am just confused. I tried this.
<?php
mysql_connect("localhost","root","") or die("Could not Connect.");
mysql_select_db("check") or die("Could not Select DB");
$table = "cc";
$i = 1;
$query = "select * from $table";
$sql = mysql_query($query);
if($sql){
echo "<table border='5'><tr>";
while($i<=2 && $row = mysql_fetch_array($sql)){
echo "<td>" . $row[id] . " : " . $row[name] . "</td>";
++$i;
}
echo "</tr><tr>";
while($i<=4 && $row = mysql_fetch_array($sql)){
echo "<td>" . $row[id] . " : " . $row[name] . "</td>";
++$i;
}
echo "</tr><tr>";
while($i<=6 && $row = mysql_fetch_array($sql)){
echo "<td>" . $row[id] . " : " . $row[name] . "</td>";
++$i;
}
echo "</tr><tr>";
while($i<=8 && $row = mysql_fetch_array($sql)){
echo "<td>" . $row[id] . " : " . $row[name] . "</td>";
++$i;
}
echo "</tr><tr>";
echo "</tr></table>";
}
?>
As you can see it is written again and again with a slight change of 2,4,6,8 in the while loop. It works but the problem is I cant rewrite it again and again as when the website will go live it will have more than 1000 entries. Could You guys help me out by suggesting another way to do this?
""** I need it to be like these dots (dots represent records in the database) **"""
. . . .
. . . .
. . . .
THANKS in Advance.
Ramzy
<?php
mysql_connect("localhost","root","") or die("Could not Connect.");
mysql_select_db("check") or die("Could not Select DB");
$table = "cc";
$query = "select * from $table";
$sql = mysql_query($query);
if($sql){
echo "<table border='5'><tr>";
while($row = mysql_fetch_array($sql)){
echo "<td>" . $row['id'] . " : " . $row['name'] . "</td>";
}
echo "</tr></table>";
}
?>
while($row = mysql_fetch_array($sql)) {
echo "<td>" . $row['id'] . " : " . $row['name'] . "</td>";
}
I don't really see what's the problem here.
By the way you should never call you're array like this $row[id] but you should quote the key instead $row['id']; Because if a constant id exists it will screw up your code and also for performance reason.
Just use
$limit = 1000;//place any value you need here to limit the number of rows displayed
while ($i<=$limit && $row = mysql_fetch_array($sql)){
echo "<td>" . $row['id'] . " : " . $row['name'] . "</td>";
++$i;
}
Also, that limit is unnecessary if all you want is to flush every record to the output. You could just do
while ($row = mysql_fetch_array($sql)){
echo "<td>" . $row['id'] . " : " . $row['name'] . "</td>";
}
And it will stop as soon as there are no more records.
To print all database rows into an HTML-table, use:
echo '<table>';
$i = 0; // $i is just for numbering the output, not really useful
while($row = mysql_fetch_array($sql))
{
echo '<tr><td>' . $i . ' - ' . $row['id'] . ' : ' . $row['name'] . '</td></tr>';
$i++;
}
echo '</tr></table>';
here is a general function I use:
function query_result_to_html_table($res, $table_id = NULL, $table_class = NULL, $display_null = true)
{
$table = array();
while ($tmp = mysql_fetch_assoc($res))
array_push($table, $tmp);
if (!count($table))
return false;
echo "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" "
. ($table_id ? "id=\"$table_id\" " : "")
. ($table_class ? "class=\"$table_class\" " : "") . ">";
echo "<tr>";
foreach (array_keys($table[0]) as $field_name) {
echo "<th>$field_name";
}
foreach ($table as $row) {
echo "<tr>";
foreach ($row as $col => $value) {
echo "<td>";
if ($value === NULL)
echo "NULL";
else
echo $value;
}
echo "\n";
}
echo "</table>\n";
return true;
}
I Got The Answer.. I wanted it to be like this. I made this and It Actually Works.
<?php
$i = 1;
mysql_connect("localhost" , "root" , "") or die('Could not Connect.');
mysql_select_db("db") or die('Could not select DB.');
$query = "select * from `check`";
$sql = mysql_query($query) or die(mysql_error());
echo "<table border='5' width='50%'><tr><th>Name</th><th>Gender</th></tr></table><table border='5' width='50%'><tr>";
if($i<3){
echo "<td align='center'>".$row['name']."</td>";
echo "<td align='center'>".$row['gender']."</td>";
++$i;
} else {
echo "<td align='center'>".$row['name']."</td><td align='center'>".$row['gender']."</td>";
echo "</tr>";
$i = 1;
echo "<tr>";
}
}
echo "</table>";
?>
</div>
Thank You Guys For Your Support