Foros del Web » Programando para Internet » PHP »

Consulta programa PHP para realizar consultas SQL

Estas en el tema de Consulta programa PHP para realizar consultas SQL en el foro de PHP en Foros del Web. Hola a todos!! Mi nombre es Lisandro y me animo a hacer una pregunta despues de haber leido bastante el foro y haber intentado a ...
  #1 (permalink)  
Antiguo 31/05/2011, 22:15
 
Fecha de Ingreso: mayo-2011
Mensajes: 78
Antigüedad: 12 años, 11 meses
Puntos: 0
Consulta programa PHP para realizar consultas SQL

Hola a todos!! Mi nombre es Lisandro y me animo a hacer una pregunta despues de haber leido bastante el foro y haber intentado a mas no poder solucionar el problema por las mias.

El asunto es el siguiente, estoy aprendiendo php y SQL y tengo un problema para realizar consultas. El manual de donde estoy aprendiendo, sugiere hacer un programa en php para poder realizar consultas a la base de dato a traves de Mysql, el problema esta en el codigo del archivo PHP que tiene un error...

A continuacion les pego el codigo para ver si algun alma caritativa me puede dar una ayuda y decirme donde esta el error de sintaxis...
El error que me da cuando quiero ejecutarlo es:

Parse error: syntax error, unexpected ';' in /home/a7081447/public_html/mysql_send.php on line 34


supongo que hay un problema con algun ; pero no puedo darme cuenta, les copio el codigo y si alguien me puede ayudar lo voy a agradecer muchisimo!!!




Código:
<! -- Program: mysql_send.php
      Desc:   PHP Program thar sends an SQL query to the
		MySQL server and displays the results
-->
<html>
<head><title>SQL Quey Sender</title></head>
<body>
<?php

$host="aca va el host";
$user=" aca va el user ";
$password="aca va la contraseña";
/* Section that executes query */
if (@$_GET['form'] == "yes")

{
  mysql_connect($host,$user,$password);
  mysql_select_db($_POST['database']);
  $query = stripSlashes($_POST['query']);
  $result = mysql_query($query);
  echo "Database Selected:<b>{$_POST['database']}</b><br>
	Query: <b>$query</b><h3>Results</h3><hr>";
if($result == 0)
  echo "<b>Error ".mysql_errno().": ".mysql_error().
	"</b>";
elseif (@mysql_num_rows($result) == 0)
  echo("<b>Query Completed. No results returned.
	</b><br>");
else
{
echo "<table border='1'>
	<thead>
	<tr>";
	for($i = ;$i < mysql_num_fields($result);$i++)
	{
	echo "<th>".mysql_field_name($result,$i).
	"</th>";
}
	echo " </tr>
		</thead>
		<tbody>";
		for ($i = ; $i < mysql_num_rows($result); $i++)
{
		echo "<tr>";
		$row = mysql_fetch_rows($result);
		for($j = 0;$j<mysql_num_fields($result);$j++)
{
		echo("<td>" . $row[$j] . "</td>");
}
		echo "</tr>";
	}
echo "</tbody>
	</table>";
}	// end else
echo "
  <hr><br>
  <form action=\"{$_SERVER['PHP_SELF']}\"method=\"POST\">
	<input type='hidden' name='query' value='$query'>
	<input type='hidden' name='database'
		value={$_POST['database']}>
	<input type='submit' name=\"queryButton\"
		value=\"New Query\">
	<input type='submit' name=\"queryButton\"
		value=\"Edit Query\">
</form>";
unset($form);
exit();
}  // endif form=yes

/* Section that requests user input of query */
@$query=stripSlashes($_POST`'query']);
if (@$_POST['queryButton'] != "Edit Query")
{
  $query = " ";
}
?>

<form action="<?php echo $_SERVER['PHP_SELF'] ?>?form=yes"
	method="POST">
<table>
<tr>
<td align=right><b>Type in database name</b></td>
<td><input type="text" name="database"
	   value=<?php echo @$_POST['database'] ?> ></td>
</tr>
<tr>
<td align="right" valign="top">
	<b>Type in SQL query</b></td>
<td><textarea name="query" cols="60"
		rows="10"><?php echo $query ?></textarea>
</td>
 </tr>
  <tr>
   <td colspan="2" align="center"><input type="submit"
	value="Submit Query"></td>
   </tr>
 </table>
</form>
</body></html>
Muchas gracias!!!!
  #2 (permalink)  
Antiguo 31/05/2011, 22:44
Avatar de 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
  #3 (permalink)  
Antiguo 01/06/2011, 18:42
 
Fecha de Ingreso: mayo-2011
Mensajes: 78
Antigüedad: 12 años, 11 meses
Puntos: 0
Respuesta: Consulta programa PHP para realizar consultas SQL

Muchisimas gracias Memoadian!! ahi estuve chequeando lo que me decis!.


Gracias nuevamente!!

Saludos

Etiquetas: mysql
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 18:27.