Skip to main content

SQL coding examples

This SQL and PHP will count how many rows contain unique elements in your database.   For example, you may have a database of user entries and want to know how many entries each user has.   In this example, you want to know how many rows contain each unique element "name":

$query = mysql_query("SELECT name, COUNT(*) AS number FROM $table GROUP BY name");
if ( mysql_num_rows($query) == 0 ) echo "No rows in the database";
while ($row = mysql_fetch_array($query)) {
  $name = $row['name'] ;
  $count = $row['number'] ;
  echo "$name has $count rows in the database<br>";
}

Using PHP/MySQL scripts at your host and at home
If you have PHP/MySQL scripts that you want to use both at your host and on your home machine, you can add a few script lines that will allow you to do that without requiring any further modifications when you transfer your scripts back and forth.  In your MySQL configuration statements include a simple "if test" to see if you are on the remote server or on your home machine and set the parameters accordingly.

- - Start Script Here - -
<?phpif($_SERVER['HTTP_HOST'] == "localhost") {
  
$host="localhost";
} else {
  
$host="your-host-mysql-server.com";
}
?>

- - End Script Here - -

The script example above tests for the domain name to see if it is executing on your home machine (localhost in this example), then sets the configuration accordingly.


MySQL Error Notification
Would you like to be notified when your site has a MySQL error?  This PHP script will trap MySQL errors, send the user a custom error page, and email you with the details of the error.  You can customize this example to launch other pages based on the type of error, or to have the error routine do what ever you want.

A typical MySQL statement should look like this:

- - Start Script Here - -
<?php
mysql_connect
("$host""$dbuser""$dbpswd") or sql_error("connect",mysql_error());?>

- - End Script Here - -

When there is an error, this will call the function sql_error, sending a marker (in this case "connect") and the actual MySQL error.

At the top of your script (before any MySQL statements) put the function sql_error:

- - Start Script Here - -
<?phpfunction sql_error($function,$mysql_err) {
  echo 
"<center><br><hr>
  Oops . . . looks like our database is having technical problems.
  <br><br>
  Please come back later . . . hopefully we can get things up and running again soon.
  <hr>"
;
  
$error_time = (date(" F d h:ia"));
  
$message .= "There has been a MySQL error.\n\n";
  
$message .= "Error: $function - $mysql_err\n\n";
  
$message .= "Server time of the error: $error_time\n\n";
  
mail("youremail@domain.com""MySQL error" $message"From: Website <>");
  die();
}
?>

- - End Script Here - -


Automated MySQL Backups
Here is a php script that will backup your MySQL database, creating a different file each day for a month (then start over) so you have 30 days of backups.  It will email you with a backup confirmation.  Run it every night using cron to kick it off.  Replace the X's with your information.  In this example the backups will be placed in your FTP Root directory.

- - Start Script Here - -
<?php
$emailaddress 
"XXXXXX@yourdomain.com";$host="XXXXXX"// database host$dbuser="XXXXXX"// database user name$dbpswd="XXXXXX"// database password$mysqldb="XXXXXX"// name of database$day = (date("d"));$filename "/full_path_to_file_goes_here/backup" $day ".sql";
if ( 
file_exists($filename) ) unlink($filename);system"mysqldump --user=$dbuser --password=$dbpswd --host=$host $mysqldb > $filename",$result);$size filesize($filename);$runtime = (date(" F d h:ia"));$message .= "The backup has been run.\n\n";$message .= "The return code was: $result\n\n";$message .= "Size of the backup: $size bytes\n\n";$message .= "Server time of the backup: $runtime\n\n";mail($emailaddress"Backup Message" $message"From: Website <>"); ?> 
- - End Script Here - -

You can restore the database using the following:

system( "mysql --user=$dbuser --password=$dbpswd --host=$host $mysqldb < $filename",$result);

Note that hosts may have different paths for the mysql commands.  For example /usr/local/bin/mysqldump could be required.

Comments

Popular posts from this blog

links

0.  https://michael67654.qowap.com/64523001/new-pos-technique-to-perk-up-your-company 1.  https://johnnydinqr.blog2learn.com/52856602/new-pos-system-to-perk-up-your-company 2.  http://edwinsqgcu.onesmablog.com/New-POS-Process-to-Perk-Up-Your-company-43309737 3.  http://chloe69246.bloguetechno.com/New-POS-Process-to-Perk-Up-Your-Business-39814721 4.  http://jacob87541.pointblog.net/New-POS-Technique-to-Perk-Up-Your-online-business-43741223 5.  http://arlette53302.thezenweb.com/New-POS-System-to-Perk-Up-Your-online-business-41464611 6.  http://devinpixna.tinyblogging.com/New-POS-Program-to-Perk-Up-Your-online-business-46354577 7.  https://rylanevsom.blog5.net/46759651/new-pos-program-to-perk-up-your-small-business 8.  https://mariowoesh.affiliatblogger.com/56515206/new-pos-system-to-perk-up-your-organization 9.  https://liam66429.diowebhost.com/60164326/new-pos-procedure-to-perk-up-your-company 10.  https://henry19219.fitnell.com/4557...

Raisonnement, la résolution de problèmes

Les chercheurs ont d'abord développé des algorithmes mimétiques raisonnement humain étapes que les gens utilisent pour résoudre le casse-tête ou faire méthode d'exclusion logique. [2] Dans les années 1980 et 1990 fin, l'étude de la grippe aviaire a développé des méthodes de traitement de l'information incertaine ou incomplète, en utilisant des concepts de probabilité et de l'économie. [3] Pour ces problèmes, les algorithmes requis matériel assez puissant pour effectuer des calculs de géant - à subir « combinaisons d'explosion »: la quantité de mémoire et le temps de calcul peut devenir invisible prendre si la résolution d'un problème difficile. La plus haute priorité est l'algorithme de recherche pour résoudre le problème.  Les gens utilisent généralement les jugements rapides et intuitifs plutôt que pas de déduction que les chercheurs en IA d'origine peuvent simuler. [5] Amnesty International a progressé en utilisant la résolution de problèmes « c...

Tracking and Securing Downloads

If you want to report or track downloads from your website, try this script.   This script will send you an email every time you have a download.   The email will tell you what file was downloaded and who did the download.   You could change this script to keep counts (store them in flat file or MySQL) if you desire. The variable $directory is the directory where the download files are located.   If you want the script in the same directory as the files then use "./" as the directory (you must always have the slash). In your html page, use the following structure as your download link (where name.txt is the file name to download): <a href="download.php?file=name.txt">download</a> Then you use the following script (called download.php): - - Start Script Here - - <?php $emailaddress  =  "email@yourdomain.com" ; $filename  =  $_GET [ 'file' ]; $directory  =  "downloads/" ; $path  =  "$directory$filename" ; putenv (...