-1

I was using CodeIgniter 2 back then and didn't got any trouble but now I'm using CodeIgniter 3 and have some trouble over mysql and mysqli for my code.

My code is:

<?php       

$query_i = "select MAX(Id_Product) from products";
$result_i = mysql_query($query_i);
$data2 = mysql_fetch_array($result_i);

$MaxID = $data2[0];

$temp = (int)substr($MaxID,2,4);
$temp++;

$NewID = "P".sprintf("%04s",$temp); ?>

error messages :

mysql_query(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead

and

mysql_fetch_array() expects parameter 1 to be resource, boolean given

Does anyone know how to fix it?

halfer
  • 19,471
  • 17
  • 87
  • 173
  • There is nothing to do with Codeigniter if you use this code even in core PHP it will going to give same error. As MySQL extension is no more available so start using mysqli or PDO – Sunil Pachlangia Dec 09 '16 at 04:29
  • Looking at [this](http://php.net/manual/en/function.mysql-query.php), it looks like `mysql_query` will produce false on error when using `select`. – Robert Prévost Dec 09 '16 at 04:31

2 Answers2

0

Well, your error seems to say that the version of PHP you are using works with mysqli. Please do replace all mysql statements with mysqli statements.

As for your second error, apparently mysql_fetch_array is receiving a boolean value, false. It seems that your query is failing and returning false, of which you are trying to fetch an array. Please add this line of code to your program and post the output as soon as possible, so that I may be able to debug it.

`
//after the mysql_fetch_array statement

 var_dump($query_i)
 var_dump($result_i)
 var_dump($data2)`
Fabulous
  • 719
  • 1
  • 10
  • 28
0

You can use this following code to solve your problem

<?php
$sql="select MAX(Id_Product) from products";
$result=mysqli_query($con,$sql);

// Numeric array
$row=mysqli_fetch_array($result,MYSQLI_NUM);

$MaxID = $row[0];
$temp = (int)substr($MaxID,2,4);
$temp++;
$NewID = "P".sprintf("%04s",$temp); 
?>
K.Suthagar
  • 2,096
  • 1
  • 15
  • 28