PHP code example of ypw / friendofmysql

1. Go to this page and download the library: Download ypw/friendofmysql library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ypw / friendofmysql example snippets





/**
 * FriendOfMysql.
 * PHP Version =>7.4.
 *
 * @see https://github.com/yolfry/FriendOfMysql/ The FriendOfMysql GitHub project
 *
 * @author    yolfry (ypw) <[email protected]>
 * @copyright 2015 - 2021 yolfry
 * @license   http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
 * @note      This program is distributed in the hope that it will be useful - WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 */



/*
 * QueryApp structure
 * */

/*
* Input variables that the query block receives:
*
* $cmd           Command to select the query on the switch
* $connection    Object variable, which contains the structure of the connection
* $data          Array containing the data to send by the query
*/


/*
*   @param string $cmd Query block name
*/


try {
    switch ($cmd) {

            /* 
            * * * * *  * * * * * * * * * * * 
            *          Insert user         *
            * * * * * * * * * * * * * * * * *
            */

        case 'insertUser':
            $sql = "INSERT INTO `user` (name) VALUES(?);";

            if (!($sentencia = $connection->prepare($sql))) {
                throw new Exception("Preparation failed: (" . $connection->errno . ") " . $connection->error);
            }

            if (!$sentencia->bind_param("s", $data['name'])) {
                throw new Exception("Parameter binding failed: (" . $sentencia->errno . ") " . $sentencia->error);
            }

            if (!$sentencia->execute()) {
                throw new Exception("Execution failed: (" . $sentencia->errno . ") " . $sentencia->error);
            }

            $sentencia->close();

            $this->res = true;
            break;

            /* 
            * * * * *  * * * * * * * * * * * 
            *         Select user          *
            * * * * * * * * * * * * * * * * *
            */
        case 'selectUser':

            $sql = "SELECT * FROM `user`";

            if (!($sentencia = $connection->prepare($sql))) {
                throw new Exception("Preparation failed: (" . $connection->errno . ") " . $connection->error);
            }

            if (!$sentencia->execute()) {
                throw new Exception("Execution failed: (" . $sentencia->errno . ") " . $sentencia->error);
            }

            $datos = $sentencia->get_result();

            $field = array();
            $table = array();
            $i = 0;

            while ($field = $datos->fetch_array(MYSQLI_BOTH)) {
                $table[$i]['userID'] = strip_tags($field['userID']);
                $table[$i]['name'] = strip_tags($field['name']);
                $i++;
            }

            $sentencia->close();

            $this->res = $table;/*return parameter*/
            break;

        default:
            $this->res = false;
            break;
    }
} catch (\Throwable $e) {
    echo $e->getMessage();
    $this->res = false;
}






use ypw\FOM;

M(true);

$FOM->type = 'MySQLi_Object';
$FOM->serverDB ='127.0.0.1';
$FOM->nameDB = 'system';
$FOM->userDB = 'root';
$FOM->passDB = '123456789';
$FOM->port = '3306';

$FOM->fileQuery = __DIR__ ."/queryApp.php";


$data['name'] = 'Alejandro';
$FOM->sed('insertUser',$data);


if($FOM->res){
    echo "\n Registered user";
}else
{
    echo "\n Unregistered user";
}

/*
* sed 2, Select User and print
*/


$FOM->sed('selectUser');

if($FOM->res){
    echo json_encode($FOM->res);
}else{
    echo "\n Unable to select user user";
}