gpt4 book ai didi

php - 根据函数获取mysql表列的值

转载 作者:行者123 更新时间:2023-11-30 00:17:46 25 4
gpt4 key购买 nike

我有一个函数get_me()。 get_me() 返回页面的 slug。

我还有mysql表table,其中包含以下数据,分别具有slugblock_to_echo列:

----------------------------------------
| slug | block_to_echo |
----------------------------------------
| home | <b>Welcome</b> to home! |
| about | You're in about page! |
| services | Services, <i>yes</i>! |
----------------------------------------

现在,根据 get_me(),我想获取 block_to_echo 的值,是的,回显它。

例如,如果get_me()返回服务,我想回显“Services,yes!”

get_me()函数已经定义。我只需要知道如何根据 get_me() 获取 block_to_echo。

显然,我不知道如何编写这个代码,所以如果我无法提供代码,我很抱歉。

最佳答案

<?php    

$database_host='database host goes here. (use localhost if on the same machine)';
$username='database username goes here';
$password='database password goes here';
$database_name='enter the name of your database here';


$database=mysqli_connect($database_host,$username,$password,$database_name)or die("Error " . mysqli_error($database));
$query='SELECT block_to_echo from table where slug='."'".get_me()."'";
$result=mysqli_query($database,$query);
if(!$result){
//Error handling code here for issue with query
}else{
//Everything went ok
if(mysqli_num_rows($result) < 1){
//There are no results for the value returned from get_me();
}else{
//We know we have at least one row. I'll assume it will only have a single row
$row=mysqli_fetch_assoc($result);
echo $row['block_to_echo']; //This should print the block to echo on the screen
}
}
?>

关于php - 根据函数获取mysql表列的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23516877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com