PHP MYSQL Error Message-mysql_fetch_row expects parameter 1 to be resource-


This is an error that one faces when they use mysql and mysqli Interchangeably. Functions that begin with “mysql” are deprecated and no longer supported in the latest versions of PHP. So, one should either use mysql throughout the program or mysqli, mix and match doesn’t work. Mix and match might work in the grocery store that might help you to get sale prices but unfortunately it doesn’t work here! 😀



How to fetch the last ID from a MySQL Table and save it in a PHP Variable

When you are Involved in comparing and updating two different tables there may be times when you would need the last ID of a table so that it can be used in another table provided the ID is a primary key set to Auto Increment.

The way you would go about this is to use the max keyword to get the last ID of a table and then use the mysql_fetch_row function to fetch a row of data from the result handle. This value is then saved in a PHP Variable that can be later used for further manipulation. The overall code would appear to be something like this-

//Database Connection
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);

//Get the Last ID of a table. The column name is "SNo"
$last_id = mysqli_query($db,"SELECT max(SNo) FROM Test4_HarvestData");

if (!$last_id) {

    die('Could not query:' . mysql_error());


//The Last ID saved in a PHP Variable
$row_sno= mysqli_fetch_row($last_id);

echo "<br>The Last ID is : ".$row_sno[0];



How to stop a PHP Script from terminating abruptly?

One can schedule PHP Scripts to run on a server using a Cron job but that does not mean it is going to run successfully. With the fact that  you may have tested the script multiple times with no run time errors being reported. Why would you think that there are uncertainties in this aspect?

PHP as you all know is a server side scripting language which is deemed to run on a server without Interruption. But if the script is running on a server that is  part of a shared hosting plan, restrictions are put in place to prevent a script from running indefinitely. This is done to prevent one tenant from consuming all the resources. And similarly if you use a 3rd party server to schedule/run your script, it would have similar rules on the table. If a script takes more than 30 seconds to run than there is a timeout and it is put on hold.

There are cases where if an end user runs a PHP Script on the browser and closes the tab/window, it would stop executing  because there has been a disconnect from the client side. As a developer we wouldn’t want this to happen. The script should run irrespective of the client’s activity status or the type of Server hosting plan that you have.

Fortunately there is an easy way to fix this! PHP has a one line code that helps in resolving this Issue. The code literally Ignores the Client’s Activity Status.



Ignore_user_abort accepts a boolean parameter. If set to true it would Ignore user action. So even if the user disconnects, the script would continue to run until it terminates on it’s own!

How to check whether the current year is a leap year in PHP?

A leap year is a year that has 366 days that Includes an extra day in February. It occurs once every 4 years with the next leap year occurring in 2020. You would think that finding a leap year would Involve some complex calculation. But, in PHP it is just a single line of code that determines whether the current year is a Leap Year or not.

It uses the date library function.


This returns a boolean value. 1 being that the current year is a leap year and 0 if it is not.


How to convert to a specific date format in PHP?

Date is something which could be represented or described in many different ways. For Instance the date today could be represented in the following ways-

20th March 2018




And so on

So, it is Important to know how would one convert from one format to another.

The first thing to do is set a default date format.



Then if you want the output of the date to be as month/day/year



This statement can be modified to the format that you require.

You need to pay attention to the characters that are used to describe the dates, they are case sensitive.

This would give you a detailed explanation on the characters and their description.


Debug Print Backtrace PHP

Debug Print Backtrace is a function in PHP. As the name suggests it traces back function calls.

For Example

1. <?php
2. function one(){

 3. two();

4. }

5. function two(){

6. three();

7. }

8. function three(){

9. debug_print_backtrace();

10. }

11. one();

12. ?>

The output would show you the list of functions that were called, starting from the last to the first function along with the line numbers


#0  three() called at 6
#1  two() called at 3
#2  one() called at 11



A PHP Resource

PHP as you know is a popular server side scripting language.  If you want to get started with PHP it might seem  a bit overwhelming at first since you have to setup your own server and then go through different documentation. And sometimes documentation can get quite Intimidating. Fortunately there is a resource that is quite easy to read and it is catered for beginners starting out with PHP and learning the nuances of the language.

It is called PHP the Right Way.  This website covers different topics right from setting up your environment to security, error handling and caching. Hope people Interested in getting started in PHP can take some time perusing this resource 🙂