Jump to content

Recommended Posts

hello,

 

I am using php to access my database and return rows that meet two different parameters.  This one happens to return a number of articles in a list with links to each article's content.

 

The problem I am having is that when I query the database for rows that meet two different criteria (id number and field['type']) it returns blank rows where the row in the database doesn't meet both.  Here is my code:

 

<?php
//begin recent news doc

@ $db = mysqli_connect('xxxxxx', 'xxxxxxx', 'xxxxxxx', 'xxxxxxx');

if (mysqli_connect_errno($db)) {
	echo 'Error: Could not connect to database.  Please try again later.';
}

mysqli_select_db($db, 'xxxxxxxxx');
$result = mysqli_query($db, "SELECT id, title, price FROM Equipmentposting");
$num_rows = mysqli_num_rows($result);

$i = $num_rows;

for($i = $num_rows; $i >= 1; $i--) {
	$sql = "SELECT id, date, type, title, price FROM Equipmentposting WHERE id = $i && type = 'General Laboratory'";
	$queryresult = mysqli_query($db, $sql);
	$rowresult = mysqli_fetch_array($queryresult, MYSQLI_ASSOC);

	$id = $rowresult['id'];

	echo '<a href="equiparticle.php?id='.$id.'">'.$rowresult['date'].'     '.$rowresult['title'].'   '.$rowresult['price'].'</a>';
	echo "<br>";
	}

?>

 

my output looks something like this:

 

"//blank line

active link because article meets both criteria

//blank line

//blank line

//blank line

active link because article meets both criteria"

 

The blank lines are the rows in the table that have the id, but not type 'General Laboratory'.  How do I make it that it does not display the blank lines?  Oh, and I am using the id part as well so that postings with larger id numbers are displayed first (they are more recent posts).

 

change

      $sql = "SELECT id, date, type, title, price FROM Equipmentposting WHERE id = $i && type = 'General Laboratory'";

to

      $sql = "SELECT id, date, type, title, price FROM Equipmentposting WHERE id = $i AND type = 'General Laboratory'";

 

Hope that helps!

Unfortunately I've tried it with the "&&" and the "AND" and they both act the same way.  Are there any other ideas?  Perhaps an if statement that says if the type is equal to 'General Laboratory' then echo the line, else, skip?

 

If that might work, how do I say in code "else skip".  Thanks.

<?php
//begin recent news doc

   @ $db = mysqli_connect('xxxxxx', 'xxxxxxx', 'xxxxxxx', 'xxxxxxx');
   
   if (mysqli_connect_errno($db)) {
      echo 'Error: Could not connect to database.  Please try again later.';
   }
   
   mysqli_select_db($db, 'xxxxxxxxx');
   $result = mysqli_query($db, "SELECT id, title, price FROM Equipmentposting WHERE type = 'General Laboratory' ORDER BY id");
   $num_rows = mysqli_num_rows($result);
   
   if ($num_rows > 0) { 
      while ($rowresult = mysqli_fetch_array($queryresult, MYSQLI_ASSOC)) {
            $id = $rowresult['id'];
            echo '<a href="equiparticle.php?id='.$id.'">'.$rowresult['date'].'     '.$rowresult['title'].'   '.$rowresult['price'].'</a>';
            echo "<br>";
      }
   }
?>

 

How you had your script setup made no sense at all. I modified it and see if that gets you the desired results.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.