I need to use the goto operator in my code as I can’t seem to think of a way around it. However the problem is my host only has PHP version 5.2.17 installed.
Any ideas?
Below is my code:
if ($ready !=="y")
{
$check=mysql_query("SELECT `inserted` FROM `team`");
$numrows = mysql_num_rows($check);
$i="0";
while ($i<$numrows && $row = mysql_fetch_assoc($check))
{
$array[$i] = $row['inserted'];
$i++;
}
if (in_array("n", $array))
{
goto skip;
}
else
{
mysql_query("
UPDATE game SET ready='y'
");
}
}
skip:
There are a few anti-patterns in your code. Let’s clean it up. I’ll explain what’s been changed in a jiffy.
First things first: There is no need to perform a query, fetch all of the results, then loop through those results (
in_array) looking for a specific value. Let the database do that for you by expressly looking only for rows whereinsertedis the string literal"n".Because we know that we’re only getting
"n"records back, we just need to check if there are any results. If so, run the query. If there are no"n"records, theUPDATEisn’t run.If you need to know that the
UPDATEran, add a check for it: