I am a newbie to php, so pardon me if this email sounds, well... dumb. I am trying to port some php4 code for my boss. Here is this one function that makes me stumped.
function dbank($table,$sql,$dbankoverride="",$dbconfig="") {
if(empty($dbconfig)) $dbconfig="0";
$dbhostconfig[0]="XXXX";
$dbuserconfig[0]="XXXX";
$dbpasswdconfig[0]="XXXX";
$dbankconfig[0]="db1";
$dbhostconfig[1]="YYYY";
$dbuserconfig[1]="YYYY";
$dbpasswdconfig[1]="";
$dbankconfig[1]="db2";
$dbhost=$dbhostconfig[$dbconfig];
$dbuser=$dbuserconfig[$dbconfig];
$dbpasswd=$dbpasswdconfig[$dbconfig];
$dbank=$dbankconfig[$dbconfig];
if(!empty($dbankoverride)) $dbank=$dbankoverride;
$db=@mysql_connect($dbhost,$dbuser,$dbpasswd);
if(!$db) {
$errormsg="<style type=\"text/css\">\n";
$errormsg.="<!--\n";
$errormsg.=".errormsg01 { font-family: \"Times New Roman\", Times, serif; font-size: 12pt; font-style: normal; line-height: normal; font-weight: normal; font-variant: normal; text-decoration: none}\n";
$errormsg.="-->\n";
$errormsg.="</style>\n";
$errormsg.="<span class=\"errormsg01\">Database Error</span>\n";
die($errormsg);
} else {
# Select Database
@mysql_select_db($dbank,$db);
# Query
$result=@mysql_query($sql,$db);
if (substr($sql,0,6)=="select" || substr($sql,0,8)=="describe") {
for($i=0;$i<mysql_num_fields($result);$i++) {
$field_name[$i] = mysql_field_name($result,$i);
}
$num=@mysql_num_rows($result);
for($i=0;$in=@mysql_fetch_array($result);$i++) {
$input[$i]=$in;
}
@mysql_close($db);
$return=array($num,$field_name,$input);
return $return;
}
}
@mysql_close($db);
$return=array($num,$field_name,$input);
return $return;
}
As I understand, this function runs a sql query on a table and returns the variables $num, $field_name and $input in an array.
This function was written for a php4 server and it gives me errors "Undefined variable $num on line .. " for each of these variables.
My question is: How can I port this code so that it runs with PHP 5?
Hoping to hear back soon. Thanks a lot.
but if this statement return false
if (substr($sql,0,6)=="select" || substr($sql,0,8)=="describe")
you can't use variable $num, because it's not defined anywhere, out of this "if" statement?