I have a php script that tries to find a particular name in a database where the specified program is $q, a variable passed from an html page. I'm very new to this so I'm having trouble figuring out how to code an if not found, then display type of message. Below is what I currently have:
$sql="SELECT * FROM names WHERE program='".$q."'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo "<div class='header'>Program Name:</div>";
echo "<div class='data'>";
echo $row['program'];
echo "</div>";
}
And I need it to echo a message saying if nothing was found. I tried looking at NOT IN condition in SQL and http://www.techonthenet.com/sql/exists.php along with other things on the internet but I'm not sure if this is the right thing to use. Any help would be appreciated.
use th php function
mysql_num_rows($result);
to check results found
So your code should be like:
$sql="SELECT * FROM names WHERE program='".addslashes($q)."'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
while($row = mysql_fetch_array($result))
{
echo "<div class='header'>Program Name:</div>";
echo "<div class='data'>";
echo $row['program'];
echo "</div>";
}
else
echo "No data found";
please note i added
addslashes($q)
in query, in order to avoid SQL injection problems.
$sql="SELECT * FROM names WHERE program='".$q."'";
$result = mysql_query($sql);
$found = false;
while($row = mysql_fetch_array($result))
{
$found = true;
echo "<div class='header'>Program Name:</div>";
echo "<div class='data'>";
echo $row['program'];
echo "</div>";
}
if ($found == false)
echo "I found nothing";
Related
<?php
$conn=mysqli_connect("localhost","id6755695_artemi8","sharanod"
,"id6755695_user_info");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$department = $_POST["department"];
$year = $_POST["year"];
$sem = $_POST["semester"];
$reg = $_POST["regulation"];
$sql = "SELECT book_name, author, edition, image FROM dept_search WHERE
department='$department' AND year='$year' AND semester='$sem' AND
regulations='$reg' ";
$result=mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($result))
{
if($row)
{
echo "<br>";
?>
<img src=" <?php echo $row['image']; ?> " height="300" width="300">
<?php
echo "<br>";
echo "<b>",$row['book_name'],"</b>";
echo "<br>";
echo $row['author'];
echo "<br>";
echo $row['edition'];
}
else
{
echo "sorry book not found";
}
}
mysqli_close($conn);
?>
please help me with this code,i am building a library management system.. The thing is I should be able to display the books if the given values are present i have in the database if not book not found must be displayed but in while loop after if, else does not runs.....
As others have pointed out, your else statement will never run. If you are already inside the while loop, you will certainly have $row defined and for that reason, else will never run.
What you can do is, check beforehand if the query returned actual results, like so:
$result=mysqli_query($conn,$sql);
if($result->num_rows > 0){
while($row = mysqli_fetch_assoc($result)){
echo "<br>";
?>
<img src=" <?php echo $row['image']; ?> " height="300" width="300">
<?php
echo "<br>";
echo "<b>",$row['book_name'],"</b>";
echo "<br>";
echo $row['author'];
echo "<br>";
echo $row['edition'];
}
}else{
echo "Sorry book not found";
}
You can try with mysqli_num_rows .. sample code as follows :
$rowcount=mysqli_num_rows($conn,$sql);
if($rowcount!=0){
$result=mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($result))
{
echo "<br>";
?>
You are looping through all the rows returned from the "mysqli_fetch..." command. Your "if" and "else" is useless -- you will always have rows. If you get no rows, you do not even enter the body of the while loop.
You need to COUNT the rows returned (count($row)) and display a message that nothing was found if the count is less than one.
All you need to do is that you have to change if the condition from if($row) to if($other_condition)
Currently, you are just checking either there is something inside $row, and this condition will never be wrong unless you will assign it null. Because where $row will have something then while loop will be executed, and when while loop will be executed then if condition will be executed.
you have to simply one thing, that is to change if condition like given below...
if($row['value'] == 'something')
$sql = "SELECT * FROM today WHERE heading='$heading' and day='$day'";
$sql1 = "SELECT * FROM today WHERE day='$day'";
$result = $conn->query($sql);
$result1 = $conn->query($sql1);
if ($result->num_rows > 0) {
echo "<div id='post'><h1>".$row["heading"]."</h1>
<aside class='related-post'>".while($row = $result1->fetch_assoc())
{echo'<img src='".$row["image"]."'>;}
.</aside>}";
I have been using while loops for fetching data from table. My connection is working is perfect but I need another loop in the first that is not working. Isn't it the good way?
Update: I tried to finish echo and again started as follow but still an error
while($row = $result->fetch_assoc()) {
echo "<div id='post'><h1>"
.$row["heading"].
"</h1><div class='post-side'><img class='post-image' src='"
.$row["image"].
"'><div class='post-data'><p><strong>Age: </strong><span>$age</span></p><p><strong>Date of birth: </strong><span>"
.$row["day"].
"-"
.$row["month"].
"-"
.$row["year"].
"</span></p></div></div></div><div class='description'><p>"
.$row["description"].
"</p></div><div class='bottom-related'><aside class='related-post'>";
while($row = $result1->fetch_assoc())
{echo"<img src='"
.$row["image"].
"'>/";}.echo"</aside><aside class='ad2'>".$includead."</aside></div>";
}
echo "</div>";
} else {
echo "No table found";
}
$conn->close();
You're trying to concatenate to a string a WHILE loop; this is wrong.
You should echo your first part, end with it and then do your while loop, and echo the end afterwards:
Your quotes are a bit messed up as well
if ($result->num_rows > 0)
{
echo "<div id='post'><h1>".$row["heading"]."</h1>
<aside class='related-post'>";
while($row = $result1->fetch_assoc())
{
echo'<img src="'.$row["image"].'">';
}
echo '</aside>';
}
You can't concatene while with String, it's a syntaxic error
Also, you have a probleme when trying to echo a String, you can use this syntax:
echo "PHP"; // will evaluate PHP variables and whitespace inside a string
echo 'PHP'; // will evaluate nothing;
But you can not start flushing a string ' and finish by " or vice-versa.
Here the correct code :
<?php
$sql = "SELECT * FROM today WHERE heading='$heading' and day='$day'";
$sql1 = "SELECT * FROM today WHERE day='$day'";
$result = $conn->query($sql);
$result1 = $conn->query($sql1);
if ($result->num_rows > 0) {
echo "<div id='post'><h1>" . $row["heading"] . "</h1><aside class='related-post'>";
while($row = $result1->fetch_assoc()) {
echo'<img src="' . $row["image"] .'">';
}
echo "</aside>";
}
What is the problem with my code?
$sql = "select username,num from login order by num desc";
$result = $conn->query($sql);
$rnk=1;
if ($result->num_rows > 0) {
while($row = mysqli_fetch_array($result)){
echo "<tr><td>";
echo $row["username"];
echo "</td><td>";
echo $row["num"];
echo "</td><td>";
echo $rnk;
echo "</td></tr>"
}
} else {
echo "0 results found";
}
$conn->close();
I had placed the above code after providing necessary connection codes between table element in my html page with .php extension. When i call the page it shows only a blank page. Its not even showing the table headers.
Try this one
$sql = "select username,num from login order by num desc";
$result = $conn->query($sql);
$rnk=1;
if ($result->num_rows > 0) {
echo "<table>";
while($row = mysqli_fetch_array($result)){
echo "<tr><td>".($row['username'])."</td>";
echo "<td>".($row['num'])."</td>";
echo "<td>".($rnk)."</td></tr>";
}
echo "</table>";
} else {
echo "0 results found";
}
$conn->close();
Crank up PHP error reporting so you can see what the real problem is.
http://php.net/manual/en/errorfunc.configuration.php#ini.error-reporting
Also look in the Apache error.log to see what, if any, errors are being logged. Assuming you are using Apache
Can someone take a look at my code? I am trying to display a record from a link from the previous page. When I echo $id (below)... it displays the value of 'id' from the previous page. However the query returns all rows of the table. I had it working earlier. I have since changed something. Pleas point out what I over looking. Thank you, in advance.
<?php
mysql_connect('host', 'userid', 'password') or die (mysql_error());
mysql_select_db('my_database') or die (mysql_error());
//$result = mysql_query("SELECT * from table");
$id= mysql_real_escape_string($_GET["id"]);
echo $id;
$result = mysql_query("SELECT * FROM uatbeta1 WHERE id=".intval($_REQUEST['id']));
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);
echo $result;
while($row = mysql_fetch_array($result)){
//Display the results from the current row and a line break
echo "<table border='1'>";
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['company']."</td>";
echo "<td>".$row['constructioncontractor']."</td>";
echo "<td>".$row['weldingcontractor']."</td>";
echo "<td>".$row['ndtcontractor']."</td>";
echo "<td>".$row['weldid']."</td>";
echo "<td>".$row['projectname']."</td>";
echo "<td>".$row['examinationdate']."</td>";
echo "<td>".$row['shift']."</td>";
echo "</tr>";
Why not use $id? You have it nicely escaped, so use that variable:
$result = mysql_query("SELECT * FROM uatbeta1 WHERE id=".$id);
this program must output a directory that you searched for how ever if its not found a message must appear that the org_name is not found,i don't know how to do that, i keep trying on some if-else but it just won't output it.
<?php
$con=mysql_connect("localhost","root","");
if(!$con) {
die('could not connect:'.mysql_error());
}
mysql_select_db("final?orgdocs",$con);
$org_name = $_POST["org_name"];
$position = $_POST["position"];
$result = mysql_query("SELECT * FROM directory WHERE org_name = '$org_name' OR position = '$position' ORDER BY org_name");
echo '<TABLE BORDER = "1">';
$result1 = $result;
echo '<TR>'.'<TD>'.'Name'.'</TD>'.'<TD>'.'Organization Name'.'</TD>'.'<TD>'.'Position'.'</TD>'.'<TD>'.'Cell Number'.'</TD>'.'<TD>'.'Email-Add'.'</TD>';
echo '</TR>';
while ( $row = mysql_fetch_array($result1) ){
echo '<TR>'.'<TD>'.$row['name'].'</TD>'.'<TD>'.$row['org_name'].'</TD>';
echo '<TD>'.$row['position'].'</TD>'.'<TD>'.$row['cell_num'].'</TD>'.'<TD>'.$row['email_add'].'</TD>';
echo '</TR>';
}
echo '</TABLE>';
?>
What you want is mysql_num_rows to check your query for how many result rows it has.
http://de2.php.net/manual/en/function.mysql-num-rows.php
if (mysql_num_rows($result)>0) {
// your thing above with mysql_fetch_array($result1) etc
} else {
echo 'nor found';
}