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"); ?>

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.

Member Avatar for LastMitch

@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>

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";
Member Avatar for LastMitch

@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?

yes, the name of tables and fields are the same in db.

Member Avatar for LastMitch

@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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.