Jump to content

php inside of a mysql query


ohdang888

Recommended Posts

can this be done?

 

i have 6 search fields, and there might be 1 filled, or all 6 might be filled.

 

So i want to search by how many fields they've entered. Can this be done in one query?

 

example:

<?php
$result = mysql_query("SELECT * FROM `common_traits WHERE if(isset($genus)){genus='$genus'} AND if(isset($var2)){genus='$var2} ") or die(mysql_error());
?>

 

thanks!

Link to comment
https://forums.phpfreaks.com/topic/110225-php-inside-of-a-mysql-query/
Share on other sites

no not this way u can use it.

what about this

$where = '';
if(isset($var1)) {
   $where? $where .= " AND var1 = '$var1' " : $where = " var1 = '$var1' "
}
if(isset($var2)) {
   $where? $where .= " AND var2 = '$var2' " : $where = " var2 = '$var2' "
}

if(isset($var3)) {
   $where? $where .= " AND var3 = '$var3' " : $where = " var3 = '$var3' "
}
.
.//so on
.

$result = mysql_query("SELECT * FROM `common_traits WHERE $where ") or die(mysql_error());

hope this will help u

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.