我在代码中使用mysql_fetch_object
,并且收到错误,因为我尝试创建实例的对象有一个构造函数。
错误如下:警告:第 9 行/Users/Alex/Sites/All Good Things/_manage/c/c_ratio.php 中的 Ratio::__construct() 缺少参数 1
我能理解;这是 Ratio
构造函数的代码:
function __construct($new_name, $new_x, $new_y)
{
$this->name = $new_name;
$this->x = $new_x;
$this->y = $new_y;
}
无论如何,如何将刚刚创建的结果中的变量传递给函数,即,我要做的是这样的:
while ($currentRatio = mysql_fetch_object(
$ratio_rs,
'Ratio',
array(
<VALUE OF NAME>,
<VALUE OF X>,
<VALUE OF Y>)))
我找不到第三个参数所需的语法示例。
最佳答案
使用列别名。列别名取决于您构建查询的方式。如果您使用别名(通过 as
),那么它应该是别名,否则使用列名。请参阅下面的示例。
$ratio_rs = mysql_query("SELECT Name, x as `X`, y as `Y` from ...");
mysql_fetch_object($ratio_rs,
'Ratio',
array(
'Name',
'X',
'Y'
)
);
关于mysql_fetch_object() 函数的 PHP class_name 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9449695/