从mysql中提取数据

时间:2011-08-02 16:15:45

标签: mysql

  1. 我正在尝试从2个表中提取数据,当我在$ query中添加它时,当我点击我的链接ex时,结果都搞砸了。 /leads.php?contactstatus=Hot

  2. 当我只有“Hot”“Warm”“Cold”作为表格行contactstatus中的选项时,我如何默认显示所有的contactstatus类型?

    <?
    mysql_connect ("xxxx","xxxx","xxxx") or die ('Error: ' .mysql_error());
    mysql_select_db ("xxxx");
    
    if(isset($_GET['contactstatus'])
    && in_array($_GET['contactstatus'], array('Hot', 'Warm', 'Cold'))){     
    $status = $_GET['contactstatus']; 
    } 
    else {      
    $status = ''; // what do I put here so by default users for all contactstatus types show in results 
    }  
    
     $query = "SELECT * FROM contacts,contacttodo WHERE contacts.contactstatus =    `'".$status."' OR contacttodo.type = 'Appointment'";`  
    
    $result=mysql_query($query);
    
    while($row = mysql_fetch_array($result)){
    
    ?>
    
  3. 更新:

    这有效:

    <div class="nav">
    <table width="100%">
    <tr>
    <td>
    <li><a href="/dbs/a.php?type=Appointment">Appointments</a></li> 
    <li><a href="/dbs/a.php?contactstatus=Hot">Hot</a></li> 
    <li><a href="/dbs/a.php?contactstatus=Warm">Warm</a></li> 
    <li><a href="/dbs/a.php?contactstatus=Cold">Cold</a></li>
    </td>
    </tr>
    </table>
    </div>
    
    $status = ''; 
    $todotype = '';  
    
    if(isset($_GET['contactstatus']) 
    && in_array($_GET['contactstatus'], array('Hot', 'Warm', 'Cold')))
    {     
    $status = $_GET['contactstatus'];     
     $query = "SELECT * FROM contacts,contacttodo,contactnotes WHERE contacts.ID = contacttodo.contacts_id = contactnotes.contacts_id AND contacts.contactstatus = '".$status."' ORDER BY contacts.firstname ASC"; 
    }  
    
    if(isset($_GET['type']) 
    && in_array($_GET['type'], array('Appointment', 'Email', 'Call')))
    {     
    $todotype = $_GET['type'];     
    $query = "SELECT * FROM contacts,contacttodo,contactnotes WHERE contacts.ID = contacttodo.contacts_id = contactnotes.contacts_id AND contacttodo.type = '".$todotype."' ORDER BY contacts.firstname ASC"; 
    } 
    $result=mysql_query($query);
    
    while ($row = mysql_fetch_array($result)) {
    
    <div id="contact-results">
    <table width="100%" cellspacing="0" cellpadding="0" class="contact-results"> 
    <tr>
    <td align="left" width="15%"><a href="/dbs/editcontact.php?ID=<? echo $row['ID']; ?>"><strong><? echo $row['firstname']; ?> <? echo $row['lastname']; ?></strong></a></td>
    <td align="left" width="5%"><? echo $row['contactstatus']; ?></td>
    <td align="left" width="15%"><? echo $row['contacttype']; ?></td>
    </tr>
    </table>
    </div>
    <?
    }
    mysql_close();
    ?>
    

    默认情况下,如何显示所有“热”'暖''冷'潜在客户?

4 个答案:

答案 0 :(得分:0)

您没有连接联系人和contacttodo表的连接条件,因此您将获得一个交叉产品。你需要这样的东西:

SELECT * 
    FROM contacts c
        INNER JOIN contacttodo ctd
            ON c.contact_id = ctd.contact_id /* Obviously, I guessed on the column names */
    WHERE c.contactstatus = '".$status."' 
        OR ctd.type = 'Appointment'

答案 1 :(得分:0)

if(isset($_GET['contactstatus']) && in_array($_GET['contactstatus'], array('Hot', 'Warm', 'Cold')))
{
  $status = contacts.contactstatus = '".$_GET['contactstatus']."' ; 
} 
else 
{       
 $status = '0'; 
}

$query = "SELECT * FROM contacts,contacttodo WHERE contacttodo.id =  contacts.id AND (  ".$status." OR contacttodo.type = 'Appointment')";

需要针对您的结构更新contacttodo.id = contacts.id

答案 2 :(得分:0)

在您对表格定义的反馈后,我正在更新我的答案。

$query = "SELECT DISTINCT t2.type FROM contacts t1, contacttodo t2 WHERE t1.id=t2.contacts_id AND (t1.contactstatus IN ('Hot', 'Warm', 'Cold') OR t2.type = 'Appointment')";

答案 3 :(得分:0)

if(isset($_GET['contactstatus']) && in_array($_GET['contactstatus'], array('Hot', 'Warm', 'Cold'))){
    $status = "'" . $_GET['contactstatus'] . "'";
} else {
    $status = 'contact.contactstatus';
    // since contact.contactstatus ALWAYS equals contact.contactstatus
    // I wonder about the wisdom of doing it this way, but this is roughly how I do it anyway
}

$query = "SELECT *
    FROM contacts
        INNER JOIN contacttodo ON contacts.contact_id=contacttodo.contact_id
    WHERE contacts.contactstatus = " . $status . " OR contacttodo.type = 'Appointment'";

另外,从Joe的关于连接的回答中稍微补充一点,因为如果你把条件设置为true,那么在WHERE子句中加入连接条件会真的搞砸了结果。