/query.php

https://bitbucket.org/wlynch92/cs336-dbproject · PHP · 50 lines · 39 code · 9 blank · 2 comment · 6 complexity · 6996dce8ec4a206b151f2b4438b94460 MD5 · raw file

  1. <?php
  2. function query(){
  3. $input=$_POST['input'];
  4. echo $input."\n<br/>\n";
  5. $arr = explode(' ',trim($input));
  6. if (strtolower($arr[0])=="select"){
  7. $con = mysql_connect('cs336-64','csuser','cs277315');
  8. if (!$con){
  9. die(mysql_error());
  10. }
  11. mysql_select_db("cs336",$con);
  12. //echo $query."\n";
  13. $res = mysql_query($input,$con);
  14. // If any errors, exit. This includes empty results
  15. if (!$res){
  16. die(mysql_error());
  17. }
  18. for($i=0;$i<mysql_num_fields($res)-1;$i++){
  19. echo mysql_field_name($res,$i)." | ";
  20. }
  21. echo mysql_field_name($res,$i)."\n<br/>\n";
  22. while ($row = mysql_fetch_array($res)){
  23. for($i=0;$i<mysql_num_fields($res)-1;$i++){
  24. echo $row[$i]." | ";
  25. }
  26. echo $row[$i]."\n";
  27. echo "\n<br/>\n";
  28. }
  29. mysql_close($con);
  30. }
  31. }
  32. ?>
  33. <html>
  34. <body>
  35. <form action="query.php" method="post">
  36. Enter Query: <input type="text" name="input"><br>
  37. <input type="submit">
  38. </form>
  39. <?php query() ?>
  40. </body>
  41. </html>