Jump to content

MySQL syntax error


Vel

Recommended Posts

Hello all. I'm getting a syntax error that I don't understand. When the result is 0 it throws an error, but when the result is 1 it works perfectly fine. Can someone explain why?

 

<?php
//Check if character is in a fleet
$_SESSION['charIsInFleet'] = FALSE; //Set fleet flag false
$sqlLoadFleetNames = "SELECT * FROM `activefleets`"; //Set sql search
$queryLoadFleetNames = mysql_query($sqlLoadFleetNames); //Set query
$i = 0;
while($row = mysql_fetch_array($queryLoadFleetNames)) {
$fleetNames[$i] = $row['fleetName'];
$i++;
}
$numberOfFleets = $i;
for($i = 0; $i <= $numberOfFleets; $i++) {
$fleetName = $fleetNames[$i];
$sqlSearchFleet = "SELECT * FROM `" .  $fleetName . "` WHERE `charId` = '" . $charId . "'"; //Set sql search
$querySearchFleet = mysql_query($sqlSearchFleet) or die('Error: ' . mysql_error());
$resultSearchFleet = mysql_num_rows($querySearchFleet);
if($resultSearchFleet > 0) {
	$rowSearchFleet = mysql_fetch_array($querySearchFleet);
	$_SESSION['charIsInFleet'] = TRUE;
	$_SESSION['charFleetName'] = $fleetName;
	$_SESSION['charShipHull'] = $rowSearchFleet['shipHull'];
	$_SESSION['charShipType'] = $rowSearchFleet['shipType'];
	break;
}
}

When it finds a person in a fleet then it works. When someone is not in an active fleet it throws the error:

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE charId = '90613880'' at line 1
Link to comment
https://forums.phpfreaks.com/topic/236030-mysql-syntax-error/
Share on other sites

It's now throwing up another error as well, which I've not seen before. I've included everything in the page.

Notice: Undefined offset: 1 in /usr/home/nhrpgame/public_html/btl/includes/initiate.php on line 57

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE charId = '90613880'' at line 1 sqlSearchFleet: SELECT * FROM WHERE charId = '90613880'

 

It seems the table name isn't being loaded. But why does it work when a person is in a fleet?

 

Line 57 is $fleetName = $fleetNames[$i]; btw.

 

EDIT:

That brings up another question. Why is it trying to search for number 1? There is only 1 fleet, so it should be 0. If I ignore the first row in the array (0) will that work? Nope, that just changes it to undefined offset 0. Is it because I add an extra i++ even when there is no more fleets and it's not hitting that when there is someone in the fleet. I need to change it to $numberOfFleets = $i-1.

 

That was it. Solved. Either $numberOfFleets = $i-1 or use $i < $numberOfFleets instead of <=. It was trying to access a non-existent array.

Link to comment
https://forums.phpfreaks.com/topic/236030-mysql-syntax-error/#findComment-1213434
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.