Cannot get PHP search & results to work - code is crashing Apache!!!

I'm trying to code a PHP/MySQL driven results bit (see snippet).

Take aware the while() and the code works, Apache doesn't crash (lock up - needing a restart) and I can see 1 image returned out of 2. Running the same SQL query on the DB via phpMyAdmin and it returns 2 results.

Can anyone tell me where I'm going wrong please?

All I want is the results listed one under another. There are only 5 images max per request, so no need for LIMIT in the query.
<?php
 
# Fetch images
while($sql_imgs = mysql_fetch_row(mysql_query('SELECT PhotoTitle, PhotoId FROM Photos WHERE PhotoPropertyId='.$this->request_id.' ORDER BY PhotoOrder ASC'))) {
    $this->img_id       =    $sql_imgs['PhotoId'];
}
 
?>

Open in new window

DReade83Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
Try this:
<?php
 
# Fetch images
$r = mysql_query('SELECT PhotoTitle, PhotoId FROM Photos WHERE PhotoPropertyId=' . $this> request_id.' ORDER BY PhotoOrder ASC') or die( mysql_error() );
while($sql_imgs = mysql_fetch_row($r)) {
    $this->img_id       =    $sql_imgs['PhotoId'];
}
 
?>
0
RurneCommented:
Two things I'm noticing off the bat.

Line 4: bad practice, though technically not syntactically invalid.  You should separate the mysql_query and the mysql_fetch call, especially in a while() loop.  Depending on the state of the connection, you can end up losing your result pointer and re-executing the query.

Line 5:  you're referencing $sql_imgs['PhotoID'], but you're using mysql_fetch_row.  Arrays returned by mysql_fetch_row only use numeric indices.  If you want an associative array that uses field names instead of numerals, use mysql_fetch_assoc() or mysql_fetch_array() on Line 4.

If you're simply getting a blank page, I'd recommend adding:

ini_set('display_errors', 1);
error_reporting(E_ALL);

at line 2, to give you all error output to the screen, and you can remove these lines when you're done debugging.
0
hieloCommented:
If you leave the SELECT in the while condition, it will forever be making a new query and mysql_fetch_row will return the first record all the time, as in in forever!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

nplibCommented:
change this
while($sql_imgs = mysql_fetch_row(mysql_query('SELECT PhotoTitle, PhotoId FROM Photos WHERE PhotoPropertyId='.$this->request_id.' ORDER BY PhotoOrder ASC'))) {
    $this->img_id       =    $sql_imgs['PhotoId'];
}
to
$rs = mysql_query('SELECT PhotoTitle, PhotoId FROM Photos WHERE PhotoPropertyId='.$this->request_id.' ORDER BY PhotoOrder ASC')
while($sql_imgs = mysql_fetch_row($rs)) {
    $this->img_id       =    $sql_imgs['PhotoId'];
}
0
hieloCommented:
Did you manage to resolve this? Notice that this:
<?php
 
# Fetch images
$r = mysql_query('SELECT PhotoTitle, PhotoId FROM Photos WHERE PhotoPropertyId=' . $this-> request_id.' ORDER BY PhotoOrder ASC') or die( mysql_error() );
while($sql_imgs = mysql_fetch_row($r)) {
    $this->img_id       =    $sql_imgs['PhotoId'];
}
?>
is the same thing nplib is suggesting above. Read my second post to understand why it was going into an infinite loop and hence cause apache to crash.
0
DReade83Author Commented:
Still not working, but Apache doesn't crash now. See latest snippet...
<?php
			# Fetch images
			$result			=	mysql_query('SELECT PhotoTitle, PhotoId FROM Photos WHERE PhotoPropertyId='.$this->property_id.' ORDER BY PhotoOrder ASC');
			$this->img_loop	=	0;
			while($img_results = mysql_fetch_row($result)) {
				$this->img_loop++;
				$this->img_id		=	$img_results['PhotoId'];
				$this->thumb_img	=	'<td class="images"><div class="spacer">&nbsp;</div><table border="0" cellspacing="0" cellpadding="0"><tr><td class="img" style="background: url(/requests/'.$this->request_id.'/images/'.$this->img_id.'.jpg)">&nbsp;</td></tr></table>';
			}
?>

Open in new window

0
nplibCommented:
sorry @heilo,

this is the result of opening a question, but waiting so long to answer.
0
nplibCommented:
can you define not working.
0
DReade83Author Commented:
Sorry, ignore that snippet, try this one instead:
<?php
# Fetch images
    $result = mysql_query('SELECT PhotoTitle, PhotoId FROM Photos WHERE PhotoRequestId='.$this->request_id.' ORDER BY PhotoOrder ASC');
    while($img_results = mysql_fetch_row($result)) {
        $this->img_id      = $img_results['PhotoId'];
        $this->thumb_img   = '<td class="images"><div class="spacer">&nbsp;</div><table border="0" cellspacing="0" cellpadding="0"><tr><td class="img" style="background: url(/requests/'.$this->request_id.'/images/'.$this->img_id.'.jpg)">&nbsp;</td></tr></table>';
    }
?>

Open in new window

0
RurneCommented:
You're still using mysql_fetch_row().  Change Line 5 to:

while($img_results = mysql_fetch_assoc($result)) {
0
DReade83Author Commented:
Yeah, sorry. Basically it's still only returning 1 result of 2, but Apache isn't crashing now. I've included the HTML I'm using ($this->thumb_img) in the latest snippet.

Many thanks.
0
DReade83Author Commented:
Sorry Rurne, I got lost along the way... Snippet updated, same problem though, 1 of 2 results only. :-(
<?php
# Fetch images
    $result = mysql_query('SELECT PhotoTitle, PhotoId FROM Photos WHERE PhotoRequestId='.$this->request_id.' ORDER BY PhotoOrder ASC');
    while($img_results = mysql_fetch_assoc($result)) {
        $this->img_id      = $img_results['PhotoId'];
        $this->thumb_img   = '<td class="images"><div class="spacer">&nbsp;</div><table border="0" cellspacing="0" cellpadding="0"><tr><td class="img" style="background: url(/requests/'.$this->request_id.'/images/'.$this->img_id.'.jpg)">&nbsp;</td></tr></table>';
    }
?>

Open in new window

0
nplibCommented:
are you sure there is more then one image that has the same PhotoRequestId value
0
RurneCommented:
Um... yeah.


$this->thumb_img is getting clobbered on each loop iteration.  Try this:
<?php
                        # Fetch images
                        %this->thumb_img        =  '';
                        $result                 =       mysql_query('SELECT PhotoTitle, PhotoId FROM Photos WHERE PhotoPropertyId='.$this->property_id.' ORDER BY PhotoOrder ASC');
                        $this->img_loop =       0;
                        while($img_results = mysql_fetch_row($result)) {
                                $this->img_loop++;
                                $this->img_id           =       $img_results['PhotoId'];
                                $this->thumb_img.       =       '<td class="images"><div class="spacer">&nbsp;</div><table border="0" cellspacing="0" cellpadding="0"><tr><td class="img" style="background: url(/requests/'.$this->request_id.'/images/'.$this->img_id.'.jpg)">&nbsp;</td></tr></table>';
                        }
?>

Open in new window

0
nplibCommented:
$this->img_id = $img_results['PhotoId'];
$this->thumb_img   = '<td class="images"><div class="spacer">&nbsp;</div><table border="0" cellspacing="0" cellpadding="0"><tr><td class="img" style="background: url(/requests/'.$this->request_id.'/images/'.$this->img_id.'.jpg)">&nbsp;</td></tr></table>';

will only retain the last row of values.
try

$this->img_id .= $img_results['PhotoId'];
$this->thumb_img   .= '<td class="images"><div class="spacer">&nbsp;</div><table border="0" cellspacing="0" cellpadding="0"><tr><td class="img" style="background: url(/requests/'.$this->request_id.'/images/'.$this->img_id.'.jpg)">&nbsp;</td></tr></table>';
0
RurneCommented:
gah.  I just goofed on the concat operator.  Change line 9 of my sample to:

                               $this->thumb_img       .=       '<td class="images"><div class="spacer">&nbsp;</div><table border="0" cellspacing="0" cellpadding="0"><tr><td class="img" style="background: url(/requests/'.$this->request_id.'/images/'.$this->img_id.'.jpg)">&nbsp;</td></tr></table>';

Open in new window

0
hieloCommented:
No, not one of two. You get all  the records. The problem is that this:
$this->thumb_img = "..."
gets set every time the while executes. So you are actually seeing only the last record! True or True?
0
hieloCommented:
It looks like an array is what you need:
<?php
# Fetch images
    $result = mysql_query('SELECT PhotoTitle, PhotoId FROM Photos WHERE PhotoRequestId='.$this->request_id.' ORDER BY PhotoOrder ASC');
    while($img_results = mysql_fetch_assoc($result)) {
        $this->img_id      = $img_results['PhotoId'];
        $this->thumb_img[]   = '<td class="images"><div class="spacer">&nbsp;</div><table border="0" cellspacing="0" cellpadding="0"><tr><td class="img" style="background: url(/requests/'.$this->request_id.'/images/'.$this->img_id.'.jpg)">&nbsp;</td></tr></table>';
    }
?>

echo ( $obj->thum_img[0]);//would print the first thumbnail in the object.
0
DReade83Author Commented:
It seems adding the . as nplib suggested has resulted in both appearing.

Had to change them from background images to foreground images too so I could do what I need to do with them.

Many thanks guys for all your help. :-)
0
hieloCommented:
Technically speaking, the Accepted solution was not the solution to the question you asked. Your crashing was due to infinite looping. You are killing my score card!
0
DReade83Author Commented:
Sorry for the points. I did it wrong. I've contacted a mod to get them adjusted. :-)
0
DReade83Author Commented:
Brilliant feedback, very helpful, a very big thank you! :-D
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.