PHP read file, update database, SQL
—
search database for part:
connect_errno) {
// echo “Failed to connect to MySQL: ” . $mysqli->connect_error;
exit();
}
// Set the INSERT SQL data
$sql = “SELECT * FROM ” . $table . ” WHERE part='” . $query . “‘ ORDER BY lot”;
// Process the query so that we will save the date of birth
$results = $mysqli->query($sql);
if ($results === false) {
echo “Query failed: ” . mysqli_error($conn);
}
// else if ($result === true)
// echo “Query executed successfully.\n”;
// Fetch Associative array
$row = $results->fetch_all(MYSQLI_ASSOC);
// Free result set
$results->free_result();
// Close the connection after using it
$mysqli->close();
echo json_encode($row);
——
download file:
connect_errno) {
echo “Failed to connect to MySQL: ” . $mysqli->connect_error;
exit();
}
else
echo “Connected to database.\n”;
if($action == ‘clear’ ) {
// empty table OFF HOURS ONLY
$sql = “TRUNCATE TABLE ” . $table;
if(mysqli_query($conn, $sql))
echo $table . ” Table emptied successfully.\n “;
else
die (“Could not empty table” . $table . “.\n”);
}
// Read in the names of all files
$lines = glob($directory . ‘*.*’);
// Loop through the file names and insert them into the database
foreach ($lines as $line) {
if (strpos($line,’@’) !== false) // if have @ sign
continue; // skip it))
$line = str_replace($directory,”,$line); // remove directory from name
$line = mysqli_real_escape_string($conn,$line);
$pos = strpos($line,”#”);
if ($pos == 0) // if don’t have point sign
$pos = strpos($line,” “);
$part = substr($line,0,$pos);
$part = trim($part);
$pos2 = strpos($line,”.”);
$lot = substr($line,$pos+1,$pos2-$pos-1);
if ($action == ‘clear’ ) {
$sql = “INSERT INTO ” . $table . ” (part, lot, link) VALUES (‘” .$part. “‘,'” .$lot. “‘,'” .$line.”‘)”;
}
else if ($action == ‘update’ ) {
// $sql = “INSERT INTO ” .$table. ” (part, lot, link) VALUES (‘” .$part. “‘,'” .$lot. “‘,'” .$line. “‘) ON DUPLICATE KEY UPDATE link = ‘” .$line. “‘”;
$sql = “INSERT IGNORE INTO ” .$table. ” (part, lot, link) VALUES (‘” .$part. “‘,'” .$lot. “‘,'” .$line. “‘)”;
}
else
die (“Invalid action: ” . $action . “\n”);
//perform the query
$result = mysqli_query($conn, $sql);
if ($result === false) {
echo “Query failed: ” . mysqli_error($conn);
}
else if ($result === true)
echo “Query executed successfully.\n”;
}
// Close the database connection
mysqli_close($conn);
?>