Hellow evey body,

I have to use same sql query frequently on diferenet php scripts pages in a appliction, i want to put this query in a funtion to use it where i want but this funtion is not working at all. i am unable to figure out the problem, the code is as follow,

function PhotoListQuery ($user, $album, $imViewAuth){
{
$sql = "SELECT a.im_id, a.im_title, a.im_thumbnail, a.im_album_id, 
                         a.im_view_auth, DATE_FORMAT(a.im_date, '%d-%m-                         %Y') AS im_date, b.al_id, b.user_id 
             FROM  " . image . " a, " . album ." b  
           WHERE  b.user_id = $user AND  a.im_album_id = b.al_id AND
	        (a.im_view_auth = 'Show to every body' OR 
    		  a.im_view_auth = '$imViewAuth')";
if ($album != '') {
$sql .= "AND a.im_album_id = $album ";
}
$sql .= "ORDER BY a.im_title ";
}
return $sql;
}

please help....
thanks in advance

Just check again your query function...
Why to put 2 { (braces) next to function declaration...
Secondly after FROM: what is image and album.... if they are variables then put $ before their names, and if they are constants declared by define... then check if it is defined and used...

Also.. most important thing.... (where the actual problem is)
in the second $sql .= "AND ........"; part...
There is a problem....
In sql variable defined above.... it ends with OR a.im_view_auth = '$imViewAuth')";

After the ) you have put double quotes...
And in the next $sql.. you have started "AND without a space...

So when the query is actually made.. it results in an error because the compiler sees the sql query as

OR a.im_view_auth = '$imViewAuth')AND a.im_album_id=$album

Similarly for the last $sql, there is no space before ORDER BY...

So for both of the $sql.= , put a space before
i.e.
$sql.= " ORDER by ....";
and
$sql.= " AND ...... ";

I hope what i have said is clear...

function PhotoListQuery ($user, $album, $imViewAuth)
{
	$sql = "SELECT a.im_id, a.im_title, a.im_thumbnail, a.im_album_id, 
		a.im_view_auth, DATE_FORMAT(a.im_date, '%d-%m-%Y') AS im_date, 
		b.al_id, b.user_id FROM  " . image . " a, " . album ." b  
           	WHERE  b.user_id = $user AND  a.im_album_id = b.al_id AND
	        (a.im_view_auth = 'Show to every body' OR a.im_view_auth = '$imViewAuth')";

	if ($album != '') 
	{
		$sql .= " AND a.im_album_id = $album ";
	}
	
	$sql .= " ORDER BY a.im_title ";


	return $sql;
}

Thank you sikka_varun for your response, when i use the same query in regular scrips, it runs fine, but when it is used in finction it does not response without any error message.

However i tested your suggested query, but it also produce no response just as like the first one...

I hope you are using the query in the following way:

<?php

function PhotoListQuery ($user, $album, $imViewAuth)
{
    ........
}

$result = mysql_query(PhotoListQuery('username','albumname','All');
$count_rows=mysql_num_rows($result);

// Rest of the code here...

once again thank you sikka_varun for your valueable solution. actually i was using $sql return value of function instead of function its self. $result = mysql_query(PhotoListQuery'username','albumname','All'); now it is working very much fine..
once again thank you.....

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.