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

viernes, 14 de febrero de 2025

save csv on db

 <?php

// Database connection

$mysqli = new mysqli("localhost", "username", "password", "database_name");


// Check for connection error

if ($mysqli->connect_error) {

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

}


// Open the CSV file

if (($handle = fopen("data.csv", "r")) !== FALSE) {

    // Skip the first line if it contains the header

    fgetcsv($handle);


    // Prepare the SQL query for inserting data

    $stmt = $mysqli->prepare("INSERT INTO users (name, email, age) VALUES (?, ?, ?)");

    $stmt->bind_param("ssi", $name, $email, $age); // 'ssi' means string, string, integer


    // Read each line of the CSV and insert the data into the database

    while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {

        $name = $data[0];    // First column: name

        $email = $data[1];   // Second column: email

        $age = (int)$data[2]; // Third column: age (cast to integer)


        // Execute the prepared statement

        if (!$stmt->execute()) {

            echo "Error inserting record: " . $stmt->error . "<br>";

        }

    }


    // Close the prepared statement and file handle

    $stmt->close();

    fclose($handle);

} else {

    echo "Error opening the file.";

}


// Close the database connection

$mysqli->close();

?>


convert seconds to minutes

 function MinSecs($time){

    $Seconds = $time % 60;  // Seconds is the remainder when divided by 60

    $Minutes = floor($time / 60);  // Minutes is the total time divided by 60, rounded down

    $arr = [$Minutes, $Seconds];  // Return minutes first, then seconds


    return $arr;

}


print_r(MinSecs(841));

prepare sttament

 <?php


mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

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


// Check for connection errors

if ($mysqli->connect_error) {

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

}

/* Prepare an insert statement */

$stmt = $mysqli->prepare("INSERT INTO myCity (Name, CountryCode, District) VALUES (?,?,?)");


// Check if the statement was prepared successfully

if (!$stmt) {

    die("Error preparing the statement: " . $mysqli->error);

}


/* Bind variables to parameters */

$stmt->bind_param("sss", $val1, $val2, $val3);


$val1 = 'Stuttgart';

$val2 = 'DEU';

$val3 = 'Baden-Wuerttemberg';


/* Execute the statement */

if ($stmt->execute()) {

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

} else {

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

}


// Close the connection

$mysqli->close();

?>

miércoles, 12 de febrero de 2025

martes, 11 de febrero de 2025

insert

<?php


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();

?>