-1

网址:

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 />";
    }




    ?>
4

3 回答 3

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;
于 2013-03-19T16:11:02.377 回答
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) 值不需要它周围的 tic。

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

于 2013-03-19T16:15:18.720 回答
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 />";
    }




    ?>

基本上你的查询是错误的,我希望这会有所帮助。

萨卢多斯 ;)

于 2013-03-19T16:34:04.990 回答