查询在mysql中运行良好,但在PHP中运行不正常

时间:2015-03-24 09:45:29

标签: php mysql inner-join

查询在mysql中运行良好但在php中运行不正常。 在php中实现内连接后,我只获得了周二的价值。星期一和星期二有两张桌子。从周一和第二周的lab1选择lab1,就在星期二的lab1正在展示。

P.S。该查询在mysql中工作。

<form action="lab.php" method="POST">
    Check routine for : <select name="new">
        <option name="lab1">LAB1</option>
        <option name="lab2">LAB2</option>
        <option name="lab3">LAB3</option>
        <option name="lab4">LAB4</option>
    </select>&nbsp&nbsp
    <input type="submit" value="Submit">
</form>


<?php
require 'conn.php';
if(isset($_POST['new'])&& !empty($_POST['new']))
{
        $lab=$_POST['new'];
        $lab=strtolower($lab);
        $result = mysql_query("select  monday.time,
        monday.$lab,tuesday.$lab from monday inner join 
        tuesday where monday.time=tuesday.time");

        echo "<table BORDER='1' width='50%' cellpadding='3'>
        <tr> 
            <th colspan='7'><h2>$lab</h2></th> 
        </tr>

        <tr> 
            <th>TIME</th>
            <th>MONDAY</th>
            <th>TUESDAY</th>

        </tr>";

        while($row = mysql_fetch_array($result))
        {   echo "<tr>";

                    echo "<td>" .$row['time']. "</td>";
                    echo "<td>" .$row[$lab]. "</td>";
                    echo "<td>" .$row[$lab]. "</td>";

            echo "</tr>";
        }
    echo "</table>";


}

?>

1 个答案:

答案 0 :(得分:1)

试试这个有效。

 <?php
    require 'conn.php';
    if(isset($_POST['new'])&& !empty($_POST['new']))
    {
            $lab=$_POST['new']; 
            $lab=strtolower($lab);
            $result = mysql_query("select  monday.time,
            monday.$lab,tuesday.$lab as labval from monday inner join 
            tuesday where monday.time=tuesday.time") or die(mysql_error());

            echo "<table BORDER='1' width='50%' cellpadding='3'>
            <tr> 
                <th colspan='7'><h2>$lab</h2></th> 
            </tr>

            <tr> 
                <th>TIME</th>
                <th>MONDAY</th>
                <th>TUESDAY</th>

            </tr>";

            while($row = mysql_fetch_array($result))
            {   echo "<tr>";

                        echo "<td>" .$row['time']. "</td>";
                        echo "<td>" .$row[$lab]. "</td>";
                        echo "<td>" .$row['labval']. "</td>";

                echo "</tr>";
            }
        echo "</table>";


    }

    ?>