neogemima Posted April 28, 2009 Share Posted April 28, 2009 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). Quote Link to comment https://forums.phpfreaks.com/topic/155928-solved-return-row-with-multiple-parameters-satisfied/ Share on other sites More sharing options...
mikesta707 Posted April 28, 2009 Share Posted April 28, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/155928-solved-return-row-with-multiple-parameters-satisfied/#findComment-820805 Share on other sites More sharing options...
neogemima Posted April 28, 2009 Author Share Posted April 28, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/155928-solved-return-row-with-multiple-parameters-satisfied/#findComment-821251 Share on other sites More sharing options...
premiso Posted April 28, 2009 Share Posted April 28, 2009 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/155928-solved-return-row-with-multiple-parameters-satisfied/#findComment-821271 Share on other sites More sharing options...
neogemima Posted April 28, 2009 Author Share Posted April 28, 2009 Premiso, that worked perfectly with one or two tweaks. Thank you much, I was able to have them list in reverse by saying "ORDER BY -id". Thanks much. Quote Link to comment https://forums.phpfreaks.com/topic/155928-solved-return-row-with-multiple-parameters-satisfied/#findComment-821469 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.