高级安装程序将MYSQL PHP脚本更改为MYSQLI

时间:2017-08-30 10:26:11

标签: php mysql sql mysqli advanced-installer

我有一个与高级安装程序一起运行的php脚本。 Mysql不起作用,我需要使用Mysqli函数。我已经将连接与Mysqli一起正常工作,但其他功能似乎无法正常工作。

脚本基本上只需要确认输入的序列号是否有效,并根据它的使用次数进行检查。我有一种方法可以让这更简单我都是耳朵!我不是一个专业的PHP开发人员,但高级安装人员的支持说他不知道如何将其更改为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();
}


?>

谢谢!

杰森

0 个答案:

没有答案