0

我尝试使用此函数在PHP中返回MySQL结果。我在PhpMyAdmin中运行了查询,它运行成功。但是当我尝试在PHP中运行它时,什么都没有返回。

我的结构如下:

<table>
    <tr>
    <td>Setting</td>
    <td>Value</td>
    </tr>
    <tr>
    <td>Setting</td>
    <td>stylesheet.css</td>
    </tr>
</table>

功能

function getData()
{
require 'config.php';
$con = mysql_connect($hostname, $username, $password, $database);
$sql = "SELECT Value FROM Settings WHERE Setting = 'Stylesheet'";
$result = mysql_query($con,$sql);
$row = mysql_result($result, 0);
mysqli_close($con);
return $row;
}

更新仍然无法正常工作

function getData()
{
require 'config.php';
$con = mysql_connect($hostname, $username, $password, $database);
$sql = "SELECT Value FROM Settings WHERE Setting = 'Stylesheet'";
$result = mysql_query($sql, $con);
$row = mysql_result($result, 0);
mysql_close($con);
return $row;
}
4

3 回答 3

2

mysql_query()功能不应该是mysql_query($sql, $con);吗?

关闭不应该mysql_close($con);吗?

编辑:我认为这就是它没有返回的原因。由于语法错误,它没有完成功能。

于 2013-09-05T01:35:50.097 回答
1

你正在混合mysql_*mysqli_*。如果只有一个 for ,则不需要指定连接mysql_*

Function [ <internal:mysql> function mysql_query ] {

  - Parameters [2] {
    Parameter #0 [ <required> $query ]
    Parameter #1 [ <optional> $link_identifier ]
  }
}

如果您不能使用例如PDO,请考虑重构您的函数:

require 'config.php';
$con = mysql_connect($hostname, $username, $password, $database);

function getData()
{
  $sql = "SELECT Value FROM Settings WHERE Setting = 'Stylesheet'";
  $result = mysql_query($sql);
  $row = mysql_result($result, 0);
  return $row;
}

连接在请求结束时关闭,您只是在进行不必要的连接/断开操作。

于 2013-09-05T01:45:58.917 回答
0
    class MySQL {
    var $ServerURL='localhost';
    var $ServerPort='3306';
    var $DataBaseName='test';
    var $DataBaseUser='root';
    var $DataBasePass='';
    public function __construct() {
        $this->mysqli = new mysqli($this->ServerURL,$this->DataBaseUser,$this->DataBasePass,$this->DataBaseName,$this->ServerPort);
        if (mysqli_connect_errno()) {
            printf('failed to connect');
            }
        else {
            }


    }

    }
    $MySQL = new MySQL;

       require 'config.php';
class Data extends MySQL {
     function getData()
        {
        $this->sql = "SELECT Value FROM Settings WHERE Setting = 'Stylesheet'";
        $result = mysqli_query($this->mysqli, $this->sql);
        $this->row = mysql_result($this->result, 0);
        return $this->row;

        }
$Data = new Data;
于 2014-04-30T06:04:24.617 回答