根据函数获取mysql表列的值

时间:2014-05-07 11:43:21

标签: php html mysql

我有一个功能 get_me()。 get_me()返回页面的slug。

我还有mysql表,其中包含以下数据,分别为 slug block_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()返回服务,我想回复&#34;服务,!&#34;

已经定义了get_me()的功能。我只需要知道如何根据get_me()来获取block_to_echo。

显然,我不知道如何对此进行编码,所以如果我无法提供代码,我很抱歉。

1 个答案:

答案 0 :(得分:0)

<?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
   }
}
?>