0

Dear,

I have 2 tables,

first table
name: directory

fields: id, name, surname num1, operator1 (operator id from operator table), num2, operator2 (operator id from operator table).

(num2 and operator2 may be left empty)

second table.
name: operator

fields: id, operator

I made a page to display the data from these two tables and I have a small problem with select query (line 8) because when I write this part AND directory.operator2 = operator.id, the page didn't display any data. Anyone help me to solve this problem please?

<?php 
    require("../headerloggedin.php"); 
?> 
    <div id="content">
            <?php
                $userid = $_SESSION["userid"];

                $query = "SELECT * FROM directory, operator WHERE directory.operator1 = operator.id AND directory.operator2 = operator.id" ;
                $result = $db->query($query);

                while ($row = mysql_fetch_array($result))
                {
            ?>
            <table>
                <tr>
                    <td>Name:</td>                        
                    <td><?php echo $row["name"];?></td>
                </tr>
                <tr>
                    <td>Surname:</td>
                    <td><?php echo $row["surname"]; ?></td>
                </tr>
                <tr>
                    <td>Mobile Number:</td>
                    <td><?php echo $row["num1"]; ?></td>
                </tr>
                <tr>
                    <td>Operator:</td>
                    <td><?php echo $row["operator"]; ?></td>
                </tr>
                <tr>
                    <td>Mobile Number 2:</td>
                    <td><?php echo $row["num2"]; ?></td>
                </tr>
                <tr>
                    <td>Operator 2:</td>
                    <td><?php echo $row["operator"]; ?></td>
                </tr>
            </table>
            <br />
            <?php 
                }
            ?>
        <br />   
        <a href="../indexloggedin.php">Go to main menu.</a>
    </div>
<?php require("../footerloggedin.php"); ?>
2
Contributors
7
Replies
8
Views
5 Years
Discussion Span
Last Post by LastMitch
0

My problem is when I display the data and there is no num2 because it is empty in the DB, the field operator of num2 is displaying the same data of operator in num1.

0

@marifard

My problem is when I display the data and there is no num2 because it is empty in the DB, the field operator of num2 is displaying the same data of operator in num1.

I don't see any code regarding for:

<td><?php echo $row["num2"]; ?></td>

What I do see you have 2 of this:

<td><?php echo $row["operator"]; ?></td>

it should be 1

This is your code:

<td>Operator:</td>
<td><?php echo $row["operator"]; ?></td>

-

<td>Operator 2:</td>
<td><?php echo $row["operator"]; ?></td>

You forgot to put 1 & 2 in operator.

It should look like this

<td>Operator:</td>
<td><?php echo $row["operator1"]; ?></td>

-

<td>Operator 2:</td>
<td><?php echo $row["operator2"]; ?></td>
0

ok I change it and I try to make a JOIN in SQL statment to display the name of operator from operator table not the id of the operator because in operator1 and operator2 of directory table it save the id of the operator but I have some problems because the name of the operator of operator1 didn't display rhe name but the id and I don't know how to display the name of operator2 not the id.

the sql statment is:

$query = "SELECT dir.*,oprt.* FROM operator as oprt INNER JOIN directory AS dir ON oprt.id = dir.operator1";
0

@marifard

I have some problems because the name of the operator of operator1 didn't display rhe name but the id and I don't know how to display the name of operator2 not the id.

Can you show me the actually error when you run on phpmyadmin?

Is the table the same as you posted on top?

0

@marifard

The past few thread that you post is related?

I mean all of them is regarding about this:

$query = "SELECT dir.*,oprt.* FROM operator as oprt INNER JOIN directory AS dir ON oprt.id = dir.operator1";

My question is have you try those suggestion from the previous thread?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.