Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to search in a database with variable positions. The variables are created here:

&numbers  //= user input
&naar    // = user input

$number = range($numbers+1, $naar -1); //define the range between the inputs
foreach ($number as $key=>$val){
$number[$key] = $letter.$val;}         //define the array
$string = implode (' ',$number);       // make a string from the array

This works fine. The output is a string that contains a minimum of 0 outputs and a maximun of 7 outputs. For example: A2 A3 A4 A5

I want the database to search if something is at one of the generated positions. Ive got this already:

  $query="select chess_id from stelling where positie=\"".$number."\"";
  $result = mysql_query($query, $connection);
  $spring = 0;
    if(mysql_num_rows($result)>0)                
        {
        $spring = mysql_result($result, 0);
        }
    echo "$spring";

With this code only the last generated $string output will be checked. How can i let the database check all generated string code? For example:

$string = `A2 A3 A4 A5`
$query="select chess_id from stelling where positie=\"".$number."\"";

will only check A5

sample rows from table:

wt,A1 wp,A2 wl,A3 wq,A4

share|improve this question
    
Can you post some sample rows from the table, and which ones should be selected? –  Barmar Jul 2 '13 at 8:22
1  
I suspect what you really want is WHERE position IN ("A2", "A3", ...). –  Barmar Jul 2 '13 at 8:23
    
Or WHERE position BETWEEN "A2" AND "A5". –  Barmar Jul 2 '13 at 8:23
    
i edit the code –  danny Jul 2 '13 at 8:30
    
Do you understand that positie = "A2 A3 A4 A5" compares the entire string to the column value, it doesn't look for partial matches? This is beginner stuff. –  Barmar Jul 2 '13 at 8:34
show 1 more comment

1 Answer

Well I am not shure what exactly is your problem but why don't you use IN statement ?

$string = '(`' . implode('`, `',$number) . '`)';   
$query="select chess_id from stelling where positie IN {$string}";
share|improve this answer
    
I get the following warning: Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given. I changed my line of code with yours. thanks –  danny Jul 2 '13 at 8:58
    
maybe there are no matches in DB. also check the value of $string before anything –  vodich Jul 2 '13 at 9:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.