Hi all,

I have read a number of posts on this subject but I can't find a resolution so here goes.

I am trying to run an SQL query which is inside a function but I get the errors;

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /var/www/vhosts/mydomain.com/httpdocs/lgw/lgwfids.php on line 86

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /var/www/vhosts/mydomain.com/httpdocs/lgw/lgwfids.php on line 88

I know it's to do with "global variables" but I am not sure how or where to assign them.

Can anyone provide some assistance, please.

function myRecordHandler($record)
  {
  global $id, $AODBUniqueID, $FIDSFlightActive, $CarrierCode, $FlightNumber, $FlightSuffix, $FlightNoComplete, $ArrivalOrDeparture, $FlightSuffix, $TerminalCode, $GateNumber, $IATALocationCode, $PublicLocationName,$ScheduledDateTime, $EstimatedDateTime, $StatusMessageText, $ArrDep, $Iata, $StandAllocations, $City2;  

$ArrDep = mysql_real_escape_string($record["ArrDep"]);
$CarrierCode = mysql_real_escape_string($record["Airline"]);
$FlightNumber = mysql_real_escape_string($record["Airline"]."".$record["FltNo"]);
$ScheduledDateTime = mysql_real_escape_string($record["Time"]);
$EstimatedDateTime = mysql_real_escape_string($record["Est"]);
$PublicLocationName = mysql_real_escape_string($record["City1"]);
$City2 = mysql_real_escape_string($record["City2"]);
$IATALocationCode = mysql_real_escape_string($record["Iata"]);
$Stat = mysql_real_escape_string($record["Stat"]);
$GateNumber = mysql_real_escape_string($record["Gate"]);
$TerminalCode = mysql_real_escape_string($record["Term"]);
$StandAllocations = mysql_real_escape_string($record["Stand"]);
$Date = mysql_real_escape_string($record["Date"]);

$FlightDate = $Date ;


$timeflag = date("Y-m-d H:i:s"); 

$AODBUniqueID = $FlightNumber."".$ScheduledDateTime;
$_POST['AODBUniqueID'] = $AODBUniqueID ;
$colname_flight = "-1";
if (isset($_POST['AODBUniqueID'])) {
	$colname_flight = $_POST['AODBUniqueID'];
}
mysql_select_db($database_flightq, $flightq); // LINE 86
$query_flight = sprintf("SELECT * FROM flightdata WHERE AODBUniqueID = %s", GetSQLValueString($colname_flight, "text"));
$flight = mysql_query($query_flight, $flightq) or die(mysql_error());  // LINE 86
$row_flight = mysql_fetch_assoc($flight);  
$totalRows_flight = mysql_num_rows($flight);

}
$result = MagicParser_parse("/var/www/vhosts/mydomaincom/httpdocs/xml/LGW.CSV","myRecordHandler","csv|44|1|0");
    dcjones wrote:

    I know it's to do with "global variables" but I am not sure how or where to assign them.

    The first guideline to remember when using global variables is: don't use them.

    The second guideline is: don't use them!

    The thi... okay I'll stop.

    The source of your problem right now is that $flightq (hopefully the variable that contains a valid MySQL-Link resource) isn't within the scope of your function, so you'll either have to pass it in as a parameter (preferred over using global variables) or use the [man]global[/man] keyword to add the corresponding variable in the global scope into the function's local scope.

    This code looks like an excellent candidate for conversion to OOP. For example, you'd have a class that describes a Flight (with appropriate member variables and accessors/mutators for them). When you parse a flight, you'd create a new Flight object, populate it with the appropriate data, and pass that Flight object onto a DB abstraction class that extracts the Flight object's properties and does whatever you need it to do with that data.

      Hi Brad,

      Thanks for your reply, to get around the issue I move the "Link resource" inside the function and all is working as expected.

      Again, many thanks.

        Write a Reply...