Jump to content

My code works fine. Once I add a WHERE sql function, it doesn't


Recommended Posts

<?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?

 

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.

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?

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?

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

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. 

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.