Replacing goto with a function, why doesn't this work?
<p>I've been trying to change some code for a validation script so that it doesn't include goto, as I want to run my scripts on a webhost, and unfortunately most of them don't really have 5.3 or later.</p>

<p>The code is meant to use a value generated by rand for a unique validation number. If it finds it already in the DB, it will append 1 on to the number as many times as it needs to until it finds a usable number. Pretty straightforward. I know it's terrible practice for a MySQL query, but it's really just a simple script for practice.</p>

<p>The code is as follows:</p>

<pre><code>function gen_val($val)
{
    $query = mysql_query("SELECT val_id FROM users_tmp WHERE val_id=$val");
    $row = mysql_fetch_array($query);
    if ($row[0]==$val) {
        gen_val($val+1);
    } else {
        return $val;
    }
}

$val_x=gen_val(rand());
</code></pre>

<p>This returns a value '0' if the value already existed in the DB. Function works fine if there's no similar value already, so I think there's a problem with the function recursing. I don't think I really know how to use it properly, so please go ahead and educate me.</p>

<p>Cheers.</p>

<p>Asher</p>
