domingo, 23 de marzo de 2025

php class example

 class Hombre {

  private $name;

  private $age;


  // Constructor to initialize the name and age

  function __construct($name, $age = 1) {

    // If the name length is less than 3, generate a unique ID as the name

    $this->name = (strlen($name) < 4) ? $name.'_'.uniqid() : $name;

    $this->age = $age;

    echo "$this->name has been created<br>"; 

  }


  // Method to change the name

  function changeName($name) {

    $this->name = $name;

  }


  // Method to get the name

  function getName() {

    return $this->name; 

  }


  // Method to get the age if needed

  function getAge() {

    return $this->age;

  }

}


// Creating an object with the name "a" and age 34

$m = new Hombre('a', 34);  // name will be replaced with a unique ID


// Changing the name to "Mario Rodriguez"

$m->changeName('Mario Rodriguez');


// Outputting the new name

echo $m->getName();  // Outputs: Mario Rodriguez

miércoles, 19 de marzo de 2025

Insert delete function mode

 <?php

function Updatedb($query){



mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);




// Create a connection to the database


$mysqli = new mysqli("localhost", "my_user", "my_password", "world");




// Check for connection errors


if ($mysqli->connect_error) {


    die("Connection failed: " . $mysqli->connect_error);


}




// Check if table exists before creating it


//if ($mysqli->query("SHOW TABLES LIKE 'myCity'")->num_rows == 0) {


    // If the table doesn't exist, create it by duplicating the structure of the 'City' table


  //  if (!$mysqli->query("CREATE TABLE myCity LIKE City")) {


    //    die("Error creating table: " . $mysqli->error);


    //}


//} else {


  //  echo "Table 'myCity' already exists.\n";


//}




// Insert a new record into 'myCity'


//$query = "INSERT INTO myCity (ID, Name, CountryCode, District, Population) VALUES (NULL, 'Stuttgart', 'DEU', 'Stuttgart', 617000)";


if ($mysqli->query($query)) {


    printf("New record has ID %d.\n", $mysqli->insert_id);


} else {


    echo "Error inserting record: " . $mysqli->error;


}




// Close the connection


$mysqli->close();



}

//$query = "INSERT INTO myCity (ID, Name, CountryCode, District, Population) VALUES (NULL, 'Stuttgart', 'DEU', 'Stuttgart', 617000)";

Updatedb($query);

?>


martes, 18 de marzo de 2025

select function

   <?php

function select($query){


mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);  // 1 enable reporting


$mysqli = new mysqli("localhost", "my_user", "my_password", "world");  // 2 connect to the db


if ($mysqli->connect_error) {                                     // 3 check connection error

    /* Use your preferred error logging method here */

    error_log('Connection error: ' . $mysqli->connect_error);

    exit();

}


//$query = "SELECT Name, CountryCode FROM City ORDER BY ID LIMIT 3";  // 4 corrected query


// Execute the query and check for errors

$result = $mysqli->query($query); 

if (!$result) {  // Check if the query failed

    printf("Error message: %s\n", $mysqli->error);

    exit();

}


$mysqli->close();  // 5 Close the connection as soon as it's no longer needed


// Fetch results as an associative array

return $rows = $result->fetch_all(MYSQLI_ASSOC);  // 6 print result as assoc array

//foreach ($rows as $row) {

  //  printf("%s (%s)\n", $row["Name"], $row["CountryCode"]);

//}

print_r(select($query =$argv[1]\));


?>


 php select.php "select * from users;" 

 Array

(

    [0] => Array

        (

            [userId] => 60

            [phoneNumber] => 12127960900

            [accessCode] => 593161

            [pin] => 1234

            [nameURL] => https://rdasteriskrecordings.s3.amazonaws.com/12127960900.wav

            [lastCalled] => 2025-03-06

            [userLang] => en

            [userAuthMode] => cid

        )


    [1] => Array

        (

            [userId] => 61

            [phoneNumber] => 100001

            [accessCode] => 406366

            [pin] => 1234

            [nameURL] => https://rdasteriskrecordings.s3.amazonaws.com/2100001.wav

            [lastCalled] => 2025-03-06

            [userLang] => en

            [userAuthMode] => cid

        )


)


lunes, 10 de marzo de 2025

mysqi step by step

 <?php


mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);  // 1 enable reporting


$mysqli = new mysqli("localhost", "my_user", "my_password", "world");  // 2 connect to the db


if ($mysqli->connect_error) {                                     // 3 check connection error

    /* Use your preferred error logging method here */

    error_log('Connection error: ' . $mysqli->connect_error);

    exit();

}


$query = "SELECT Name, CountryCode FROM City ORDER BY ID LIMIT 3";  // 4 corrected query


// Execute the query and check for errors

$result = $mysqli->query($query); 

if (!$result) {  // Check if the query failed

    printf("Error message: %s\n", $mysqli->error);

    exit();

}


$mysqli->close();  // 5 Close the connection as soon as it's no longer needed


// Fetch results as an associative array

$rows = $result->fetch_all(MYSQLI_ASSOC);  // 6 print result as assoc array

foreach ($rows as $row) {

    printf("%s (%s)\n", $row["Name"], $row["CountryCode"]);

}


?>


sábado, 8 de marzo de 2025

sábado, 15 de febrero de 2025

SANITIZE_NUMBER

<?php 

 $nums="12a";

echo filter_var($nums,FILTER_SANITIZE_NUMBER_INT); //12


//https://www.php.net/manual/en/filter.constants.php#constant.filter-sanitize-string

//https://www.php.net/manual/en/function.filter-var.php


?>

Date forma option

 https://www.php.net/manual/en/datetime.format.php