结果没有显示在mysql php中获取id

时间:2013-03-19 16:08:35

标签: php mysql

URL:

empdetail.php?id=1

我的MySQL数据库中有两个表。我想合并两个表,我想我已经完成了。 $ _GET['id']或其他代码可能存在问题。当我点击empdetail.php?id=1时,结果显示完美。当我点击empdetail.php?id=2empdetail.php?id=3和其他人时,没有显示任何结果。我不知道为什么它没有显示任何结果。

   <?
    //////Displaying Data/////////////

       //connect to database

         mysql_connect('localhost','root','');
         mysql_select_db('cdcol');

    $id=$_GET['id']; // Collecting data from query string
    if(!is_numeric($id)){ // Checking data it is a number or not
        echo "Data Error"; 
        exit;
    }

    $result = mysql_query("SET NAMES utf8"); //the main trick

    $query = "SELECT ospos_employees.person_id, ospos_people.first_name ".
     "FROM ospos_employees, ospos_people ".
        "WHERE ospos_employees.person_id = ospos_people.person_id='$id'";

    $result = mysql_query($query) or die(mysql_error());


    // Print out the contents of each row into a table 
    while($row = mysql_fetch_array($result)){
        echo $row['person_id']. " - ". $row['first_name'];
        echo "<br />";
    }




    ?>

3 个答案:

答案 0 :(得分:1)

$query = "SELECT ospos_employees.person_id, ospos_people.first_name ".
     "FROM ospos_employees, ospos_people ".
        "WHERE ospos_employees.person_id = ospos_people.person_id AND ospos_people.person_id =".$id;

我有一个问题:为什么您的查询从2个表中选择。

怎么样?
 $query = "SELECT ospos_people.person_id, ospos_people.first_name ".
         "FROM  ospos_people ".
            "WHERE  ospos_people.person_id =".$id;

答案 1 :(得分:0)

首先,你的SQL语句是错误的。转换为直接SQL:

SELECT 
    ospos_employees.person_id, ospos_people.first_name
FROM
    ospos_employees, ospos_people
WHERE 
    ospos_employees.person_id = ospos_people.person_id= '$id'

您会看到WHERE声明错误。你需要解决它。之一:

ospos_employees.person_id = '$id'

ospos_people.person_id= '$id'

或(如果打算):

ospos_employees.person_id = '$id' AND ospos_people.person_id= '$id'
                                  ^^^

另请注意,SQL中的(int)值不需要围绕它的抽搐。

注意:不要使用MySQL_ *函数,因为它们自PHP 5.5起已被弃用。请改用MySQLi_ *或PDO。

答案 2 :(得分:0)

这是我的完整答案:

 <?
    //////Displaying Data/////////////

       //connect to database

         mysql_connect('localhost','root','');
         mysql_select_db('cdcol');

    $id=$_GET['id']; // Collecting data from query string
    if(!is_numeric($id)){ // Checking data it is a number or not
    echo "Data Error"; 
    exit;
    }

    $result = mysql_query("SET NAMES utf8"); //the main trick

    $query = "SELECT ospos_employees.person_id, ospos_people.first_name 
             FROM ospos_employees
             inner join ospos_people on(ospos_employees.person_id = ospos_people.person_id)
             where ospos_employees.person_id = $id";

    $result = mysql_query($query) or die(mysql_error());


    // Print out the contents of each row into a table 
    while($row = mysql_fetch_array($result)){
        echo $row['person_id']. " - ". $row['first_name'];
        echo "<br />";
    }




    ?>

基本上你的查询错了,我希望这有帮助。

Saludos;)