Display the name instead of the ID


Display the name instead of the ID



Database setup:



Table: customers


id | name | address | zipcode | city | phone | email | active



Table: todo


id | customerid | description | information | active


$sql = "
SELECT *
FROM todo
ORDER
BY `customerid` ASC
, `description` ASC
";



show results:


echo $row['customerid'] $row['description'] $row['information'];



output:


customerid description information



desired output:


customername (from table customers) description information





I have been reading this forum and i find that i should use INNER JOIN but i can't get it to work.

Could anyone assist me?




2 Answers
2



First,you need use join to get the customername value


join


SELECT t.description,t.information,c.name
FROM todo t JOIN customers c ON c.id=t.customerid
ORDER BY `t.customerid` ASC, `description` ASC



Then try with below:


echo $row['name'] $row['description'] $row['information'];





Dear, With those 2 lines i dont get any output anymore. It's empty :(
– Hans van de Put
Jul 1 at 7:34





you need check your table data or use left join instead of join
– lucumt
Jul 1 at 7:35


left join


join





SELECT t.description,t.information,c.customername FROM todo t LEFT JOIN customers c ON c.customerid=t.customerid ORDER BY customerid ASC, description ASC LIMIT 0, 25 #1054 - Unknown column 'c.customername' in field list
– Hans van de Put
Jul 1 at 7:39



customerid


description





then customername is not in table customers,you need add more information about your question
– lucumt
Jul 1 at 7:41


customername


customers





Thank you, it worked! I provided the tables setup above. there is indeed no customersname, in that table it's just the 'name'. Thank you for your help!
– Hans van de Put
Jul 1 at 7:47



The thing is when you use "ORDER BY" you can't use the "*". Here is how your code should go:


<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT customers.customerid,name, description, information FROM customers INNER JOIN todo ON customers.customerid=todo.customerid ORDER
BY customers.customerid ASC
, description ASC";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br>". $row["name"]." ".$row["description"]. " " . $row["information"] . "<br>";
}
} else {
echo "0 results";
}

$conn->close();
?>



This should do it.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

NSwag: Generate C# Client from multiple Versions of an API