Jump to content

Problem with while loop and SQL Query


Notretsam

Recommended Posts

<?php

	   $i = "0";
	   
	   while ( $i < "8" ) {
	   $i++;
	   $datagetname = "";
	   /* checking the current value of $i variable and setting database name  */
	   if ( $i == "1" ) { $datagetname = "matchPlayerssingle100"; }
	   elseif ( $i == "2" ) { $datagetname = "matchPlayerssinglehardcore"; }
	   elseif ( $i == "3" ) { $datagetname = "matchPlayersiquit"; }
	   elseif ( $i == "4" ) { $datagetname = "matchPlayerssinglecage"; }
	   elseif ( $i == "5" ) { $datagetname = "matchPlayerssinglehellcell"; }
	   elseif ( $i == "6" ) { $datagetname = "matchPlayerssingleladder"; }
	   elseif ( $i == "7" ) { $datagetname = "matchPlayerssingletlc"; }
	   
	   
	   if ( $datagetname == "" ) { $moveon = "go"; }
	   else {
       // Create connection
       $conn = new mysqli($host, $user, $password, $database);
       // Check connection
      if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
        }
	/* create a prepared statement */
	$stmtgetmat = $conn->prepare("SELECT matchID , rankType, matchCompany, matchDivision, matchNumb, matchType, matchTimer, matchDaytime, lastMoveLanded, curMovecount, eyegougeCount, asianmistCount, cfpunchCount, defenseSet, weppickup, curplayerTurn, matchPlay1, heaplay1, fatig1, momeplay1, matchPlay2, heaplay2, fatig2, momeplay2, matchResult, matchWinner, matchTime, matchDate FROM $datagetname WHERE matchID=?");
    /* bind parameters for markers */
    $stmtgetmat->bind_param("s", $sid);
    /* execute query */
    $stmtgetmat->execute();
	$stmtgetmat->store_result();
	$matcount= $stmtgetmat->num_rows;
	
	if ( $matcount == "0" ) { 
	$moveon = "go";
	/* close statement */
    $stmtgetmat->close(); 
	}
	else {
    /* bind result variables */
    $stmtgetmat->bind_result($matchID , $rankType, $matchCompany, $matchDivision, $matchNumb, $matchType , $matchTimer, $matchDaytime, $lastMoveLanded, $curMovecount, $eyegougeCount, $asianmistCount, $cfpunchCount, $defenseSet, $weppickup, $curplayerTurn, $matchPlay1, $heaplay1, $fatig1, $momeplay1, $matchPlay2, $heaplay2, $fatig2, $momeplay2, $matchResult, $matchWinner, $matchTime, $matchDate);
    /* fetch value */
    $stmtgetmat->fetch();
	    /* close statement */
    $stmtgetmat->close();
	}

	
	/* end of while loop */
	}
	

?>

The above page is named getdatabase.php and I include it into match.php, I get a 500 error when trying to access match.php.

 

I can't see why above coding isn't working.

 

Basically I have multiple databases with match information and need to check each database, until I find the right match visitor is viewing.

 

I do have other code that works and does what I need to do, but like to get above coding working , as it makes life easier in future when am adding new wrestling match types to my game, which every match type has its own database.

 

FYI

 

I don't show the include file that contains the $host, $user, $password, $database vars, but it is there.

Edited by Notretsam
Link to comment
Share on other sites

https://www.piliapp.com/php-syntax-check/

 

OK could have swore above cleared the coding , but now saying unexpected $end on line 56 , which is the ?> at the end

 

 

so added in another } , as was missing one, now above syntax check link says no errors, but still getting unable to load page and 500 error.

Edited by Notretsam
Link to comment
Share on other sites

<?php

	   $i = "0";
	   
	   while ( $i < "8" ) {
	   $i++;
	   $datagetname = "";
	   /* checking the current value of $i variable and setting database name  */
	   if ( $i == "1" ) { $datagetname = "matchPlayerssingle100"; }
	   elseif ( $i == "2" ) { $datagetname = "matchPlayerssinglehardcore"; }
	   elseif ( $i == "3" ) { $datagetname = "matchPlayersiquit"; }
	   elseif ( $i == "4" ) { $datagetname = "matchPlayerssinglecage"; }
	   elseif ( $i == "5" ) { $datagetname = "matchPlayerssinglehellcell"; }
	   elseif ( $i == "6" ) { $datagetname = "matchPlayerssingleladder"; }
	   elseif ( $i == "7" ) { $datagetname = "matchPlayerssingletlc"; }
	   
	   
	   if ( $datagetname == "" ) { $moveon = "go"; }
	   else {

       // Create connection
       $conn = new mysqli($host, $user, $password, $database);
       // Check connection
      if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
        }
	/* create a prepared statement */
	$stmtgetmat = $conn->prepare("SELECT matchID , rankType, matchCompany, matchDivision, matchNumb, matchType, matchTimer, matchDaytime, lastMoveLanded, curMovecount, eyegougeCount, asianmistCount, cfpunchCount, defenseSet, weppickup, curplayerTurn, matchPlay1, heaplay1, fatig1, momeplay1, matchPlay2, heaplay2, fatig2, momeplay2, matchResult, matchWinner, matchTime, matchDate FROM $datagetname WHERE matchID=?");
    /* bind parameters for markers */
    $stmtgetmat->bind_param("s", $sid);
    /* execute query */
    $stmtgetmat->execute();
	$stmtgetmat->store_result();
	$matcount= $stmtgetmat->num_rows;
	
	if ( $matcount == "0" ) { 
	$moveon = "go";
	/* close statement */
    $stmtgetmat->close(); 
	}
	else {
    /* bind result variables */
    $stmtgetmat->bind_result($matchID , $rankType, $matchCompany, $matchDivision, $matchNumb, $matchType , $matchTimer, $matchDaytime, $lastMoveLanded, $curMovecount, $eyegougeCount, $asianmistCount, $cfpunchCount, $defenseSet, $weppickup, $curplayerTurn, $matchPlay1, $heaplay1, $fatig1, $momeplay1, $matchPlay2, $heaplay2, $fatig2, $momeplay2, $matchResult, $matchWinner, $matchTime, $matchDate);
    /* fetch value */
    $stmtgetmat->fetch();
	    /* close statement */
    $stmtgetmat->close();
	}
	
	/* end of else statement */
   }
	
	/* end of while loop */
	}
	

?>

OK forgot to close of the else statement , so above coding is updated with it, still getting same 500 error though

Edited by Notretsam
Link to comment
Share on other sites

I've figured out what the problem is, its the following line.

FROM $datagetname

Strange it is that, as use variable elsewhere in site for database name and works fine.

Edited by Notretsam
Link to comment
Share on other sites

forget about this, slowly figuring it out

 

what is the causing the problem is, some of the sql table columns aren't named right in the databases. so getting a bind_parem error

Link to comment
Share on other sites

encase anyone actually reads this and wonders, here is final code, which works after i fixed typos in SQL lol stoopid me

 

maybe someone read this and learn something :)

<?php

//connection to sql here
		
		
	   $i = "0";
	   $datagetname = "";
	   
	   while ( $i < "7" ) {
	   $i++;
	   /* checking the current value of $i variable and setting database name  */
	   if ( $i == "1" ) { $datagetname = "matchPlayerssingle100"; }
	   elseif ( $i == "2" ) { $datagetname = "matchPlayerssinglehardcore"; }
	   elseif ( $i == "3" ) { $datagetname = "matchPlayersiquit"; }
	   elseif ( $i == "4" ) { $datagetname = "matchPlayerssinglecage"; }
	   elseif ( $i == "5" ) { $datagetname = "matchPlayerssinglehellcell"; }
	   elseif ( $i == "6" ) { $datagetname = "matchPlayerssingleladder"; }
	   elseif ( $i == "7" ) { $datagetname = "matchPlayerssingletlc"; }
	   
	   if ( $datagetname == "" ) { $moveon = "go"; }
	   else {
	   if ( $datagetstop == "found" ) { $moveon = "go"; }
	   else {
	/* create a prepared statement */
	$stmtgetmat = $conn->prepare("SELECT matchID , matchCompany, matchDivision, rankType, matchNumb, matchType, matchTimer, matchDaytime, lastMoveLanded, curMovecount, eyegougeCount, asianmistCount, cfpunchCount, defenseSet, weppickup, curplayerTurn, matchPlay1, heaplay1, fatig1, momeplay1, matchPlay2, heaplay2, fatig2, momeplay2, matchResult, matchWinner, matchTime, matchDate FROM $datagetname WHERE matchID=?");
    /* bind parameters for markers */
    $stmtgetmat->bind_param("s", $sid);
    /* execute query */
    $stmtgetmat->execute();
	$stmtgetmat->store_result();
	$matcount= $stmtgetmat->num_rows;
	if ( $matcount == "0" ) { $datagetname = ""; }
	elseif ( $matcount == "1" ) { $datagetstop = "found"; }
	}
	  
/* end of else statement */
   }
	
	/* end of while loop */
	}
	

	    /* bind result variables */
    $stmtgetmat->bind_result($matchID , $rankType, $matchCompany, $matchDivision, $matchNumb, $matchType , $matchTimer, $matchDaytime, $lastMoveLanded, $curMovecount, $eyegougeCount, $asianmistCount, $cfpunchCount, $defenseSet, $weppickup, $curplayerTurn, $matchPlay1, $heaplay1, $fatig1, $momeplay1, $matchPlay2, $heaplay2, $fatig2, $momeplay2, $matchResult, $matchWinner, $matchTime, $matchDate);
    /* fetch value */
    $stmtgetmat->fetch();
	    /* close statement */
    $stmtgetmat->close();

	

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...