asmith6 Posted April 20, 2011 Share Posted April 20, 2011 <?php $objConnect = mysql_connect("localhost","","cgdfgdfg") or die(mysql_error()); $objDB = mysql_select_db("ffdfvbbd"); $pic2 = "SELECT * FROM images"; if (!isset($_GET['Page'])) $_GET['Page']='0'; $pic1 = mysql_query($pic2); $Num_Rows = mysql_num_rows($pic1); $Per_Page = 16; // Per Page $Page = $_GET["Page"]; if(!$_GET["Page"]) {$Page=1;} $Prev_Page = $Page-1; $Next_Page = $Page+1; $Page_Start = (($Per_Page*$Page)-$Per_Page); if($Num_Rows<=$Per_Page) {$Num_Pages =1;} else if(($Num_Rows % $Per_Page)==0) {$Num_Pages =($Num_Rows/$Per_Page) ;} else {$Num_Pages =($Num_Rows/$Per_Page)+1; $Num_Pages = (int)$Num_Pages;} $pic2 .="ORDER by thumbnailID DESC LIMIT $Page_Start , $Per_Page" ; $pic1 = mysql_query($pic2); $cell = 0; $link1 = "SELECT * FROM images"; echo ' <div id="tablediv"> <table border="0" cellpadding="17" cellspacing="0" class="table"> <tr>'; while($pic = mysql_fetch_array($pic1)) { if($cell % 4 == 0) { echo '</tr><tr>'; } if($cell == 2) { echo ' <td> filler </td>'; } elseif ($cell == 3) { echo ' <td> filler </td>'; } else { echo ' <td> <a href="/' . $pic["link"] . '.php"> <div class="image"> <img src="https://s3.amazonaws.com/images/' . $pic["pic"] . '.png" alt="' . $pic["alt"] . '" height="200" width="200" /> </div> </a> </td>'; } $cell++; } echo '</tr></table></div>'; ?> The code above works just fine. However, once I add a WHERE function,as shown below, I get a "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource" error. <?php $objConnect = mysql_connect("localhost","","cgdfgdfg") or die(mysql_error()); $objDB = mysql_select_db("ffdfvbbd"); $pic2 = "SELECT * FROM images WHERE folder = 'blog' "; //WHERE FUNCTION IS HERE if (!isset($_GET['Page'])) $_GET['Page']='0'; $pic1 = mysql_query($pic2); $Num_Rows = mysql_num_rows($pic1); $Per_Page = 16; // Per Page $Page = $_GET["Page"]; if(!$_GET["Page"]) {$Page=1;} $Prev_Page = $Page-1; $Next_Page = $Page+1; $Page_Start = (($Per_Page*$Page)-$Per_Page); if($Num_Rows<=$Per_Page) {$Num_Pages =1;} else if(($Num_Rows % $Per_Page)==0) {$Num_Pages =($Num_Rows/$Per_Page) ;} else {$Num_Pages =($Num_Rows/$Per_Page)+1; $Num_Pages = (int)$Num_Pages;} $pic2 .="ORDER by thumbnailID DESC LIMIT $Page_Start , $Per_Page" ; $pic1 = mysql_query($pic2); My mysql table includes column thumbnailID folder link pic alt time The folder column is there so I can specify what I want in the page. Anyhow, why won't it work? Quote Link to comment https://forums.phpfreaks.com/topic/234276-my-code-works-fine-once-i-add-a-where-sql-function-it-doesnt/ Share on other sites More sharing options...
spiderwell Posted April 20, 2011 Share Posted April 20, 2011 try echoing out the sql statement just before you execute it, so you can see what it says exactly, and if need copy it into phpmyadmin Quote Link to comment https://forums.phpfreaks.com/topic/234276-my-code-works-fine-once-i-add-a-where-sql-function-it-doesnt/#findComment-1204078 Share on other sites More sharing options...
asmith6 Posted April 20, 2011 Author Share Posted April 20, 2011 try echoing out the sql statement just before you execute it, so you can see what it says exactly, and if need copy it into phpmyadmin If you mean echoing it at $pic2, like this: $pic2 .="WHERE folder = 'blog' ORDER by thumbnailID DESC LIMIT $Page_Start , $Per_Page" ; or like this: $pic2 .="ORDER by thumbnailID DESC LIMIT $Page_Start , $Per_Page WHERE folder = 'blog'" ; I already tried both and none worked. When I enter into phpmyadmin, SELECT * FROM images WHERE folder = 'blog', it works, but when I enter in SELECT * FROM images WHERE folder = 'blog' ORDER by thumbnailID DESC LIMIT, it doesn't. Quote Link to comment https://forums.phpfreaks.com/topic/234276-my-code-works-fine-once-i-add-a-where-sql-function-it-doesnt/#findComment-1204086 Share on other sites More sharing options...
spiderwell Posted April 20, 2011 Share Posted April 20, 2011 not quite $pic2 .="ORDER by thumbnailID DESC LIMIT $Page_Start , $Per_Page" ; echo $pic2 ; $pic1 = mysql_query($pic2); then run the script and see what it says Quote Link to comment https://forums.phpfreaks.com/topic/234276-my-code-works-fine-once-i-add-a-where-sql-function-it-doesnt/#findComment-1204088 Share on other sites More sharing options...
asmith6 Posted April 20, 2011 Author Share Posted April 20, 2011 Wow, very nice lol. The problem was there was no spacing between the 'blog' and ORDER Thank you for the tip ^^;; Quote Link to comment https://forums.phpfreaks.com/topic/234276-my-code-works-fine-once-i-add-a-where-sql-function-it-doesnt/#findComment-1204093 Share on other sites More sharing options...
spiderwell Posted April 20, 2011 Share Posted April 20, 2011 its the top debugging tip for SQL not working, i always do it Quote Link to comment https://forums.phpfreaks.com/topic/234276-my-code-works-fine-once-i-add-a-where-sql-function-it-doesnt/#findComment-1204104 Share on other sites More sharing options...
Muddy_Funster Posted April 20, 2011 Share Posted April 20, 2011 that and using or die() to manage the return of SQL errors. Quote Link to comment https://forums.phpfreaks.com/topic/234276-my-code-works-fine-once-i-add-a-where-sql-function-it-doesnt/#findComment-1204165 Share on other sites More sharing options...
asmith6 Posted April 20, 2011 Author Share Posted April 20, 2011 Good tips, good tips, and um, for some reason, the OR operator works, while the AND doesn't. Am I missing anything? This works, and always displays 'blog', and not display politics $pic2 = "SELECT * FROM images WHERE folder = 'blog' OR folder = 'politics'"; However, this does not work at all. $pic2 = "SELECT * FROM images WHERE folder = 'blog' AND folder = 'politics'"; Why is that? Quote Link to comment https://forums.phpfreaks.com/topic/234276-my-code-works-fine-once-i-add-a-where-sql-function-it-doesnt/#findComment-1204243 Share on other sites More sharing options...
Muddy_Funster Posted April 20, 2011 Share Posted April 20, 2011 because with your AND your condition is saying "return records which have the folder field equal to BOTH blog AND politics AT THE SAME TIME" which is of cource impossable. Quote Link to comment https://forums.phpfreaks.com/topic/234276-my-code-works-fine-once-i-add-a-where-sql-function-it-doesnt/#findComment-1204249 Share on other sites More sharing options...
asmith6 Posted April 20, 2011 Author Share Posted April 20, 2011 because with your AND your condition is saying "return records which have the folder field equal to BOTH blog AND politics AT THE SAME TIME" which is of cource impossable. So is there any way to display both politics and blog? Quote Link to comment https://forums.phpfreaks.com/topic/234276-my-code-works-fine-once-i-add-a-where-sql-function-it-doesnt/#findComment-1204259 Share on other sites More sharing options...
Muddy_Funster Posted April 20, 2011 Share Posted April 20, 2011 the OR statement should do it, how are you displaying your results? Quote Link to comment https://forums.phpfreaks.com/topic/234276-my-code-works-fine-once-i-add-a-where-sql-function-it-doesnt/#findComment-1204264 Share on other sites More sharing options...
asmith6 Posted April 20, 2011 Author Share Posted April 20, 2011 The OR statement only displays the first listed. In other words, if it's folder='blogs' or folder='politics', it'll display blogs only. And it's being displayed like an image image gallery. If you want to see how the image gallery works, you can find it here (the first post): http://www.phpfreaks.com/forums/index.php?topic=330918.0 Quote Link to comment https://forums.phpfreaks.com/topic/234276-my-code-works-fine-once-i-add-a-where-sql-function-it-doesnt/#findComment-1204270 Share on other sites More sharing options...
btherl Posted April 20, 2011 Share Posted April 20, 2011 The "AND" you're thinking of is "OR" in sql. The thing to remember is that SQL checks the condition on each result, not on the entire set of results. So if you say "SELECT * FROM pets WHERE type = 'dog' AND type = 'cat'", it will look at the first pet and ask "Is this a dog AND is it a cat too?". Then it'll look at the second pet, checking if it's a dog-cat. And it'll never find a dog-cat, because they don't exist. But if you ask for pets which are dogs OR cats, it'll look at the first pet and ask "Is this a dog OR is this a cat?". And it'll find all pets which are either dogs OR cats. So the problem is most likely that there are no results where folder = 'politics', or that your code that follows is not displaying them. Quote Link to comment https://forums.phpfreaks.com/topic/234276-my-code-works-fine-once-i-add-a-where-sql-function-it-doesnt/#findComment-1204275 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.