MySQL Examples


MySQL Examples& PERL code snippets15 Jun 2006 12:21 pm

Like all programing languages, there are many ways to connect to databases, this in particular shows a the simplest method, though burying the host, username and password in an include would be better for security reasons, this is effective enough for most purposes.

The only requirements for perl may be the DBD::mysql module if not already installed, this should get all the needed prerequisites that may not be present. Either download them directly from CPAN or use the interactive installer shell

CODE:
  1. $perl -MCPAN -e shell
  2. $install  DBD::mysql

To use this example copy this code into your script, edits $username, $password, and $db_name values to suit your environment.

CODE:
  1. #Invoke the Perl Database Libraries
  2. use DBI;
  3.  
  4.  
  5. # MySQL server hostname
  6. my $host = "mysql.serverhost.com";
  7.  
  8. #your account username and MySQL password
  9. my $username = "your_user";
  10. my $password = "your_MySQL_password";
  11.  
  12.  
  13. #Edit this to point to the database you wish to connect to
  14. my $db_name = "your_database";
  15.  
  16.  
  17. #
  18. #The following lines do not need to be edited
  19. #
  20. #put the database and server in to the connect statement
  21. $dsn = "DBI:mysql:database=$db_name;host=$host";
  22.  
  23.  
  24. #Generate the full connect statement
  25. $dbh = DBI->connect($dsn, $username, $password);
  26.  
  27.  
  28. #run the connect statement
  29. $self->dbh = $dbh;

MySQL Examples31 May 2006 10:09 am

A quick query structure to retrive the last row entry to a logging table, in this case there can be multiple rows that would return the same id and date but I want only the very last so this query works well.

CODE:
  1. $query="SELECT a.user_id, a.access_date FROM superlog a
  2. WHERE a.access_date = (SELECT MAX(b.access_date)
  3.             FROM superlog b
  4.             WHERE a.user_id=b.user_id)
  5. AND ip = CONVERT( _utf8 '10.10.10.10' USING latin1 )
  6. COLLATE latin1_swedish_ci;

MySQL Examples08 May 2006 10:43 am

The Drop command is used to delete all the records in a table using the table name as shown below:

Syntax:

$dropSQL=("DROP tblName");

Example

CODE:
  1. $dropSQL=("DROP tblstudent");

MySQL Examples08 May 2006 10:42 am

The Update command is used to update the field values using conditions. This is done using 'SET' and the fieldnames to assign new values to them.

Syntax:

$updateSQL=("UPDATE Tblname SET (fieldname1=value1,fieldname2=value2,...) WHERE fldstudid=IdNumber");

Example:

CODE:
  1. $updateSQL=("UPDATE Tblstudent SET (fldstudName=siva,fldstudmark=100) WHERE fldstudid=2");

MySQL Examples08 May 2006 10:42 am

The Insert command is used to insert records into a table. The values are assigned to the field names as shown below:

Syntax:

$insertSQL=("INSERT INTO tblname(fieldname1,fieldname2..) VALUES(value1,value2,...) ");

Example

CODE:
  1. $insertSQL=("INSERT INTO Tblstudent(fldstudName,fldstudmark)VALUES(Baskar,75) ");

Next Page »


Flash Memory - Loans - Mobile Phones - Homeowner Loans - Consolidation Loans
X10 Home Security|Dakar's Photos
Listed on BlogShares