在我的数据库中,有一个这样的表:
id | name
==================
1 | day sunny
------------------
2 | day windy
------------------
3 | cloudy night
------------------
4 |rainy outside day
-----------------
如何在程序中打印名称为 晴天,刮风的日子,阴天的夜晚(我必须这样离开,因为我想要这样写)和外面的雨天?它必须以大写字母开头,我不能将其保存到表中,而只是在程序中打印?
这是我的代码:
$conn=mysqli_connect('localhost','root','','shop') or die ("Fail");
$sql="select * from products";
$result=mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$names = explode(" ", $row["name"]);
echo ucfirst(strtolower($names[1]." ".$names[0]." ".$names[2]."<br>" ));
}
}
它几乎完全满足了我的需求,但不是100%。 帮我谢谢你:)
答案 0 :(得分:0)
您可以使用SUBSTRING_INDEX
SELECT CONCAT(
SUBSTRING_INDEX(name, ' ', -1 ),
' ',
SUBSTRING_INDEX( name, ' ', 1 )
)
AS Name FROM table_name
结果
-------------
sunny day
-------------
windy day
-------------
cloudy night
-------------
在这里您可以使用PHP ucfirst编写首字母大写的大写字母:-
SELECT name,
(
CASE
WHEN
INSTR(name, 'day') > 0 AND (LENGTH(name) - LENGTH(REPLACE(name, ' ', '')) + 1) < 3
THEN
CONCAT(
SUBSTRING_INDEX(name, ' ', -1 ),
' ',
SUBSTRING_INDEX(name, ' ', 1 )
)
WHEN
INSTR(name, 'day') > 0 AND (LENGTH(name) - LENGTH(REPLACE(name, ' ', '')) + 1) > 2
THEN
CONCAT(
CONCAT(SUBSTRING_INDEX(name, ' ', 1),' ','day'),
' ',
REPLACE(SUBSTRING_INDEX(name, ' ', -2),'day','')
)
ELSE
name
END) AS Name
FROM tablename
结果:-
-----------
sunny day
-----------
windy day
-----------
cloudy night
-----------
rainy day outside
-----------
尝试在PHP while循环内进行以下操作:-
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
//$rows[] = ucfirst($row['Name']);
echo ucfirst($row['Name']);echo '<br/>';
}
}
完整的PHP代码:-
$conn = mysqli_connect('localhost','root','123123','stackoverflow') or die ("Fail");
$sql = "SELECT name,
(
CASE
WHEN
INSTR(name, 'day') > 0 AND (LENGTH(name) - LENGTH(REPLACE(name, ' ', '')) + 1) < 3
THEN
CONCAT(
SUBSTRING_INDEX(name, ' ', -1 ),
' ',
SUBSTRING_INDEX(name, ' ', 1 )
)
WHEN
INSTR(name, 'day') > 0 AND (LENGTH(name) - LENGTH(REPLACE(name, ' ', '')) + 1) > 2
THEN
CONCAT(
CONCAT(SUBSTRING_INDEX(name, ' ', 1),' ','day'),
' ',
REPLACE(SUBSTRING_INDEX(name, ' ', -2),'day','')
)
ELSE
name
END) AS Name
FROM tablename";
$result = $conn->query($sql);
$rows = array();
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$rows[] = ucfirst($row['Name']);
}
}
echo '<pre>';
print_r($rows);
输出:-
Array
(
[0] => Sunny day
[1] => Windy day
[2] => Cloudy night
[3] => Rainy day outside
)
答案 1 :(得分:0)
这应该有帮助
$dbhost = 'localhost:3306';
$dbuser = 'root';
$dbpass = '';
$dbname = 'example';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);
if(! $conn ) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully<br>';
$sql = 'SELECT name FROM example_table';
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$names = explode(" ", $row["name"]);
echo ucfirst($names[1])." ".$names[0] "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);