Welcome Guest, Not a member yet? Register   Sign In
retrieve data from db in same field name
#1

[eluser]tusukgigi[/eluser]
how to retrive data from the database with the same field names in the table

example:

users table
id | name | phone
1 tusuk gigi 08122222
2 tugi 021839039

products table
id | name | phone
1 php 04444444
2 java 02111111

model :
sql = SELECT a.id, a.name, a.phone, b.id, b.name, b.phone FROM products a
LEFT JOIN users b ON a.id = b.id
WHERE a.id='$id' LIMIT 1;

controller :

$data['hasPlace'] = $this->MPosts->cekOwnerPlace($id);

views :

echo $hasPlace['id'];
echo $hasPlace['name'];
echo $hasPlace['phone'];

wwith the code above I just get empty data.
whereas the data in the table is not empty
#2

[eluser]WanWizard[/eluser]
A bit of code would helpful.

How to you run the query? What exactly does the cekOwnerPlace() method return?

And please, embed code examples in [ code ] tags, to keep your posts readable.
#3

[eluser]tusukgigi[/eluser]
sorry next post will not happen again
thank you
#4

[eluser]Dennis Rasmussen[/eluser]
@tusukgigi, use the word AS for your fields.

Code:
$SQL = "SELECT p.id AS product_id, p.name AS product_name, p.phone AS product_phone, u.id AS user_id, u.name AS user_name, u.phone AS user_phone
        FROM products p
          LEFT JOIN users u
          ON product_id = user_id
        WHERE product_id = ’$id’
        LIMIT 1";

// In your view...
echo $hasPlace[‘product_id’];
echo $hasPlace[‘product_name’];
echo $hasPlace[‘product_phone’];
// ...

Does that help you? Smile
#5

[eluser]WanWizard[/eluser]
That would solve the duplicate column name issue, but still doesn't explain the fact that the result was emtpy, not wrong ( that would have been the next step Wink ).
#6

[eluser]tusukgigi[/eluser]
@Dennis Rasmussen:
thank you gan
its very helpfull ;-)

@WanWizard:
thank your suggestion




Theme © iAndrew 2016 - Forum software by © MyBB