Jump to content

MySQL syntax help needed


superseba888

Recommended Posts

Hello,

 

I'm new with PHP/MySQL, and got a problem with a SQL syntax :

 

Code :

 

  $sql = "SELECT AUN, denomination, ville FROM aun WHERE loc = $loc AND cp = $cp AND ville = $ville ORDER BY ville"; 

 

 

When the 3 variables ($loc, $cp, $ville) are filled (!= null), no problems.

 

But when there is one missing (when it is == null), I've got an SQL error.

 

I would like to find a way, that the SQL query just "ignore" the empty variables (those == null).

 

Any suggestions about the ways to solve this problem ?

 

Thank you in advance !

Vince

Link to comment
Share on other sites

if (isset($variable))

{

$condition[]=$variable;

}

if (isset($variable2))

{

$condition[]=$variable2;//edit this

}

if (isset($variable3))

{

$condition[]=$variable3;

}

if (count($condition)>1)

$value=implode('and',$condition);

$sql = "SELECT AUN, denomination, ville FROM aun WHERE loc = $loc $value ORDER BY ville";

 

long but thats the logic but wait ill make another example for you

Link to comment
Share on other sites

if (isset($variable))

{

$condition.='and value='$variable;

}

if (isset($variable2))

{

$condition.='and value='$variable;

}

if (isset($variable3))

{

$condition.='and value='$variable;

}

 

$sql = "SELECT AUN, denomination, ville FROM aun WHERE loc = $loc $condition ORDER BY ville";

 

pls try to debug if threres error

Link to comment
Share on other sites

Thanks this looks great.

 

This could be suitable if the conditions are several conditions for "loc". And not if there are some other conditions like cp = $cp and ville = $ville...

 

However, you helped me to understand some php functions (isset, implode, and so on ...)

 

Many thanks !

 

 

 

if (isset($variable))

{

$condition[]=$variable;

}

if (isset($variable2))

{

$condition[]=$variable2;//edit this

}

if (isset($variable3))

{

$condition[]=$variable3;

}

if (count($condition)>1)

$value=implode('and',$condition);

$sql = "SELECT AUN, denomination, ville FROM aun WHERE loc = $loc $value ORDER BY ville";

 

long but thats the logic but wait ill make another example for you

Link to comment
Share on other sites

Could be this :

  $condition.='and value='$variable;

written like this :

  $condition = $condition.'and value='$variable;

  (a sort of $condition + 'and value='$variable)

 

If yes, I think I've got the solution to my problem ...

 

May I please ask you to advise ?

 

Thanks,

 

 

if (isset($variable))

{

$condition.='and value='$variable;

}

if (isset($variable2))

{

$condition.='and value='$variable;

}

if (isset($variable3))

{

$condition.='and value='$variable;

}

 

$sql = "SELECT AUN, denomination, ville FROM aun WHERE loc = $loc $condition ORDER BY ville";

 

pls try to debug if threres error

Link to comment
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.