How to solve “Fatal error: Class ‘MySQLi’ not found” with PHP?

Sometimes, we want to solve "Fatal error: Class ‘MySQLi’ not found" with PHP.

In this article, we’ll look at how to solve "Fatal error: Class ‘MySQLi’ not found" with PHP.

How to solve "Fatal error: Class ‘MySQLi’ not found" with PHP?

To solve "Fatal error: Class ‘MySQLi’ not found" with PHP, we install MySQLi.

To install it, we run

sudo apt-get install php-mysqlnd

to install MySQLi by installing the php-mysqlnd package.

Conclusion

To solve "Fatal error: Class ‘MySQLi’ not found" with PHP, we install MySQLi.

How to send file attachments from form ising phpMailer and PHP?

Sometimes, we want to send file attachments from form ising phpMailer and PHP.

In this article, we’ll look at how to send file attachments from form ising phpMailer and PHP.

How to send file attachments from form ising phpMailer and PHP?

To send file attachments from form ising phpMailer and PHP, we call the AddAttachment method.

For instance, we write

if (
    isset($_FILES["uploaded_file"]) &&
    $_FILES["uploaded_file"]["error"] == UPLOAD_ERR_OK
) {
    $mail->AddAttachment(
        $_FILES["uploaded_file"]["tmp_name"],
        $_FILES["uploaded_file"]["name"]
    );
}

to call $mail->AddAttachment with the temporary name of the file and the real name of the file to add the file at the location as an attachment.

$_FILES["uploaded_file"] has the uploaded file.

Conclusion

To send file attachments from form ising phpMailer and PHP, we call the AddAttachment method.

How to preserve line breaks from a text area with PHP?

Sometimes, we want to preserve line breaks from a text area with PHP.

In this article, we’ll look at how to preserve line breaks from a text area with PHP.

How to preserve line breaks from a text area with PHP?

To preserve line breaks from a text area with PHP, we can use the nl2br function.

For instance, we write

echo nl2br("This\r\nis\n\ra\nstring\r");

to return a string with '\r\n' and '\r' replaced with '<br />'.

Conclusion

To preserve line breaks from a text area with PHP, we can use the nl2br function.

How to get the name of the caller function in PHP?

Sometimes, we want to get the name of the caller function in PHP.

In this article, we’ll look at how to get the name of the caller function in PHP.

How to get the name of the caller function in PHP?

To get the name of the caller function in PHP, we can use the debug_backtrace function.

For instance, we write

$trace = debug_backtrace();
$caller = $trace[1];

echo "Called by {$caller["function"]}";
if (isset($caller["class"])) {
    echo " in {$caller["class"]}";
}

to call debug_backtrace and get the 2nd entry to get the $caller array.

We then use $caller["function"] to get the caller function name and the $caller["class"] get the caller class name.

Conclusion

To get the name of the caller function in PHP, we can use the debug_backtrace function.

How to pass $_POST values with cURL and PHP?

Sometimes, we want to pass $_POST values with cURL and PHP.

In this article, we’ll look at how to pass $_POST values with cURL and PHP.

How to pass $_POST values with cURL and PHP?

To pass $_POST values with cURL and PHP, we set a few options.

For instance, we write

$data = ["name" => "Ross", "php_master" => true];

$data["file"] = "@/home/user/world.jpg";

$handle = curl_init($url);
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
curl_exec($handle);
curl_close($handle);

to call curl_setopt to set CURLOPT_POST to true to make a POST request.

We call curl_setopt again to set CURLOPT_POSTFIELDS to $data to use $data as the POST request body.

Then we call curl_exec to make the request.

Finally, we call curl_close to close the handle.

Conclusion

To pass $_POST values with cURL and PHP, we set a few options.

How to stream a large file using PHP?

Sometimes, we want to stream a large file using PHP.

In this article, we’ll look at how to stream a large file using PHP.

How to stream a large file using PHP?

To stream a large file using PHP, we can use the fpassthru function.

For instance, we write

$path = "path/to/file";
$public_name = basename($path);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime_type = finfo_file($finfo, $path);

header("Content-Disposition: attachment; filename=$public_name;");
header("Content-Type: $mime_type");
header("Content-Length: " . filesize($path));

$fp = fopen($path, "rb");
fpassthru($fp);
exit();

to call header to set the header values for the file.

We set Content-Type to $mime_type.

And we Content-Length to the file size that we get from $filesize.

$public_name is the public file name, which we use as the value of filename.

Then we open the file from the $path with fopen.

Next we call fpassthru with $fp to stream the file.

Once we’re done streaming, we call exit.

Conclusion

To stream a large file using PHP, we can use the fpassthru function.

How to loop through this array in PHP?

Sometimes, we want to loop through this array in PHP.

In this article, we’ll look at how to loop through this array in PHP.

How to loop through this array in PHP?

To loop through this array in PHP, we can use a foreach loop.

For instance, we write

foreach ($array as $item) {
    echo $item["filename"];
    echo $item["filepath"];
    echo "<pre>";
    var_dump($item);
}

to loop through the $array and get the item being looped through from $item.

Then we get the values from the $item associative array in the loop body.

Conclusion

To loop through this array in PHP, we can use a foreach loop.

How to get URL query string parameters with PHP?

Sometimes, we want to get URL query string parameters with PHP.

In this article, we’ll look at how to get URL query string parameters with PHP.

How to get URL query string parameters with PHP?

To get URL query string parameters with PHP, we use the parse_url function.

For instance, we write

$url = "www.mysite.com/category/subcategory?myqueryhash";
echo parse_url($url, PHP_URL_QUERY);

to call parse_url with $url and PHP_URL_QUERY to return the query string part odf the $url as an associative array with the keys and values.

Conclusion

To get URL query string parameters with PHP, we use the parse_url function.

How to insert a row and then get ‘id’ with PHP/MySQL?

Sometimes, we want to insert a row and then get ‘id’ with PHP/MySQL.

In this article, we’ll look at how to insert a row and then get ‘id’ with PHP/MySQL.

How to insert a row and then get ‘id’ with PHP/MySQL?

To insert a row and then get ‘id’ with PHP/MySQL, we can use the mysqli_insert_id function.

For instance, we write

$link = mysqli_connect("127.0.0.1", "my_user", "my_pass", "my_db");
mysqli_query($link, "INSERT INTO mytable (1, 2, 3, 'blah')");
$id = mysqli_insert_id($link);

to connect to the database with mysqli_connect.

Then we call mysqli_query function to run the insert statement.

Finally, we get the ID of the last row inserted with mysqli_insert_id.

Conclusion

To insert a row and then get ‘id’ with PHP/MySQL, we can use the mysqli_insert_id function.

How to redirect to HTTPS with Laravel 5 and PHP?

Sometimes, we want to redirect to HTTPS with Laravel 5 and PHP.

In this article, we’ll look at how to redirect to HTTPS with Laravel 5 and PHP.

How to redirect to HTTPS with Laravel 5 and PHP?

To redirect to HTTPS with Laravel 5 and PHP, we can use the \URL::forceScheme method.

For instance, we write

\URL::forceScheme("https");

to call \URL::forceScheme with "https" to force the app to be served over https.

Conclusion

To redirect to HTTPS with Laravel 5 and PHP, we can use the \URL::forceScheme method.