Advertisement
AVONnadozie

ExecuteQuery

May 26th, 2014
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.69 KB | None | 0 0
  1. <?php
  2.  
  3. //A script to execute sql queries
  4. //I'll prefer if result can be sent as XML
  5. //But you can just echo them if it's not possible
  6. //NB: error messages should be prefixed with ERROR:
  7.  
  8. $uname = filter_input(INPUT_GET, 'u');
  9. $pwd = filter_input(INPUT_GET, 'p');
  10. $type = filter_input(INPUT_GET, 'sql_type');
  11. $query = filter_input(INPUT_GET, 'q');
  12.  
  13. $con = mysql_connect('localhost', $uname, $pwd);
  14. if ($con) {
  15.     mysql_select_db('csfonlin_db1');
  16.  
  17.     if (strcasecmp($type, 'select') == 0) {
  18.         //For MySql select statements
  19.        
  20.         //Steps:
  21.         //Execute statement
  22.         //if successful, send result
  23.         //Else send error message
  24.     } elseif (strcasecmp($type, 'batch') == 0) {
  25.         //For a MySql transaction (i.e a group of statements)
  26.         //These group of statements are seperated by ';'
  27.         $querys = explode(';', $query);
  28.  
  29.         //Steps:
  30.         //In a loop, execute each statement
  31.         //if error occurs, send an error messages indicating the index of the query at which the error occurred
  32.         //If all successful, send an ok message
  33.     } elseif (strcasecmp($type, 'update') == 0) {
  34.         //For other MySql statements (This is for statements that do not return data)
  35.        
  36.         //Steps:
  37.         //Execute statement
  38.         //Send an ok message if successful
  39.         //else, send an error message
  40.     } else {
  41.         //Send an error message
  42.         //e.g
  43.         echo error_message('Unknown sql type');
  44.     }
  45. } else {
  46.     //Send an error message
  47.     //e.g
  48.     echo error_message('Unable to connect to database');
  49. }
  50.  
  51. mysql_close($con);
  52.  
  53. function error_message(string $message) {
  54.     return 'ERROR: ' . err_msg;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement