Jump to content

Recommended Posts

Hi i am still fairl new to php and sometime my logic seems of but iam having difficulty with pagination. Basically i have a function that searches the database base for user within a certain age range. Then based on that age range select data that will be paginated. My issue is the pagination is to depend on the second query as that is what i am concerned about. But i seem to be havong problems implementing this functionality. Any help or point in the right direction will be apprecited.

 

function search($gender,$agel,$ageh,$state){

 

$this->connect();

 

$searchq=mysql_query("SELECT * FROM users WHERE user_sex='$gender' AND user_state='$state'") or die(mysql_error());

$rows_search=mysql_num_rows($searchq);

 

if($rows_search>0){

 

for($i=0;$i<$rows_search;$i++){

 

$age=$this->GetAge(mysql_result($searchq,$i,"user_birthday"));

 

$mainimg=mysql_result($searchq,$i,"user_main_img");

 

if($age>=$agel && $age<=$ageh){

 

$mainimgq=mysql_query("SELECT ".$mainimg." FROM users WHERE user_id='".mysql_result($searchq,$i,"user_id")."'");

 

 

if(mysql_result($searchq,$i,"user_main_img")!=''){

$img="images/users/".mysql_result($searchq,$i,"user_id")."/".mysql_result($mainimgq,0,mysql_result($searchq,$i,"user_main_img"))."";

} else {

$img="images/noimage.png";

}

 

echo "<div style=\"float:left; width:180px; padding-right:30px;\"><div style=\"padding-bottom:5px;  text-align:center;\"><a href=\"user-details.php?user=".mysql_result($searchq,$i,"user_username")."\"><img src=\"$img\" width=\"150\" height=\"200\"></a></div><div style=\"width:180px; text-align:center; padding-bottom:20px; \"><a href=\"user-details.php?user=".mysql_result($searchq,$i,"user_username")."\" style=\"color:#838383; font-size:15px;\">".mysql_result($searchq,$i,"user_username")."</a></div></div>";

 

}

}

} else {

echo "There were no results based on your search criteria.";

}

}

 

Link to comment
https://forums.phpfreaks.com/topic/239759-php-pagination/
Share on other sites

Ok. There are a couple problems. TO preface the explanation, let me give you some advice. When debugging code, and specifically mysql query related bugs, it is best to make PHP as helpful as possible. By helpful, I mean it will tell you when an error is happening, and what the error is (and more information if able). For example, I assume the query giving you problems is:

$mainimgq=mysql_query("SELECT ".$mainimg." FROM users WHERE user_id='".mysql_result($searchq,$i,"user_id")."'");

 

PHP doesn't alert you of mysql errors unless you tell it to. In our case, the easiest way for php to tell us is to use an "or die()" statement. Note: or die() is fine for debugging, but when your product is live, it is imperative that you handle errors more gracefully.

 

So, we can use or die() like this

$mainimgq=mysql_query("SELECT ".$mainimg." FROM users WHERE user_id='".mysql_result($searchq,$i,"user_id")."'") or die(mysql_error());

What that will do is show the mysql error when the query fails. Now, we might also benefit from seeing the whole query as well as the error. we can do that by storing the query in a variable, and adding it to the die clause, like so

$query = "SELECT ".$mainimg." FROM users WHERE user_id='".mysql_result($searchq,$i,"user_id")."'"
$mainimgq=mysql_query($query); or die("There was a problem with the query: " . $query . "<br />Error: " . mysql_error());

 

 

Now, onto your problem. In your query you do

$mainimgq=mysql_query("SELECT ".$mainimg." FROM users WHERE user_id='".mysql_result($searchq,$i,"user_id")."'");

 

and you use $mainimg as a column name. This doesn't really make sense because this is a column value in the user table you get from your first select statement. I assume this is the name of the users image. I also assume you don't have column names that are based of the name of a users (presumably uploaded?) image (please correct me if i'm wrong)

 

I don't really understand what you are trying to accomplish here. From the looks of it you can do what you want with 1 query (your second query doesn't really make any sense at all, no offense). Can you explain what the code you posted is supposed to do? Also can you tell us what happens when you run this script? Blank page? Error?

Link to comment
https://forums.phpfreaks.com/topic/239759-php-pagination/#findComment-1231634
Share on other sites

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.