Well at least I think my troubles are with the get variable. I know my code is amateur I am very new to PHP and trying to learn as much as I can.

So I am trying to create a photo gallery any having trouble with getting my get variable to do its job.

I have 2 pages:
photo.php - which I believe works just fine.

$con = mysql_connect("...","...","...");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("...", $con);

$result = mysql_query("SELECT * FROM photo_albums ORDER BY uptime DESC");

while($row = mysql_fetch_array($result))
  {
  echo '<div class="photoThumb">';
  echo '<a href="viewAlbum.php?aid='.$row['aid'].'"><img src="' . $row['filepath'];
  echo $row['filename'] . '"';
  echo 'border="0" /></a>';
  echo '<br/>';
  echo '<span class="thumbText"><a href="viewAlbum.php?aid='.$row['aid'].'">' . $row['stitle'];
  echo  '</a><br/>';
  echo $row['photog'];
  echo '</span>';
  echo "</div>";
  }
mysql_close($con);

Then viewAlbum.php - this is whats not working. I have tried many different ways of code and now I am just lost.

$con = mysql_connect("...","...","...");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("...", $con);


$aid = $_GET['aid'];
$select = ("SELECT * FROM 'pictures' WHERE 'aid' = '$aid'");
    $result = mysql_query($select) or die(mysql_error());

while($row = mysql_fetch_array($result))
  {
  extract ($row);
  echo '<div class="photoThumb">';
  echo '<a href="';
  echo $row['filepath'];
  echo $row['filename'];
  echo '"><img src="';
  echo $row['filepath'];
  echo $row['filename'];
  echo '" rel="lightbox[' .$row['aid'];
  echo ']" border="0" width="100px" /></a><br/>';
  echo "</div>";
  }
mysql_close($con);

Can anyone help me get this sorted out??
Thanks,
Brian

Recommended Answers

All 37 Replies

<?php
$con = mysql_connect("...","...","...");
if (!$con)  {  die('Could not connect: ' . mysql_error());  }
mysql_select_db("...", $con);
$result = mysql_query("SELECT * FROM photo_albums ORDER BY uptime DESC");
while($row = mysql_fetch_array($result))  {
echo "<div class='photoThumb'>";
."<a href='viewAlbum.php?aid='$row['aid']'>";
."<img src='$row['filepath']$row['filename']' border='0' />";
.'</a><br/><span class="thumbText">';
."<a href='viewAlbum.php?aid='$row['aid']'>$row['stitle']</a><br/>";
."$row['photog']</span></div>";
}
mysql_close($con); ?>

to my mind once the array $row is extracted $row[variable] ceases to exist and $variable is created

<?php
$con = mysql_connect("...","...","...");
if (!$con) { die('Could not connect: ' . mysql_error()); }
mysql_select_db("...", $con);
$aid = $_GET['aid'];
$select = ("SELECT * FROM 'pictures' WHERE 'aid' = '$aid'");
$result = mysql_query($select) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
extract ($row);
echo "<div class='photoThumb'>";
."<a href='$filepath$filename'>";
."<img src='$filepath$filename' rel='lightbox[\"$aid\"]' border='0' width='100px' />";
."</a><br/></div>";
}
mysql_close($con); ?>

or

<?php
$con = mysql_connect("...","...","...");
if (!$con) { die('Could not connect: ' . mysql_error()); }
mysql_select_db("...", $con);
$aid = $_GET['aid'];
$select = ("SELECT * FROM 'pictures' WHERE 'aid' = '$aid'");
$result = mysql_query($select) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
echo "<div class='photoThumb'>";
."<a href='$row['filepath']$row['filename']'>";
."<img src='$row['filepath']$row['filename']' rel='lightbox[\"$aid\"]' border='0' width='100px' />";
."</a><br/></div>";
}
mysql_close($con); ?>

dquotes " let php parse the text in the echo statments for variables, (lazy)

So I am getting an error here:

."<a href='$row['filepath']$row['filename']'>";
."<img src='$row['filepath']$row['filename']' rel='lightbox[\"$aid\"]' border='0' width='100px' />";
."</a><br/></div>";
}
mysql_close($con); ?>

It says at line 16 but that's were line 16 starts

after I cleared my cookies I got an error here on the photo.php page.

."<a href='viewAlbum.php?aid='$row['aid']'>";
."<img src='$row['filepath']$row['filename']' border='0' />";
.'</a><br/><span class="thumbText">';
."<a href='viewAlbum.php?aid='$row['aid']'>$row['stitle']</a><br/>";
."$row['photog']</span></div>";
}
mysql_close($con); ?>

I have no idea, my dummy database had all text fields, and the code functions, thinkning
thought: amend the fieldnames of your existing code either as $variable or remove the extract() code
I have very obviously made an incorrect assumption about the source data somewhere
or made an obvious idiocy in the code structure that should be jumping out at me and isnt

well the first code that I originally posted for photo.php works fine. So I put that back to normal.
Does anyone have any suggestions on how to fix the viewAlbum.php code?

My two cents would be that the errors are coming from the way the single and double quotes are structured. Some are missing and others need to be escaped.

Perhaps it should look something like this:
photo.php

.'<a href="viewAlbum.php?aid=$row[\'aid\']">';
.'<img src="$row[\'filepath\'].$row[\'filename\']" border="0"/>';
.'</a><br/><span class="thumbText">';
.'<a href="viewAlbum.php?aid=$row[\'aid\']">$row[\'stitle\']</a><br/>';
."$row['photog']</span></div>";
}
mysql_close($con); ?>

Again, just my two cents.

thinking carefully and slowly,
'obvious idiocy in the code structure' seems to be an understatement
thanks cfrog

extract($row) destroys the $row array and creates variables of its key;value pairs

comment out // extract[$row]; and try the original page again

okay, so now I am confused.
My very first original code setup from photo.php worked fine. Or at least I think it works fine. Here is my code for that:

$result = mysql_query("SELECT * FROM photo_albums ORDER BY uptime DESC");
while($row = mysql_fetch_array($result))
  {
  echo '<div class="photoThumb">';
  echo '<a href="viewAlbum.php?aid='.$row['aid'].'"><img src="' . $row['filepath'];
  echo $row['filename'] . '"';
  echo 'border="0" /></a>';
  echo '<br/>';
  echo '<span class="thumbText"><a href="viewAlbum.php?aid='.$row['aid'].'">' . $row['stitle'];
  echo  '</a><br/>';
  echo $row['photog'];
  echo '</span>';
  echo "</div>";
  }
mysql_close($con);
?>

I did all the changing that you guys have said. But I am still pretty sure my problem lies on the viewAlbum.php page:

Here is my current code for that page:

$aid = $_GET['aid'];
$select = ("SELECT * FROM 'pictures' WHERE 'aid' = '$aid'");
$result = mysql_query($select) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
echo "<div class='photoThumb'>";
."<a href='$row[\'filepath\']$row[\'filename\']'>";
."<img src='$row[\'filepath\']$row[\'filename\']' rel='lightbox[\'$aid\']' border='0' width='100px' />";
."</a><br/></div>";
}
mysql_close($con); ?>

On this viewAlbum.php I get an Error
Parse error: syntax error, unexpected '.'

Try this:

$aid = $_GET['aid'];
$select = ("SELECT * FROM 'pictures' WHERE 'aid' = '$aid'");
$result = mysql_query($select) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
echo "<div class='photoThumb'>";
."<a href='$row[\'filepath\'].$row[\'filename\']'>";
."<img src='$row[\'filepath\'].$row[\'filename\']' rel='lightbox[\'$aid\']' border='0' width='100px' />";
."</a><br/></div>";
}
mysql_close($con); ?>

Parse error: syntax error, unexpected '.'
I think my problem lies in that period. Is there any other way to write the code?

Sorry, that was my bad :)

This should work for you:

echo '<div class="photoThumb">
<a href="$row[\'filepath\'].$row[\'filename\']">
<img src="$row[\'filepath\'].$row[\'filename\']" rel="lightbox[\'$aid\']" border="0" width="100px" />
</a><br/></div>';

now here is my error message... this is sooo frustrating.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''pictures' WHERE 'aid' = ''' at line 1

My MySQL version is 5.0 if that helps at all.

What I dont get is how it was able to parse the code all the way down to the echo statement but now it wont parse past the mysql
_query.

Parse errors happen when the formatting (syntax) of your code incorrect. Because a parse error is only detected once an unexpected piece of code is reached by the PHP intepreter, it often gives you the wrong line number of the error.

The error in this case is with this portion:

echo "<div class='photoThumb'>";
."<a href='$row[\'filepath\']$row[\'filename\']'>";
."<img src='$row[\'filepath\']$row[\'filename\']' rel='lightbox[\'$aid\']' border='0' width='100px' />";
."</a><br/></div>";

The string concatenation operator is the period "."

String concatenation example:
If you want to put two strings together.

$str1 = "first string";
$str2 = "second stirng";

You do so as such:

$str3 = "first string"."second string";

As you have it you're doing:

$str3 = "first string";."second string";

So the semicolon ";" would cause a parse error.

Note: I have the two strings on one line, but PHP doesn't care if it on one line or two lines etc.

I'd recommend looking into string concatenation a bit more. PHP offers many ways to concatenate strings as well as strings and variables.
http://php.net/manual/en/language.operators.string.php

An example of the code formatted correctly:

echo "<div class='photoThumb'>"
   ."<a href='".$row['filepath'].$row['filename']."'>"
   ."<img src='".$row['filepath'].$row['filename']."' rel='lightbox['".$aid."']' border='0' width='100px' />"
   ."</a><br/></div>";

SQL Injection note:

In following piece of code:

$aid = $_GET['aid'];
$select = ("SELECT * FROM 'pictures' WHERE 'aid' = '$aid'");

You'll want to escape any variable you place in an SQL query. Otherwise someone could modify the query in a way you didn't intend.

Example:

$aid = mysql_real_escape_string($_GET['aid'], $con);
$select = ("SELECT * FROM 'pictures' WHERE 'aid' = '$aid'");

See: http://php.net/manual/en/function.mysql-real-escape-string.php

or probably better as I'm assuming aid is has a column type of integer? Note that strings are very different then integers for a lot of operators. http://dev.mysql.com/doc/refman/5.1/en/comparison-operators.html

$aid = intval($_GET['aid']);
$select = ("SELECT * FROM 'pictures' WHERE 'aid' = $aid");

So it is good to keep integers as integers, and not quote them.

$select = ("SELECT * FROM pictures WHERE aid = '$aid' ");

Single quotes are not needed around your table and column names. There are times when tick marks are used but in most cases you will not need them.

$str3 = "first string"."second string";

So are you saying I should do this:

$filename = "$row['filepath']"."$row['filename']";
..
..
echo '<img src"$filename"..';
$select = ("SELECT * FROM pictures WHERE aid = '$aid' ");

Single quotes are not needed around your table and column names. There are times when tick marks are used but in most cases you will not need them.

now I get nothing... not even an error. I even double checked my db table setup to make sure I got all the table names and row names correct and they all seem to be fine.
I am super stumped on what could be the problem.

I suspect that you are just not getting any results back from your query. I would echo $aid and make sure it is holding a value and work from there.

Also, I should allow D-E to answer but

$filename = "$row['filepath']"."$row['filename']";

is incorrect.

Should be:

$filename = "$row['filepath'].$row['filename']";

Concatenate is merely a way of saying "to join." The period in this case is the operator that is doing the joining. You are joining two strings to create a single string out of them. So, like any other string you only need your opening and closing quotes. "." would be incorrect.

I suspect that you are just not getting any results back from your query. I would echo $aid and make sure it is holding a value and work from there.

Also, I should allow D-E to answer but

$filename = "$row['filepath']"."$row['filename']";

is incorrect.

Should be:

$filename = "$row['filepath'].$row['filename']";

Concatenate is merely a way of saying "to join." The period in this case is the operator that is doing the joining. You are joining two strings to create a single string out of them. So, like any other string you only need your opening and closing quotes. "." would be incorrect.

$filename = "$row['filepath']"."$row['filename']";

Is fine.

It could also be:

$filename = "$row['filepath']$row['filename']";

or

$filename = $row['filepath'].$row['filename'];

Double quotes enclose strings. However, the string is then parsed again by PHP before the output is taken as the final stirng.

So if you have anything that "looks like" a variable, PHP will try and make it a variable.

So even if you had "sdofjsodfj$sdojsdoifjsd" PHP would try convert $sdojsdoifjsd to it's value because it see's a $ in there.

With single quotes, the string is taken literally. So 'sdofjsodfj$sdojsdoifjsd' is taken as 'sdofjsodfj$sdojsdoifjsd'.

I stand corrected, thank you D-E :) I suppose I need to do a little brushing up.

Now I'm the one that is a bit confused. I taught myself a lot of bad habits along the way so I suppose perhaps I'm doing something incorrectly here but

$filename = "$row['filepath']$row['filename']";
$filename = "$row['filepath']"."$row['filename']";

both throw an error for me.

ok so I did it two different ways and I will continue to mess with it to try and get some results.
1.

$aid = mysql_real_escape_string($_GET['aid'], $con);
$select = ("SELECT * FROM pictures WHERE aid = '$aid' ");
$result = mysql_query($select) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
      $filename = $row['filepath'].$row['filename'];
      echo '<div class="photoThumb">
      <a href="$filename">
      <img src="$filename" rel="lightbox[\'$aid\']" border="0" width="100px" />
      </a><br/></div>';
}
mysql_close($con); ?>

2.

$aid = mysql_real_escape_string($_GET['aid'], $con);
$select = ("SELECT * FROM pictures WHERE aid = '$aid' ");
$result = mysql_query($select) or die(mysql_error());
$filename = $row['filepath'].$row['filename'];
while($row = mysql_fetch_array($result)) {
      echo '<div class="photoThumb">
      <a href="$filename">
      <img src="$filename" rel="lightbox[\'$aid\']" border="0" width="100px" />
      </a><br/></div>';
}
mysql_close($con); ?>

Would this code (the stuff we have been working on to get the thumbnails to display) being in an include file have anything to do with it not working?

No, that shouldn't have anything to do with it. Did you try to echo the variable "$aid" to ensure it is holding a value and you are not just getting an empty result set back from your query?

yeah, that might be an empty result unless I am doing this wrong. I also did that two different ways.

1.

$aid = mysql_real_escape_string($_GET['aid'], $con);
$select = ("SELECT * FROM pictures WHERE aid = '$aid' ");
$result = mysql_query($select) or die(mysql_error());
//$filename = "$row['filepath'].$row['filename']";

while($row = mysql_fetch_array($result)) {	
//tried both these ways also
echo "$aid";
echo ".$aid.";
echo "<div class='photoThumb'>"
   ."<a href='".$row['filepath'].$row['filename']."'>"
   ."<img src='".$row['filepath'].$row['filename']."' rel='lightbox['".$aid."']' border='0' width='100px' />"
   ."</a><br/></div>";
}

2.

$aid = mysql_real_escape_string($_GET['aid'], $con);
//tried both these ways also
echo "$aid";
echo ".$aid.";

And I didnt get anything. But in my url it does say viewAlbum.php?aid=1
So I am a little stumped not sure how exactly it works.

echo $aid; would work just fine.

Try

$aid = mysql_real_escape_string($_GET['aid']);
die($aid);

I dont know what this is supposed to do:

$aid = mysql_real_escape_string($_GET['aid']);
die($aid);

but I didnt get anything. not even an error message.

Also, echo $aid; gave me nothing.

so does that mean I am left with the conclusion that PHP is not getting the aid variable that it needs?

What about writing come little code snippet that when the user clicks on the thumbnail on the photo.php page it creates like a little cookie that the next page then reads and then tells it to expire after it reads it? would something like that work?

What that's telling you is that the variable $aid has no value.

Using $_GET is quite simple. Here's an example just to be sure you are using it correctly.

Let's say you are have a page with a hyperlink on it that points to a different page:

somePage.php

<a href="someOtherPage.php?aid=1">Some other page</a>

Clicking the link will take you to the page (url) someOtherPage.php?aid=1 On someOtherPage.php

$value = $_GET['aid'];
echo $value; // <--- Will return the value of "1"

If you are sure that you are doing this correctly I would venture to guess that perhaps you are overwriting the variable of $aid somewhere on the page. In that case you should post the code for that entire page so we can have a look at it.

Well I am pretty sure that's exactly what I am doing...
viewAlbum.php

<?php
// create an array to set page-level variables
$page = array();
$page['title'] = 'title';
/* once the file is imported, the variables set above will become available to it */
// include the page header
include('includes/template/header.php');
?>
<!-- HTML content here -->
<div align="center">
<div class="wrapper_photo">
	<div class="photoWrap"> 
                //Here would be an include that displays the title of the album which also does not work and I removed to figure out later...      	
		<div align="left"></div>
                //here is the include file i am working with
		<?php include('http://www.domain.com/newsite/includes/album.php'); ?>
                //code thats inside the above include...
                <?PHP

$con = mysql_connect("...","...","...");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("...", $con);
$aid = mysql_real_escape_string($_GET['aid'], $con);
die($aid);
$select = ("SELECT * FROM pictures WHERE aid = '$aid' ");
$result = mysql_query($select) or die(mysql_error());
//$filename = "$row['filepath'].$row['filename']";
while($row = mysql_fetch_array($result)) {
echo $aid;
echo "<div class='photoThumb'>"
   ."<a href='".$row['filepath'].$row['filename']."'>"
   ."<img src='".$row['filepath'].$row['filename']."' rel='lightbox['".$aid."']' border='0' width='100px' />"
   ."</a><br/></div>";
}
mysql_close($con); ?>
//end include file code

	</div><!--close galleryWrap-->
</div><!--close wrapper_gallery-->
</div><!--clsoe center-->
<?php
// include the page footer
include('includes/template/footer.php');

?>

Here is the code to photo.php (this is the page that links to viewAlbum.php)
This page works fine and would leave it unless it is why the other page is not working.

<?php
// create an array to set page-level variables
$page = array();
$page['title'] = 'Title';
/* once the file is imported, the variables set above will become available to it */
// include the page header
include('includes/template/header.php');
?>
<!-- HTML content here -->
<div align="center">
<div class="wrapper_photo">
	<div class="photoWrap">	
	<div class="mainLeft">
		<div align="left"><img src="http://www.domain.com/newsite/images/contentboxes/photos_052909.png" alt="photos" border="0" /></div>
                //Include to the album thumbnail page.
		<?php include('http://www.domain.com/newsite/includes/photothumb2.php'); ?>
                //Code to the above include
               <?php
$con = mysql_connect("...","...","...");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("....", $con);
$result = mysql_query("SELECT * FROM photo_albums ORDER BY uptime DESC");
while($row = mysql_fetch_array($result))
  {
  echo '<div class="photoThumb">';
  echo '<a href="viewAlbum.php?aid='.$row['aid'].'"><img src="' . $row['filepath'];
  echo $row['filename'] . '"';
  echo 'border="0" /></a>';
  echo '<br/>';
  echo '<span class="thumbText"><a href="viewAlbum.php?aid='.$row['aid'].'">' . $row['stitle'];
  echo  '</a><br/>';
  echo $row['photog'];
  echo '</span>';
  echo "</div>";
  }
mysql_close($con);
?>
	</div><!--close mainLeft -->
	</div><!--close galleryWrap-->
</div><!--close wrapper_gallery-->
</div><!--clsoe center-->
<?php
// include the page footer
include('includes/template/footer.php');
?>

Looks okay to me. You'll want to remove die($aid); from viewAlbum.php ... that was just for testing purposes.

While looking at the viewAlbum page if the url is not displayed as viewAlbum.php?aid=1, $_GET will obviously have no effect. If the url does look like it should then having this as the very first line on the page

$value = $_GET['aid'];
echo $value;

should return the value of "1"

Another way to test your query (which I'm guessing works just fine) would be to temporarily replace your variable in the query:

$select = ("SELECT * FROM pictures WHERE aid = '1' ");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.