我有一个网站,其中包含CS:GO项目的价目表。现在,我是怎么做到的:
MySQL DB w/ All prices --> SELECT FROM DB Where X ( Item name ) --> echo Table
(工作正常)
现在我想对项目名称进行翻译。我打算怎么做:
MySQL DB --> $result= From Where ( signature character ) --> $translate = str_replace (signature character from $result)
但是,我认为代码或str_replace存在问题。
$ translate返回$ result而不进行过滤。
<?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();
?>
原始代码如果重要:
<?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();
?>
答案 0 :(得分:1)
您可以使用SQL的REPLACE()
函数:
$sql = "SELECT REPLACE(Name, 'K', 'Karambit') AS Name, Price FROM utf WHERE Name LIKE 'K%'";
如果您想在PHP中执行此操作,则必须在获取行后执行此操作:
while($row = $result->fetch_assoc()) {
$name = str_replace('K', 'Karambit', $row['Name']);
echo "<tr><td>".$name."</td><td>".$row["Price"]." ".$row["Trend"]."</td></tr>";
}