2

I'm using mysql_fetch_object in my code, and am getting an error because the object I am trying to create an instance of has a constructor.

The error is as follows: Warning: Missing argument 1 for Ratio::__construct() in /Users/Alex/Sites/All Good Things/_manage/c/c_ratio.php on line 9

Which I can understand; here's the code for the Ratio constructor:

function __construct($new_name, $new_x, $new_y)
{
    $this->name = $new_name;
    $this->x = $new_x;
    $this->y = $new_y;
}

Anyway, how do I pass variables from the result I have just created to the function, ie, What I what to do is something like this:

while ($currentRatio = mysql_fetch_object(
                              $ratio_rs, 
                              'Ratio', 
                               array(
                                    <VALUE OF NAME>, 
                                    <VALUE OF X>, 
                                    <VALUE OF Y>)))

I can't find any examples of the syntax required for this third parameter.

1
  • The third parameter is an array of the constructor parameters, like you outlined it. Take care that the constructor will be called after the properties have been set from the database, not before. If name is a column, first the data will be fetched from the database, then the constructor will be called with your parameters and then name will be overwritten by your constructor code. Probably that's already intended, just saying. Commented Feb 26, 2012 at 1:31

1 Answer 1

2

Use the Column alias. Column alias depends on how you construct the query. If you use alias (by as) then it should be alias otherwise use the column name. See the example bellow.

$ratio_rs = mysql_query("SELECT Name, x as `X`, y as `Y` from ...");
mysql_fetch_object($ratio_rs, 
    'Ratio', 
    array(
        'Name', 
        'X', 
        'Y'
    )
);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.