Jump to content

[SOLVED] need to increment a number


blue-genie

Recommended Posts

what i'm doing is before I insert a new row, i want to get the last value in the gameInstance_No field, increment that by 1 and do the insert.

 

while I run the select statement it returns the value correctly however the insert is putting a zero.

 

$sql = "SELECT gameInstance_No FROM gameinstance ORDER BY gameInstance_No DESC LIMIT 0,1";

    $result = mysql_query($sql);

 

    if (mysql_num_rows($result)){

              $gameInstanceNo = $row['gameInstance_No']+1;

 

    }else{

              $gameInstanceNo = 0;

    }

 

thanks.

 

Link to comment
https://forums.phpfreaks.com/topic/174159-solved-need-to-increment-a-number/
Share on other sites

this is what I opted for, not sure if it's the best way.

 

<?php

 

session_start();

include 'config.php';

include 'opendb.php';

 

$gameID= $_REQUEST['gameID'];

$gameRNG= $_REQUEST['gameRNG'];

 

$insert = mysql_query("INSERT INTO gameinstance (gameID, gameRNGResult, gameDateTime) VALUES ('$gameID', '$gameRNG', now())");

if(!$insert){

echo '<?xml version="1.0"?>';

                echo '<dataxml>';

die("There's little problem: ".mysql_error());

$MyError = "An error occured while saving data. Please try again later." + mysql_error();

echo "<sqlError>".$MyError."</sqlError>";

echo '</dataxml>';

} else {

  $gameInstanceNo = mysql_insert_id();

  printf("Last inserted record has id %d\n", mysql_insert_id());

  mysql_query("UPDATE gameInstance SET gameInstance_No='$gameInstanceNo' WHERE USN_Gameinstance='$gameInstanceNo'");

  echo '<?xml version="1.0"?>';

          echo '<dataxml>';

  $success = mysql_insert_id();

  echo "<success>".$success."</success>";

  echo '</dataxml>';

}

 

?>

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.