SQL String Trouble

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
snowrhythm
Forum Commoner
Posts: 75
Joined: Thu May 04, 2006 1:14 pm
Location: North Bay Area, CA

SQL String Trouble

Post by snowrhythm »

Here is the code I'm having trouble with:

Code: Select all

# Query the MachineServiceProfile for all machines for given company and put data into array
	$machineQuery=odbc_exec($odbc, "SELECT * FROM MachineServiceProfiles Where CompanyID = $companyID ORDER BY DateMade ASC");
	while ($machineInfo = odbc_fetch_array($machineQuery)){
		# Load up the array
		$tmpMachine = array('LocationID' => $machineInfo["LocationID"], 'MachineServiceProfileID' => $machineInfo["MachineServiceProfileID"], 'SerialNumber' => $machineInfo["SerialNumber"], 'Model' => $machineInfo["Model"], 'DateMade' => $machineInfo["DateMade"], 'ShippedDate' => $machineInfo["ShippedDate"], 'Notes' => $machineInfo["Notes"]);
		$machineArray[] = $tmpMachine;
	}
	$machineID = $machineArray['MachineServiceProfileID'];
	
	# THE SECOND STRING --
	$machineNotesQuery=odbc_exec($odbc, "SELECT * FROM MachineServiceNotes Where MachineServiceProfileID = $machineID");
	while ($machineNotes = odbc_fetch_array($machineNotesQuery)){
		# Load up the array
		$noteMachine = array('MachineServiceProfileID' => $machineNotes["MachineServiceProfileID"], 'DateCreated' => $machineNotes["DateCreated"], 'Notes' => $machineNotes["Notes"]);
		$machineNotesArray[] = $noteMachine;
	}
What's happening here is that I get an error when I run this code saying that I'm giving an invalid argument to the second SQL string. The part of the string it doesn't like is "WHERE MachineServiceProfileID = $machineID" which leads me to believe that $machineID isn't being filled properly with $machineArray['MachineServiceProfileID']. Is it because I'm assigning the $machineID variable outside of the while loop where $machineArray[] is declared? Any help is appreciated!

Thanks in advance
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

From what I can see, the $machineArray is a multidimensional array with the arrays returned from the original query. So each turn through the while() loop, the record returned as an array is input into a new index in $machineArray.

Add this line just prior to the second query, dump the values in your $machineArray.

Code: Select all

echo '<pre>'; var_dump($machineArray); echo '</pre>';
Post Reply