Substring in a where clause

Discussion in 'MySQL' started by schlogo, Jan 1, 2012.

  1. #1
    Hello all

    I have some problemes inserting a substring in a where clause. All the code works xith a simple where= . I am soinf something wring in the substring but i can't find out what. Any help is welcome .
    Tks !

    <?php 
    $cible=$_REQUEST['cible']; 
    mysql_connect("xxxxxxxxx", "root", "xxxxxxxxx") or die(mysql_error());
    mysql_select_db("xxxxxxxx");
    ?>
    
    <select name="city" id="city" style="width:auto; margin-right:10px;" onChange="getxxxxxx('xxxxxxxx.php?diagrefs='+this.value)" >    
    <?php    
    $req = "[B]SELECT * FROM xxxxxxxxxx WHERE SUBSTRING(field_xxxxxxx,0,2)=$cible[/B]";
    $result = mysql_query($req);
    while($row=mysql_fetch_array($result))
    { 
    echo '<option value="'.$row['xxx'].'">'.$row['xxxxxx'].' - '.$row['xxxxx'].' - '.$row['xxxx'].'</option>';      
    }    
    Code (markup):
    ?>
    </select>
     
    schlogo, Jan 1, 2012 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    $req = "SELECT * FROM xxxxxxxxxx WHERE ".SUBSTRING(field_xxxxxxx,0,2)."=$cible";
     
    jestep, Jan 3, 2012 IP
  3. schlogo

    schlogo Well-Known Member

    Messages:
    1,615
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    140
    #3
    Hello

    Thanks for your answer, i actually got the answer from another forum. My script works if I use ,1,2 in the substring. If i use ,0,2, it comes back empty. This point is actually explained black on white in the documentation. I should have RTFM :D
     
    schlogo, Jan 9, 2012 IP