$ this-> value = $ dbarray什么都不返回

时间:2013-11-06 18:12:56

标签: php html mysql function class

所以我试图了解类和函数等,并且我坚持尝试从MySQL返回值。

php代码:

<?php 
    class SQL{
             var $value

    function GetMYSQL_SERVER(){
        mysql_connect('127.0.0.1','root',''); 
        mysql_select_db('admin');
          $q = "SELECT * FROM `configurations` WHERE `Information` = 'MYSQL_SERVER'";
          $result = mysql_query($q);
          $dbarray = mysql_fetch_array($result);
          $this->value = $dbarray['Value'];
        }       
    };
    $MYSQL = new SQL;
    ?>

HTML code:

<?php include "include/process.php"; ?>

<td>Mysql Servername</td>
<td><input type="text" style="width:100px;"></td>
<td><?php echo $MYSQL->value; ?></td>

因此,当我执行HTML代码时会出现问题。
它在HTML服务器值上返回空白。

3 个答案:

答案 0 :(得分:0)

我将查询更改为通用只是为了显示概念......

<?php   
class SQL{
   var $value; 
   public function GetMYSQL_SERVER()
   {
      mysql_connect('127.0.0.1','root','');  
      mysql_select_db('admin');
      $q = "select 'value' as Value";
      $result = mysql_query($q);
      $dbarray = mysql_fetch_array($result);
      $this->value = $dbarray['Value'];
  }       
};


$MYSQL = new SQL();

/// you have to call the function 
$MYSQL->GetMYSQL_SERVER(); 
?> 

<!-- ?php include "include/process.php"; ?-->

<td>Mysql Servername</td>
<td><input type="text" style="width:100px;"></td>
<td><?php echo $MYSQL->value; ?></td>

答案 1 :(得分:0)

GetMYSQL_SERVER()从未打过电话,所以:

  1. 将其重命名为__construct() - 它将在$MYSQL = new SQL;
  2. 上自动调用

    1. GetMYSQL_SERVER()之后手动调用$MYSQL = new SQL;方法$MYSQL->GetMYSQL_SERVER();

答案 2 :(得分:0)

您没有调用设置value属性的方法:

$MYSQL = new SQL;
$MYSQL->GetMYSQL_SERVER()