Jump to content

[SOLVED] mysql search


ted_chou12

Recommended Posts

I have three input values, username, gender, photo, here i am plugging in the $user into the gender
[code]<?php
if ($gender == "Boy"){
$read = mysql_query("SELECT username FROM userinfo WHERE gender='Boy' AND username='$user'") or die(mysql_error());
$row = mysql_fetch_array($read); $user = $row['username'];}

if ($gender == "Girl"){
$read = mysql_query("SELECT username FROM userinfo WHERE gender='Girl' AND username='$user") or die(mysql_error());
$row = mysql_fetch_array($read); $user = $row['username'];}
?>[/code]
This is the photo, but i dont know how to plug the username filtered from the last one in
[code]<?php
if ($photo == "Yes"){
$read = mysql_query("SELECT username FROM usercus WHERE photo!='' AND username='$user'") or die(mysql_error());//I dont know if the varialbe $user is correct.
$row = mysql_fetch_array($read); $user = $row['username'];}

if ($photo == "No"){
$read = mysql_query("SELECT username FROM usercus WHERE photo='' AND username='$row'") or die(mysql_error());//same as for this one.
$row = mysql_fetch_array($read); $user = $row['username'];}

Thanks Ted
[/code]
Link to comment
https://forums.phpfreaks.com/topic/32312-solved-mysql-search/
Share on other sites

Acually, I dont know what I have done wrong at all, this is all of it:
[code]
<?php
if(isset($_POST['searchuser'])){
$user = $_POST['search_query'];
$age = $_POST['age'];
$gender = $_POST['gender'];
$photo = $_POST['photo'];
$datetime = date("Y-m-d");

$ages = array('no preference' => '','under 20' => '1','20-29' => '2','30-39' => '3','40-49' => '4','50-59' => '5','60 above' => '6',);

$age_select = "";
foreach ($ages as $range => $year) {if ($range == $age) {$age_select = $year;}}

// connect to mysql below
require("../mysqlconnection.php");

if ($photo == "Yes"){
$read = mysql_query("SELECT username FROM usercus WHERE photo!='' AND username like '%$user%'") or die(mysql_error());
$row = mysql_fetch_array($read);}

if ($photo == "No"){
$read = mysql_query("SELECT username FROM usercus WHERE photo='' AND username like '%$user%'") or die(mysql_error());
$row = mysql_fetch_array($read); $user = $row['username'];}

if ($gender == "Boy"){
$read = mysql_query("SELECT username FROM userinfo WHERE gender='Boy' AND username like '%$user%'") or die(mysql_error());
$row = mysql_fetch_array($read); $user = $row['username'];}

if ($gender == "Girl"){
$read = mysql_query("SELECT username FROM userinfo WHERE gender='Girl' AND username like '%$user%'") or die(mysql_error());
$row = mysql_fetch_array($read); $user = $row['username'];}

if($age_select != ""){
$read = mysql_query("SELECT username FROM userinfo WHERE age like '$age_select%' AND username like '%$user%'") or die(mysql_error());
$row = mysql_fetch_array($read); $user = $row['username'];}
?>[/code]
The weird thing is that I tried for 30~39 range, it appears the 30s range as well as the 60s range, but when i searched for the 60s range, it appears only the 60s range...
Can anyone help me with this, btw, the gender doesnt work at all... photo i havent try yet.
Thanks
Ted
Link to comment
https://forums.phpfreaks.com/topic/32312-solved-mysql-search/#findComment-150008
Share on other sites

I want to have a muti categorized search, so i can get a targeted group, like male 20~29, or female 50~59, but I dont know how to put the two categories together. My search doesnt work, I wish anyone could have a look at it and give me some suggestions.
Ted
Link to comment
https://forums.phpfreaks.com/topic/32312-solved-mysql-search/#findComment-150377
Share on other sites

Ted condense your code. You're repeating the same thing over and over again. The only thing that you need to build is the where clause of the query

[code]
<?php
$where = '';

if ($photo == "No"){
$where .= 'photo='' AND ';
}

if($age_select != ""){
$where .= 'age like '. $age_select .'% AND ';
}

$read = mysql_query("SELECT username FROM userinfo WHERE ". $where ."username like '%$user%'") or die(mysql_error());
$row = mysql_fetch_array($read); $user = $row['username'];
?>
[/code]

if you do it that way, you could buld the where clause with all of those values included
Link to comment
https://forums.phpfreaks.com/topic/32312-solved-mysql-search/#findComment-150568
Share on other sites

thanks, but I dont quite understand, what are the "." beside  $where . << where for? and I think there is a syntax mistake inside, because my page doesnt show anything ( i cant configure it to make it show error.) Thanks a lot for the help by the way.
Ted
Link to comment
https://forums.phpfreaks.com/topic/32312-solved-mysql-search/#findComment-150601
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.