$query1= mysql_query("select PageId FROM page");
$result = mysql_query("SELECT * FROM page");

echo "<td><a href='http://localhost:1234/usman/page_update_insert.php'>
        <img src='IMG/create.png'>Add New Record</a>  </td>";
        echo "<table border='0'>

<tr class='tableheader'>
    <th>PageName</th>
    <th>MenuName</th>
    <th>ParentMenuId</th>
    <th>LinkWidth</th>
    <th>OtherUrl</th>
    <th>Status</th>
    <th>CreatedDate</th>
    <th>OnMainMenu</th>
    <th>FooterMenu</th>
</tr>";
while($row = mysql_fetch_array($result)) {
        echo "<tr class='tablerow'>";
        echo "<td>" . $row['PageId'] . "</td>";
        echo "<td>" . $row['MenuName'] . "</td>";
        echo "<td>" . $row['ParentMenuId'] . "</td>";
        echo "<td>" . $row['LinkWidth'] . "</td>";
        echo "<td>" . $row['OtherUrl'] . "</td>";

want to show title from abc table instead of pageid from page

Dani AI

Generated

Use a JOIN in the SELECT so the title from the abc table comes back in the same rowset as the page rows. 's two-query approach will work for tiny datasets but easily becomes an N+1 problem if a query is run inside the loop; is right to ask whether the tables are related; is also correct that a JOIN requires a common key. If the tables share a key, pulling the title with a LEFT JOIN is the simplest and most efficient solution.

SELECT p.PageName, p.MenuName, p.ParentMenuId, p.LinkWidth, p.OtherUrl,
       COALESCE(a.title, '') AS Title
FROM page AS p
LEFT JOIN abc  AS a ON a.id = p.PageId;  -- replace a.id with the actual PK/FK

The SELECT alias Title will be available in PHP as $row['Title']. Use LEFT JOIN if pages should still appear when there is no matching abc row; use INNER JOIN if only matched rows are wanted. If there really is no relation you can still avoid per-row queries by first querying abc once and building an associative map keyed by id, then looking up titles in that map while looping through the page result.

Replying to ’s concern: joining for a SELECT does not prevent inserts/updates/deletes on those tables. MySQL also supports multi-table UPDATE/DELETE when necessary, for example:

UPDATE page p
JOIN abc a ON a.id = p.PageId
SET a.title = 'new title'
WHERE p.PageId = 123;

Finally, stop using the deprecated mysql_* functions. Migrate to mysqli or PDO with prepared statements for safety, and always escape HTML output (e.g. htmlspecialchars) when printing database values into a page.

Recommended Answers

All 4 Replies

I think the only real way for you to do this would be to run two queries and create a variable for the field from the other table. That is probably your simplest way, if I have understood you correctly.

So like where you have your query text, just have two, named $query1 and $query2.

First run the first one which gets everything, then run your second one in the same way and store the single field to a variable.

Let me know if you need further help.

Jack

Do we have any relations form the two tables rjusman90? I mean abc and page,as it may be an efficient way though as scaasiboi suggested you may try it...

Hey scaasiboi

Its' not necessary that we need to run two queries. But condtion is that there need to be some common so we can apply the join in sql.

by appling join we can not insert update delete

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.