2
I have been perfecting the techniques of safe use with database connection and arrived at the script below.
<?php class query_sql {
protected static $conect;
private function __construct() {
$mysql_host = "";
$mysql_base = "";
$mysql_user = "";
$mysql_pass = "";
try { self::$conect = new PDO("mysql:host=" . $mysql_host . "; dbname=" . $mysql_base, $mysql_user, $mysql_pass);
self::$conect -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
self::$conect -> exec("set names utf8");
$conect = null; }
catch(PDOException $error) {
echo $error -> getMessage(); } }
public static function conect_sql() {
if (!self::$conect) {
new query_sql; }
return self::$conect; } }
$bind_sql = query_sql::conect_sql(); ?>
my next step is to learn how to send
$mysql_host = "";
$mysql_base = "";
$mysql_user = "";
$mysql_pass = "";
together with the values below
<?php $list_information = $bind_sql -> prepare("SELECT user_email, user_name FROM usuarios WHERE user_email = :user_email LIMIT 1");
$list_information -> bindValue(":user_email", "@");
$list_information -> execute();
$list_information = $list_information -> fetchAll(); ?>
only list above and compare below (login as example)
<?php $compare_information = $bind_sql -> prepare("SELECT user_email, user_name FROM usuarios WHERE user_email = :user_email AND user_name = :user_name LIMIT 1");
$statement_array = array(":user_email" => "@", ":user_name" => "*");
foreach($statement_array as $array_key => $array_value) {
$compare_information -> bindValue($array_key, $array_value); }
$compare_information -> execute();
$compare_information = $compare_information -> fetchAll(); ?>
To receive so
<?php foreach($list_information as $list_information) {
echo $list_information["user_email"] . " | " . $list_information["user_name"]; } ?>
and so
<?php foreach($compare_information as $compare_information) {
echo $compare_information["user_email"] . " | " . $compare_information["user_name"]; } ?>
I just need a little help as to what I can learn so that I can send the variables into the class so they don’t have to be in there, which is why I have a database for every piece of information, the destination of this script uses 4 databases so I would define which database to use in each prepare (select, delete, att etc.)
First you should understand about visibility. It is mixing with static methods and the constructor is as private. http://php.net/manual/en/language.oop5.visibility.php
– Daniel Omine