How to fix the mysqli_query() expects at least 2 parameters, 1 given error with PHP?

Sometimes, we want to fix the mysqli_query() expects at least 2 parameters, 1 given error with PHP.

In this article, we’ll look at how to fix the mysqli_query() expects at least 2 parameters, 1 given error with PHP.

How to fix the mysqli_query() expects at least 2 parameters, 1 given error with PHP?

To fix the mysqli_query() expects at least 2 parameters, 1 given error with PHP, we can use prepared statements.

For instance, we write

$stmt = $mysqli->prepare(
    "INSERT INTO `counter`.`hits` (`page_hits`) VALUES (?)"
);
$stmt->bind_param("s", $hits);
$stmt->execute();

to call prepare with the SQL query string to make a prepared statement object.

Then we call bind_params to bind the $hits value to the ? placeholder in the SQL string.

And then we call execute to run the prepared statement.

Conclusion

To fix the mysqli_query() expects at least 2 parameters, 1 given error with PHP, we can use prepared statements.

How to display image BLOB from MySQL with PHP?

Sometimes, we want to display image BLOB from MySQL with PHP.

In this article, we’ll look at how to display image BLOB from MySQL with PHP.

How to display image BLOB from MySQL with PHP?

To display image BLOB from MySQL with PHP, we can call a few functions.

To insert a file into the database, we write

$db = new mysqli("localhost", "root", "", "DbName");
$image = file_get_contents($_FILES["images"]["tmp_name"]);
$query = "INSERT INTO products (image) VALUES(?)";
$stmt = $db->prepare($query);
$stmt->bind_param("s", $image);
$stmt->execute();

to call file_get_contents to get the file from the $_FILES array.

Anbd then we call bind-param with 's' and $image to bind theparameter to the $query.

And then we call execute to run the query.

Next, we get the file and display it with

$db = new mysqli("localhost", "root", "", "DbName");
$sql = "SELECT * FROM products WHERE id = ?";
$stmt = $db->prepare($sql);
$stmt->bind_param("s", $id);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_array();
echo '<img src="data:image/jpeg;base64,' . base64_encode($row["image"]) . '"/>';

We make a query to get the file with prepare, bind_param, and execute.

And then we call get_result and fetch_array to get the result data as an associative array.

Finally, we display the image after calling base64_encode with the returned image to convert it to base64 string.

Then we use that has the src attribute value for the img element.

Conclusion

To display image BLOB from MySQL with PHP, we can call a few functions.

How to get remote file size without downloading file with PHP?

Sometimes, we want to get remote file size without downloading file with PHP.

In this article, we’ll look at how to get remote file size without downloading file with PHP.

How to get remote file size without downloading file with PHP?

To get remote file size without downloading file with PHP, we can use the curl_getinfo function.

For instance, we write

function retrieve_remote_file_size($url)
{
    $ch = curl_init($url);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_NOBODY, true);

    $data = curl_exec($ch);
    $size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);

    curl_close($ch);
    return $size;
}

to define the retrieve_remote_file_size function.

In it, we call curl_init with $url to get the $ch object.

Then we call curl_getinfo with $ch and CURLINFO_CONTENT_LENGTH_DOWNLOAD to get the remote file’s size.

And then we call curl_close with $ch to close the connection.

Conclusion

To get remote file size without downloading file with PHP, we can use the curl_getinfo function.

How to fix ‘Unable to send email using Gmail SMTP server through PHPMailer, getting error: SMTP AUTH is required for message submission on port 587’ error with PHP?

Sometimes, we want to fix ‘Unable to send email using Gmail SMTP server through PHPMailer, getting error: SMTP AUTH is required for message submission on port 587’ error with PHP.

In this article, we’ll look at how to fix ‘Unable to send email using Gmail SMTP server through PHPMailer, getting error: SMTP AUTH is required for message submission on port 587’ error with PHP.

How to fix ‘Unable to send email using Gmail SMTP server through PHPMailer, getting error: SMTP AUTH is required for message submission on port 587’ error with PHP?

To fix ‘Unable to send email using Gmail SMTP server through PHPMailer, getting error: SMTP AUTH is required for message submission on port 587’ error with PHP, we set the SMTPSecure property to 'tls'.

For instance, we write

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->IsHTML(true);
$mail->Username = "email@gmail.com";
$mail->Password = "password";
$mail->SetFrom("example@gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("email@gmail.com");

if (!$mail->Send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message has been sent";
}

to create the PHPMailer $mail object.

We set SMTPSecure to send emails over TLS.

Then we set IsHTML to true to send HTML email.

And then we set the Username and Password to send emails with the HTML server.

After that we set the from, subject, body, and to address with

$mail->SetFrom("example@gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("email@gmail.com");

Finally, we send the email with Send.

Conclusion

To fix ‘Unable to send email using Gmail SMTP server through PHPMailer, getting error: SMTP AUTH is required for message submission on port 587’ error with PHP, we set the SMTPSecure property to 'tls'.

How to remove the public folder from URL with Laravel and PHP?

Sometimes, we want to remove the public folder from URL with Laravel and PHP.

In this article, we’ll look at how to remove the public folder from URL with Laravel and PHP.

How to remove the public folder from URL with Laravel and PHP?

To remove the public folder from URL with Laravel and PHP, we can add a few settings into the .htaccess file.

For instance, we write

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]

to rewrite all base URLs top the /public folder.

How to allow PHP sessions to carry over to subdomains?

Sometimes, we want to allow PHP sessions to carry over to subdomains.

In this article, we’ll look at how to allow PHP sessions to carry over to subdomains.

How to allow PHP sessions to carry over to subdomains?

To allow PHP sessions to carry over to subdomains, we can make some setting changes.

In php.ini, we can write

session.cookie_domain = ".example.com"

Another way is to change the .htaccess file by writing:

php_value session.cookie_domain .example.com

Or in our script, we write

ini_set('session.cookie_domain', '.example.com' );

Another way is to change our php-fpm pool configuration for our site by writing

php_value[session.cookie_domain] = .example.com

Conclusion

To allow PHP sessions to carry over to subdomains, we can make some setting changes.

How to access @attribute from SimpleXML with PHP?

Sometimes, we want to access @attribute from SimpleXML with PHP.

In this article, we’ll look at how to access @attribute from SimpleXML with PHP.

How to access @attribute from SimpleXML with PHP?

To access @attribute from SimpleXML with PHP, we can use the attributes method.

For instance, we write

$xml->attributes()->{'field'};

to get the @attributes values with attributes.

Then we use {'field'} to get the value of the field with key 'field'.

Conclusion

To access @attribute from SimpleXML with PHP, we can use the attributes method.

How to flatten a multi-dimensional array to simple one in PHP?

Sometimes, we want to flatten a multi-dimensional array to simple one in PHP.

In this article, we’ll look at how to flatten a multi-dimensional array to simple one in PHP.

How to flatten a multi-dimensional array to simple one in PHP?

To flatten a multi-dimensional array to simple one in PHP, we call the array_merge function.

For instance, we write

$notFlat = [[1, 2], [3, 4]];
$flat = array_merge(...$notFlat);
var_dump($flat);

to call array_merge with the arrays in the $notFlat array as arguments.

Then $flat is an array with the entries in each nested array.

Conclusion

To flatten a multi-dimensional array to simple one in PHP, we call the array_merge function.

How to convert a PHP object to an associative array?

Sometimes, we want to convert a PHP object to an associative array.

In this article, we’ll look at how to convert a PHP object to an associative array.

How to convert a PHP object to an associative array?

To convert a PHP object to an associative array, we can cast the object directly into an array.

For instance, we write

$array = (array) $yourObject;

to cast the $yourObject object directly to an associative array by putting (array) before $yourObject.

Conclusion

To convert a PHP object to an associative array, we can cast the object directly into an array.

How to extract img src, title, and alt from HTML using PHP?

Sometimes, we want to extract img src, title, and alt from HTML using PHP.

In this article, we’ll look at how to extract img src, title, and alt from HTML using PHP.

How to extract img src, title, and alt from HTML using PHP?

To extract img src, title, and alt from HTML using PHP, we can parse the HTML with the DOMDocument class.

For instance, we write

$url = "http://example.com";

$html = file_get_contents($url);

$doc = new DOMDocument();
@$doc->loadHTML($html);

$tags = $doc->getElementsByTagName("img");

foreach ($tags as $tag) {
    echo $tag->getAttribute("src");
}

to call file_get_contents to get the HTML from the $url.

Then we create a new DOMDocument object and call loadHTML with the $html string.

And then we get the img element with getElementsByTagName.

Then we use the foreach loop to loop through the elements and call getAttribute to get the src attribute of each img element.

Conclusion

To extract img src, title, and alt from HTML using PHP, we can parse the HTML with the DOMDocument class.