php - While loop in if statement

626

I have bellow code which jobs announced which load only one record i have used while loop which doesn't work.

<?php
$job="SELECT * from jobs where job_exp_date>=now()";
$query=mysql_query($job);
$result=mysql_fetch_assoc($query);
if($result==0) {
echo "Sorry We Don't have current openings. Thank you for your intreset";
}else{
echo"<table width=\"auto\">&nbsp;
<tr>
    <th><p class=\"slide-title\"><strong>Title &nbsp;&nbsp;</strong></p></th>
    <th><p class=\"slide-title\"><strong>Location &nbsp;&nbsp;</p></strong></th>
    <th><p class=\"slide-title\"><strong>Expiry Date &nbsp;&nbsp;</p></strong></th>
</tr>";
while($result1=mysql_fetch_array($query)){
    echo "<tr>
    <td>";echo $result1['job_title']; echo "</td>&nbsp;&nbsp;
    <td>";echo $result1['job_location']; echo "</td>&nbsp;&nbsp;
    <td>";echo $result1['job_exp_date']; echo "</td>
</tr>"; } echo " 
</table>"; 
 }
?>
154

Answer

Solution:

try change

$result=mysql_fetch_assoc($query);

to

$result=mysql_num_rows($query);
428

Answer

Solution:

use this

<?php
    $job="SELECT * from jobs where job_exp_date>=now()";
    $query=mysql_query($job);
    if(mysql_num_rows($query) == 0) 
    {
        echo "Sorry We Don't have current openings. Thank you for your intreset";
    }
    else
    {
        echo "<table width=\"auto\">&nbsp;
        <tr>
            <th><p class=\"slide-title\"><strong>Title &nbsp;&nbsp;</strong></p></th>
            <th><p class=\"slide-title\"><strong>Location &nbsp;&nbsp;</p></strong></th>
            <th><p class=\"slide-title\"><strong>Expiry Date &nbsp;&nbsp;</p></strong></th>
        </tr>";
        while($result1=mysql_fetch_array($query))
        {
            echo "<tr>
                <td>";echo $result1['job_title']; echo "</td>&nbsp;&nbsp;
                <td>";echo $result1['job_location']; echo "</td>&nbsp;&nbsp;
                <td>";echo $result1['job_exp_date']; echo "</td>
                </tr>"; 
        } 
        echo "</table>"; 
    }
?>
187

Answer

Solution:

<?php
$job="SELECT * from jobs where job_exp_date>=now()";
$query=mysql_query($job);
$rows=mysql_num_rows($query);
if($rows==0) {
echo "Sorry We Don't have current openings. Thank you for your intreset";
}else{
echo"<table width=\"auto\">&nbsp;
<tr>
    <th><p class=\"slide-title\"><strong>Title &nbsp;&nbsp;</strong></p></th>
    <th><p class=\"slide-title\"><strong>Location &nbsp;&nbsp;</p></strong></th>
    <th><p class=\"slide-title\"><strong>Expiry Date &nbsp;&nbsp;</p></strong></th>
</tr>";
while($result=mysql_fetch_array($query)){
    echo "<tr>
    <td>";echo $result['job_title']; echo "</td>&nbsp;&nbsp;
    <td>";echo $result['job_location']; echo "</td>&nbsp;&nbsp;
    <td>";echo $result['job_exp_date']; echo "</td>
</tr>"; } echo " 
</table>"; 
 }
?>

Try this......when ever you want to check that query get a result or not use "mysql_num_rows"

694

Answer

Solution:

Try this code... it's helpful to you  :-)

Just minor error in your code. i had change just little bit change in your code and it's working fine...

People are also looking for solutions to the problem: php - Load the page content using jQuery?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.