How-to articles, tricks, and solutions about PHP

mysql_fetch_array() expects parameter 1 to be resource

It looks like you are trying to use a function from the MySQL extension in PHP, but you are getting an error saying that the first parameter should be a resource.

mysqli or PDO - what are the pros and cons?

mysqli and PDO are both PHP extensions used for interacting with databases.

mysqli_connect(): (HY000/2002): No connection could be made

This error message typically indicates that there was a problem trying to establish a connection to the MySQL server.

mysqli_fetch_array while loop columns

The mysqli_fetch_array() function in PHP is used to retrieve rows of data from a MySQL database query result, and return the data as an array.

mysqli::query(): Couldn't fetch mysqli

This error message is typically encountered when an attempt to execute a MySQLi query has failed.

New lines (\r\n) are not working in email body

In order to include new lines in the body of an email sent with PHP, you will need to use the proper new line characters.

Nginx serves .php files as downloads, instead of executing them

There are several reasons why Nginx may serve PHP files as downloads instead of executing them.

Nullable return types in PHP7

In PHP7, nullable return types were introduced, allowing a function to return either a specific type or null.

Only variable references should be returned by reference - Codeigniter

This error message in Codeigniter indicates that you are attempting to return a variable by reference, but only variable references can be returned by reference.

Output an Image in PHP

In PHP, you can output an image using the built-in function header() to set the content type to an image format and the readfile() function to read the image file and output its contents.

Package php5 have no installation candidate (Ubuntu 16.04)

I'm sorry to hear that you're having trouble installing PHP on your Ubuntu 16.04 system.

Parsing domain from a URL

To parse the domain from a URL in PHP, you can use the parse_url function, which will return an associative array containing information about the URL.

Passing an array to a query using a WHERE clause

To pass an array to a WHERE clause in PHP, you can use the implode function to join the array elements with a comma separator and use them in your WHERE clause.

Passing multiple variables to another page in url

There are several ways to pass variables from one PHP page to another.

PDO get the last ID inserted

To get the last inserted ID using PDO, you can use the PDO::lastInsertId() method after inserting a row into the database.

PDO with INSERT INTO through prepared statements

To insert data into a database using PDO and prepared statements, you can use the following steps:

PDOException: SQLSTATE[HY000] [2002] No such file or directory

This error message typically indicates that there is a problem connecting to the database using PHP's PDO (PHP Data Objects) library.

PHP __get and __set magic methods

The __get and __set magic methods in PHP are used to intercept calls to get or set the value of inaccessible properties of an object.

PHP - auto refreshing page

To refresh a page in PHP, you can use the header function to send the HTTP "Refresh" header to the client's browser.

PHP - Check if two arrays are equal

You can use the array_diff() function to check if two arrays are equal.

PHP - Copy image to my server direct from URL

To copy an image from a URL and save it to your server using PHP, you can use the following function:

PHP - Extracting a property from an array of objects

To extract a property from an array of objects in PHP, you can use the array_column function.

PHP - get base64 img string decode and save as jpg (resulting empty image )

To decode a base64 encoded image string and save it as a JPG file in PHP, you can use the following code:

PHP - setcookie(); not working

There are several reasons why the setcookie() function may not be working.