0

I am using this query below and it only returns the first query in the entry if i use only the if (empty($field1)) to display. If i fill in the print(""); it works but i want to use the if (empty($field1)) snippet to display. How can i do it?

$sql="SELECT field1, field2 FROM table WHERE p_id='$pid'
      and k_id='$kid' ORDER BY id DESC";  
$result=mysql_query($sql);

$query = mysql_query($sql) or die ("Error: ".mysql_error());

if ($result == "")
{
    echo "";
}

echo "";

$rows = mysql_num_rows($result);

if($rows == 0)
{
    print("");
}
elseif($rows > 0)
{
    while($row = mysql_fetch_array($query))
    {
        $field1 = $row['field1'];
        $field2 = $row['field2'];
        print("");
    }
}

if (empty($field1)) {
    echo ""; //Thats right, i don't want anything to show for this portion
} else {
   echo "<div id=comments>Comments</div><br>
   <div id=entries>$field1 and $field2</div>";
}
10
  • 1
    are you executing your query twice or is it just because of an inconsistancy in the example? Commented Sep 22, 2011 at 14:04
  • 1
    How many rows do you expect it to return? What are p_id and k_id? Commented Sep 22, 2011 at 14:04
  • 1
    Check the values of $pid and $kid - possibly they aren't what you expect? Commented Sep 22, 2011 at 14:05
  • 1
    You are selecting field1 and field2, but later you are using $row['field1_name'] and $row['field2_id']. Commented Sep 22, 2011 at 14:07
  • 1
    Please consider reposting your actual code. The code provided doesn't show where you use $field1 and $field2 Commented Sep 22, 2011 at 14:15

2 Answers 2

2

What are you trying to do? somthing like this:

$sql="SELECT field1, field2 FROM table WHERE p_id='$pid' and k_id='$kid' ORDER BY id DESC";  
$result=mysql_query($sql)  or die ("Error: ".mysql_error());
$rows = mysql_num_rows($result);

if ($rows > 0)
  echo "here are your entries\n";

while($row = mysql_fetch_array($result))
{
    echo $row['field1']." ";
    echo $row['field2']."\n";
}

another way

$sql="SELECT field1, field2 FROM table WHERE p_id='$pid' and k_id='$kid' ORDER BY id DESC";  
$result=mysql_query($sql)  or die ("Error: ".mysql_error());
$rows = mysql_num_rows($result);

if ($rows > 0)
  echo "here are your entries\n";

while($row = mysql_fetch_array($result))
{
    if (empty($row['field1'])) {
        echo " ";
    } else {
    echo $row['field1']." ";
    echo $row['field2']."\n";
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

that would work but i need to have to dislay using f (empty($field1)) { echo ""; } else { echo "here are your entries"; }
1

i believe mysql_fetch_array only returns one row http://www.w3schools.com/PHP/func_mysql_fetch_array.asp

also ur sure that neither p_id and k_id are not unique?

i would also try $sql="SELECT * FROM table WHERE p_id='$pid' and k_id='$kid' ORDER BY id DESC";

just to see if that yields any different results, you can always parse out just the two fields from the return data

TRY THIS TO START WITH (the $results variable is just confusing things):

  $sql="SELECT field1, field2 FROM table WHERE p_id='$pid'
      and k_id='$kid' ORDER BY id DESC";  

      $query = mysql_query($sql) or die ("Error: ".mysql_error());

   $rows = mysql_num_rows($query);

  if($rows == 0)
  {
  print("");

  }else{

  while($row = mysql_fetch_array($query))
   {
          if ($row['field1'] == "")
            {
               print("");
            }else{
           $field1 = $row['field1'];
           print($field1)
            }

if ($row['field2'] == "")
            {
               print("");
            }else{
           $field1 = $row['field2'];
           print($field2)
            }
  }

}

8 Comments

i have checked that works. the query works i use the print("") to display but if you notice i am using the if empty snipet so maybe thats why?
well per the other post about running your query twice and not really using the results in $results i would say clean up the code, run it, see what happens, then update us here. maybe update the code block in your post too since it looks a little messy
i would also say to maybe run your query in a sql window (as opposed to just within your code). this will give u a real understanding of what data is in there and what your query actually returns
ok if i use print("$field1 and $field2"); then all etnires show up, but as you can see i am not doing that, i want to use the if (empty($field1)) { echo ""; } else { echo "$field1 and $field2"; } instead of print("");
see my edits and if that fits your needs. what i dont get is a null field is a null field. meaning printing "" is the equivalent to printing a values thats set to null.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.