Advanced Installer Changing MYSQL PHP Script To MYSQLI

114 views Asked by At

I have a php script that runs alongside advanced installer. The Mysql doesn't work and I need to use Mysqli functions instead. I have already got the connection working fine with Mysqli but the other functions don't seem to be working.

The Script essentially just needs to confirm that the serial no entered is valid and check it against how many times it has been used. I'f there is a way of making this more simple I'm all ears!I'm not a professional php developer but the support from advanced installer said he doesn't know how to change it to mysqli.

<?php

define('LICENSE_VALID',   '601');
define('LICENSE_INVALID', '602');

# Fill our vars and run on cli
# $ php -f db-connect-test.php
$dbname = 'mydb';
$dbuser = '';
$dbpass = '';
$dbhost = '127.0.0.1';

$clients_tbl_name = 'clients';
$sn_tbl_col       = 'serial_no';
$lic_no_tbl_col   = 'license_no';
$val_no_tbl_col   = 'validations_no';

$conn = mysqli_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'");
mysqli_select_db($conn, $dbname) or die("Could not open the db '$dbname'");

// serial validation results
$serial_invalid = 0;    // invalid serial
$serial_ok      = 1;    // valid serial
$val_exceeded   = 2;    // valid serial but maximum number of validations exceeded

function ServerResponse($valResult, $posted_serial = '', $lang_id = 1033)
{
  global $serial_invalid, $serial_ok, $val_exceeded;

  $msg_sep = "\n";

  // load error messages from your database, using "$lang_id" for localization (optional)

  if($posted_serial == '')
    return LICENSE_INVALID . $msg_sep . "Missing Serial Number !";

  switch($valResult)
  {
    case $val_exceeded:
      return LICENSE_INVALID . $msg_sep . 'Maximum number of validations exceeded for Serial Number: ' . $posted_serial;
    case $serial_ok:
      return LICENSE_VALID;
    default:
      return LICENSE_INVALID . $msg_sep . 'Serial Number: ' . $posted_serial . ' is invalid !';
  }
}

if(isset($_POST['sn']) && trim($_POST['sn']) != '')
{
  // get the serial number entered by the installing user in the "UserRegistrationDlg" dialog 
  $sn = trim($_POST['sn']);

  // get the system language ID of the user's machine
  // (you can use this parameter to display a localized error message taken from your database)
  $languageid = (int) $_POST['languageid'];

  // prepare SQL statement
  $sn_query = sprintf("SELECT `%s`, `%s`, `%s` FROM `%s` WHERE `%s` = '%s'", 
    $sn_tbl_col, $lic_no_tbl_col, $val_no_tbl_col, 
    $clients_tbl_name, $sn_tbl_col, mysqli_real_escape_string($conn ,$_POST['sn']));

  // execute query
  $result = @mysqli_query($sn_query, $conn);

  // get result set size
  if(@mysqli_num_rows($result) == 0)
  {
    // serial number NOT found in database => issue error response
    echo ServerResponse($serial_invalid, $sn, $languageid);
    die();
  }
  else // serial number was found in database
  {
    // fetch the result row as an associative array 
    $row = @mysqli_fetch_array($result, MYSQLI_ASSOC);
    if(!$row)
    {
      // issue error response
      echo ServerResponse($serial_invalid, $sn, $languageid);
      die();
    }

    // increment the validations_no column
    $inc_val_no_query = sprintf("UPDATE `%s` SET `%s` = `%s` + 1 WHERE `%s` = '%s'",
      $clients_tbl_name, $val_no_tbl_col, $val_no_tbl_col, 
      $sn_tbl_col, mysqli_real_escape_string($conn ,$_POST['sn']));

    // execute the update query
    @mysqli_query($inc_val_no_query, $conn);

    // check whether the user has reached maximum number of validations
    $license_no    = (int) $row[ $lic_no_tbl_col ];
    $validation_no = (int) $row[ $val_no_tbl_col ];

    if($validation_no >= $license_no)
    {
      // issue error response => maximum number of validations exceeded
      echo ServerResponse($val_exceeded, $sn, $languageid);
      die();
    }
    else
    {
     // issue SUCCESS response
     echo ServerResponse($serial_ok, $sn, $languageid);
     die();
    }
  }
}
else
{
  // issue error response
  echo ServerResponse($serial_invalid);
  die();
}


?>

Thanks!

Jason

0

There are 0 answers