使用str_replace在html中翻译MySQL表


I have a website which contains a pricelist of CS:GO items. Now, how I did is this:

MySQL DB w/ All prices --> SELECT FROM DB Where X ( Item name ) --> echo Table

( Works fine )


Now I want to do a translation of the item names. How I planned to do it:

MySQL DB --> $result= From Where ( signature character ) --> $translate = str_replace (signature character from $result)

However, there's an issue with the code or str_replace I think.

$translate returns $result with no filtering.

        <?php
require 'lang.php';
$servername = "localhost";
$username = "";
$dbname = "";
$password = "";

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

// Filter Name
$sql = "SELECT Name, Price FROM utf WHERE Name LIKE 'K%'";
$result = $conn->query($sql);

// Translate
$tr = str_replace('K','Karambit','$result');

// Echo Translation ( Testing Purposes )
if (isset($tr)) {
    echo "<tr><td>".$tr."</td><td>";

}

//Draw 

if ($tr->num_rows > 0) {
    echo "<div class=\"CSSTableGenerator style=\"width:600px;height:150px;\"><table><tr><th>Name</th><th>Price</th></tr></div>";
    // output data of each row
    while($row = $tr->fetch_assoc()) {
        echo "<tr><td>".$row["Name"]."</td><td>".$row["Price"]." ".$row["Trend"]."</td></tr>";
    }
   echo "</table>";
} else {
    echo "0 results";
}

//Close 
$conn->close();
?>

Original code if that matters:

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

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

$sql = "SELECT Name, Price FROM paid WHERE Name LIKE '%kar%'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    echo "<div class=\"CSSTableGenerator style=\"width:600px;height:150px;\"><table><tr><th>Name</th><th>Price</th></tr></div>";
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<tr><td>".$row["Name"]."</td><td>".$row["Price"]." ".$row["Trend"]."</td></tr>";
    }
    echo "</table>";
} else {
    echo "0 results";
}
$conn->close();
?>

You can use SQL's REPLACE() function:

$sql = "SELECT REPLACE(Name, 'K', 'Karambit') AS Name, Price FROM utf WHERE Name LIKE 'K%'";

If you want to do it in PHP, you have to do it after fetching the row:

while($row = $result->fetch_assoc()) {
    $name = str_replace('K', 'Karambit', $row['Name']);
    echo "<tr><td>".$name."</td><td>".$row["Price"]." ".$row["Trend"]."</td></tr>";
}