简体   繁体   中英

Mysql Query Getting Item as ID in 2 different tables

Im sorry for my bad english but this is the best i can.

I am trying to make a script that get's the item values of one line in my

Logs_client_trade 表屏幕 for this example i'll be using the row with ID 2.

The first query is working correctly and getting the 2 values of 1items and 2items and deletes the ; that comes with it.

But the second part seems a little bit harder and i can't solve it. The query is getting id, base_item from the table

项目

The last 2 Outputs marked red are the one's that are matching 1items & 2items

I'm trying to let the if statement filter the $item1 as id in the item table and output the baseitem of the found row

same goes for item2

网站示例屏幕

I'm using MySQL & MySQLi because this cms doesnt support newer versions of PHP yet.

<?php
 $query = "SELECT * FROM logs_client_trade ORDER by id DESC";
 $result = mysql_query($query) or die(mysql_error());
 while($row = mysql_fetch_array($result)){

     $item1 = rtrim($row['1items'],"; ");
     $item2 = rtrim($row['2items'],"; ");

     echo("<tr>");
     echo("<td>" . $row['id'] . "</td>");
     echo("<td>" . $row['1id'] . "</td>");
     echo("<td>" . $row['2id'] . "</td>");

    $userinfo = "SELECT id, base_item FROM items LIMIT 1";
    $result2 = mysql_query($userinfo) or die(mysql_error());
        while($row2 = mysql_fetch_assoc($result2)){
            echo("<td>");
                if($row2['id'] == $item1){ echo $row2['baseitem']; } else echo "Not available";
            echo ("</td>");
            echo("<td>");
                if($row2['id'] == $item1){ echo $row2['baseitem']; } else echo "Not available";
            echo ("</td>");
        }
    $tradetime = $row['timestamp'];
    $date = date("d M Y - H:i:s", $tradetime);
    echo("<td>$date</td></tr>");
 }
?>

You forgot the while loop for the second query:

$userinfo = mysql_query("SELECT id, base_item FROM items");
while($get2 = $userinfo->fetch_assoc()) {
    $baseid = $get2['id'];
    $baseitem = $get2['baseitem'];

    echo("<tr>");
    [...]
}

In addition to that, your code is a mess. In the second loop you are still listing the rows from the logs table... I suggest you to review carefully your code, being careful on how you are using the different $row and $get2 arrays.

You can use a JOIN to get all of the info with one SQL statement:

SELECT * 
FROM `logs_client_trade` a
LEFT JOIN `items` b
ON REPLACE(a.`1items`,';','') = b.`id`
LEFT JOIN `items` c
ON REPLACE(a.`2items`,';','') = c.`id`
ORDER by a.`id` DESC

UPDATE:

Here's the PHP code that will run the query and output the table rows. I've specified the column names based on the code in the original question.

<?php

$query = "SELECT
    a.`id`,
    a.`1id`,
    a.`2id`,
    a.`1items`,
    a.`1items`,
    IFNULL(b.`baseitem`,'Not Available') as `baseitem1`,
    IFNULL(c.`baseitem`,'Not Available') as `baseitem2`,
    DATE_FORMAT(a.`timestamp`,'%d %m %Y - %H:%i:%s') as `tradetime`
FROM `logs_client_trade` a
LEFT JOIN `items` b
ON REPLACE(a.`1items`,';','') = b.`id`
LEFT JOIN `items` c
ON REPLACE(a.`2items`,';','') = c.`id`
ORDER by a.`id` DESC
";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){

    $item1 = rtrim($row['1items'],"; ");
    $item2 = rtrim($row['2items'],"; ");

    echo "<tr>\r\n";
    echo "  <td>" . $row['id'] . "</td>\r\n";
    echo "  <td>" . $row['1id'] . "</td>\r\n";
    echo "  <td>" . $row['2id'] . "</td>\r\n";
    echo "  <td>" . $row['baseitem1'] . "</td>\r\n";
    echo "  <td>" . $row['baseitem2'] . "</td>\r\n";
    echo "  <td>" . $row['tradetime'] . "</td>\r\n";
    echo("</tr>\r\n");
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM