where子句中的多个列查询mysql

时间:2013-03-04 16:04:49

标签: php mysql where-clause multiple-columns

selectbox with multiple columns from mysql database

以下代码是zgr024的获奖代码

       <?php 
    include '../config.php';
    $sql = "SELECT * FROM megabase";  
    $resultaat = mysql_query($sql) or die (mysql_error());  
    $domains = array();
    while ($row = mysql_fetch_array($resultaat))       
    { 
       if (!empty($row['domeinnaam1'])) $domains[] = $row['domeinnaam1'];
       if (!empty($row['domeinnaam2'])) $domains[] = $row['domeinnaam2'];
    }
?>

   <select size="1" name="domeinnaam">
<?php
    foreach ($domains as $domain)
    {
        echo "<option>$domain</option>";
    } 
?>
    </select>

但是这个代码导致下一页的预期错误,我有以下代码。

    <?php 

     //MySQL Database Connect
     include 'config.php';

    $domeinnaam=$_POST['domeinnaam']; 

    $data = 'SELECT * FROM megabase WHERE domeinnaam = "'.$domeinnaam.'"'; 
      $query = mysql_query($data) or die("Couldn't execute query. ". mysql_error()); 
      $data2 = mysql_fetch_array($query); 


    ?> 

如何更改最后一个代码以使用第一个代码:

我试过了:

        <?php 

         //MySQL Database Connect
         include 'config.php';

        $domeinnaam=$_POST['domeinnaam'];
       $domeinnaamquerry='domeinnaam1'.'domeinnaam2'.'domeinnaam3'.'domeinnaam4'.'domeinnaam5'.'do meinnaam6'.'domeinnaam7'.'domeinnaam8'.'domeinnaam9'.'domeinnaam10';

        $data = 'SELECT * FROM megabase WHERE $domeinnaamquerry = "'.$domeinnaam.'"'; 
          $query = mysql_query($data) or die("Couldn't execute query. ".             mysql_error()); 
          $data2 = mysql_fetch_array($query); 


        ?> 

但我收到以下错误: 无法执行查询。未知专栏&#39; $ domeinnaamquerry&#39;在&#39; where子句&#39;

Okey现在正在运作它很草率但是感谢大家的帮助,这对你有所帮助。并感谢警告。

这是代码

     <?php 

      //MySQL Database Connect
      include 'config.php';

     $domeinnaam=$_POST['domeinnaam']; 

     $data = 'SELECT * FROM megabase WHERE domeinnaam1="'.$domeinnaam.'" OR domeinnaam2="'.$domeinnaam.'" OR domeinnaam3="'.$domeinnaam.'" OR domeinnaam4="'.$domeinnaam.'" OR domeinnaam5="'.$domeinnaam.'" OR domeinnaam6="'.$domeinnaam.'" OR domeinnaam7="'.$domeinnaam.'" OR domeinnaam8="'.$domeinnaam.'" OR domeinnaam9="'.$domeinnaam.'" OR domeinnaam10="'.$domeinnaam.'"    '; 
       $query = mysql_query($data) or die("Couldn't execute query. ". mysql_error()); 
       $data2 = mysql_fetch_array($query); 


     ?> 

2 个答案:

答案 0 :(得分:1)

尝试

<?php 

    //MySQL Database Connect
    include 'config.php';

    $domeinnaam=$_POST['domeinnaam']; 

    $data = "SELECT * FROM megabase WHERE domeinnaam1 = '$domeinnaam' OR domeinnaam2 = '$domeinnaam' "; 
    $query = mysql_query($data) or die("Couldn't execute query. ". mysql_error()); 
    $data2 = mysql_fetch_array($query); 

?>

答案 1 :(得分:0)

如果要将变量解释为PHP中的值,则不能使用单引号。

$foo = 'World';

echo 'Hello $foo'; // Hello $foo
echo "Hello $foo"; // Hello World

因此,你想要

$data = "SELECT * FROM megabase WHERE $domeinnaamquerry = '".$domeinnaam."'"; 

虽然阅读了thisthis,但了解为什么 真的 不希望这样。