We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,151 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Retrive Data.

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"); ?>
3
Contributors
7
Replies
1 Day
Discussion Span
8 Months Ago
Last Updated
8
Views
Question
Answered
marifard
Newbie Poster
15 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
LastMitch
Industrious Poster
4,165 posts since Mar 2012
Reputation Points: 132
Solved Threads: 335
Skill Endorsements: 45

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.

marifard
Newbie Poster
15 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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>
LastMitch
Industrious Poster
4,165 posts since Mar 2012
Reputation Points: 132
Solved Threads: 335
Skill Endorsements: 45

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";
marifard
Newbie Poster
15 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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?

LastMitch
Industrious Poster
4,165 posts since Mar 2012
Reputation Points: 132
Solved Threads: 335
Skill Endorsements: 45

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

marifard
Newbie Poster
15 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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?

LastMitch
Industrious Poster
4,165 posts since Mar 2012
Reputation Points: 132
Solved Threads: 335
Skill Endorsements: 45
Question Answered as of 8 Months Ago by LastMitch

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0816 seconds using 2.73MB