1

I'm a .net programmer that recently decided to check PHP out and so far I must say that it's quite fun.

I use WAMPServer to work with PHP and I'm having a problem when using by reference variables.

This is the code I'm using:

function drawCategories($parent_id, $catlistids="",$level=0,$selected="") {
    global $USERLANG;
$result = mysql_query("
    SELECT 
        BPPENNYAUTOBID_categories.cat_id,
        BPPENNYAUTOBID_cats_translated.cat_name 
    FROM
        BPPENNYAUTOBID_categories 
            INNER JOIN BPPENNYAUTOBID_cats_translated ON BPPENNYAUTOBID_categories.cat_id=BPPENNYAUTOBID_cats_translated.cat_id
    WHERE
        BPPENNYAUTOBID_cats_translated.cat_name!='' 
        AND BPPENNYAUTOBID_categories.parent_id='".$parent_id."' 
        AND BPPENNYAUTOBID_cats_translated.lang='".$USERLANG."' 
    ORDER BY 
        BPPENNYAUTOBID_categories.cat_name"
);

    while ($line = mysql_fetch_array($result)) {
        if($catlistids != "") { $catlistids .= "<br />"; }
        $spaces = "";
        for($i=0;$i<$level;$i++) $spaces .="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
        $catlistids .= "<option value='".$line['cat_id']."' ".($selected==$line['cat_id'] ? " selected ":"").">".$spaces.$line["cat_name"]."</option>";


        drawCategories($line["cat_id"], &$catlistids,$level+1,$selected);
 }
 return $catlistids;
}

When I call the drawCategories function the second time passing the variable $catlistids by reference then all the website content disapears, I don't get any kind of error but I suppose it's something to do with WAMP server definitions.

Can anyone help me solve this problem?

Thanks in advance

andrefrua
  • 33
  • 3
  • 1
    How do you pass the variable by reference? – Vahid Hallaji Sep 07 '13 at 10:08
  • As you can see I'm calling the function drawCategories within itself and when this call is made I use the & symbol on the $catlistids variable in order to send it by reference. At least this is what I've read around the web. – andrefrua Sep 07 '13 at 10:13
  • is error reporting on? `ini_set('display_errors', true); error_reporting(E_ALL);` Maybe now you'll get an error. – NDM Sep 07 '13 at 10:27
  • @andrefrua: you say you start learning: you should not use the [mysql_*](http://php.net/manual/en/book.mysql.php) extension, it has been [deprecated a long time ago and is outdated](http://www.php.net/manual/en/function.mysql-connect.php) (see the red box at the top). You can use either the [mysqli_*](http://php.net/manual/en/book.mysqli.php) extension, which will provide you with pretty much the same api, or even better use [PDO](http://php.net/manual/en/book.pdo.php) which is the recommanded way to do it (it's a light database abstraction layer included in php core). – Lepidosteus Sep 07 '13 at 10:39
  • @andrefrua: you should also look into used parameterized queries rather than concatenation to avoid sql injections, see [How can I prevent SQL injection in PHP?](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) and [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). Happy coding :) – Lepidosteus Sep 07 '13 at 10:40

1 Answers1

1

There is no reference sign on a function call - only on function definitions. Function definitions alone are enough to correctly pass the argument by reference. As of PHP 5.3.0, you will get a warning saying that "call-time pass-by-reference" is deprecated when you use & in foo(&$a);. And as of PHP 5.4.0, call-time pass-by-reference was removed, so using it will raise a fatal error.

You can pass a variable by reference to a function so the function can modify the variable. Remove & from function call and try this way:

function drawCategories($parent_id, &$catlistids="", $level=0, $selected="") {
                                  //^
Vahid Hallaji
  • 7,159
  • 5
  • 42
  • 51
  • By doing that change on the code I still have the same problem. I'm probably missing something else. – andrefrua Sep 07 '13 at 10:21
  • I think you could have a better way to get categories. Give me some minutes. – Vahid Hallaji Sep 07 '13 at 10:26
  • 1
    Nevermind my previous comment. I had also the & on the original function call. I removed it and it worked like a charm. Thanks a lot for your help hallaji – andrefrua Sep 07 '13 at 10:28