Ver Mensaje Individual
  #2 (permalink)  
Antiguo 31/05/2011, 22:44
Avatar de memoadian
memoadian
Colaborador
 
Fecha de Ingreso: junio-2009
Ubicación: <?php echo 'México'?>
Mensajes: 3.696
Antigüedad: 14 años, 10 meses
Puntos: 641
Respuesta: Consulta programa PHP para realizar consultas SQL

Código PHP:
Ver original
  1. <! -- Program: mysql_send.php
  2.       Desc:   PHP Program thar sends an SQL query to the
  3.         MySQL server and displays the results
  4. -->
  5. <html>
  6. <head><title>SQL Quey Sender</title></head>
  7. <body>
  8. <?php
  9.  
  10. $host="aca va el host";
  11. $user=" aca va el user ";
  12. $password="aca va la contraseña";
  13. /* Section that executes query */
  14. if (@$_GET['form'] == "yes")
  15.  
  16. {
  17.   mysql_connect($host,$user,$password);
  18.   mysql_select_db($_POST['database']);
  19.   $query = stripSlashes($_POST['query']);
  20.   $result = mysql_query($query);
  21.   echo "Database Selected:<b>{$_POST['database']}</b><br>
  22.     Query: <b>$query</b><h3>Results</h3><hr>";
  23. if($result == 0)
  24.   echo "<b>Error ".mysql_errno().": ".mysql_error().
  25.     "</b>";
  26. elseif (@mysql_num_rows($result) == 0)
  27.   echo("<b>Query Completed. No results returned.
  28.     </b><br>");
  29. else
  30. {
  31. echo "<table border='1'>
  32.     <thead>
  33.     <tr>";
  34.     for($i = 1;$i < mysql_num_fields($result);$i++)
  35.     {
  36.     echo "<th>".mysql_field_name($result,$i).
  37.     "</th>";
  38. }
  39.     echo " </tr>
  40.         </thead>
  41.         <tbody>";
  42.         for ($i = 1; $i < mysql_num_rows($result); $i++)
  43. {
  44.         echo "<tr>";
  45.         $row = mysql_fetch_rows($result);
  46.         for($j = 0;$j<mysql_num_fields($result);$j++)
  47. {
  48.         echo("<td>" . $row[$j] . "</td>");
  49. }
  50.         echo "</tr>";
  51.     }
  52. echo "</tbody>
  53.     </table>";
  54. }   // end else
  55. echo "
  56.  <hr><br>
  57.  <form action=\"{$_SERVER['PHP_SELF']}\"method=\"POST\">
  58.     <input type='hidden' name='query' value='$query'>
  59.     <input type='hidden' name='database'
  60.         value={$_POST['database']}>
  61.     <input type='submit' name=\"queryButton\"
  62.         value=\"New Query\">
  63.     <input type='submit' name=\"queryButton\"
  64.         value=\"Edit Query\">
  65. </form>";
  66. unset($form);
  67. exit();
  68. }  // endif form=yes
  69.  
  70. /* Section that requests user input of query */
  71. @$query=stripSlashes($_POST['query']);
  72. if (@$_POST['queryButton'] != "Edit Query")
  73. {
  74.   $query = " ";
  75. }
  76. ?>
  77.  
  78. <form action="<?php echo $_SERVER['PHP_SELF'] ?>?form=yes"
  79.     method="POST">
  80. <table>
  81. <tr>
  82. <td align=right><b>Type in database name</b></td>
  83. <td><input type="text" name="database"
  84.        value=<?php echo @$_POST['database'] ?> ></td>
  85. </tr>
  86. <tr>
  87. <td align="right" valign="top">
  88.     <b>Type in SQL query</b></td>
  89. <td><textarea name="query" cols="60"
  90.         rows="10"><?php echo $query ?></textarea>
  91. </td>
  92.  </tr>
  93.   <tr>
  94.    <td colspan="2" align="center"><input type="submit"
  95.     value="Submit Query"></td>
  96.    </tr>
  97.  </table>
  98. </form>
  99. </body></html>
  100. ?>

pues tenias 3 errores,

colocaste fijate en tus for, nunca asignas un valor inicial a $i;

y hay una linea donde POST tiene simbolos raros.

compara los cósdigos y observa tus errores