The book I am reading (
http://search.barnesandnoble.com/booksearch/isbnInquiry.asp?userid=2T67AHHR6W&isbn=0764516507&itm=1
 )
PHP & MySQL for Dummies  has a CD Rom with several PHP files. One is called
mysql_send.php. Book says " The program mysql_send.php has one simple
function: to execute queries and display the results. Copy the program from
the CD into the directory where you are developing your web application,
change the information  in lines 11-13 and point your browser at the
program."



I was having problems with the mysql_send.php.

Someone in a MySQL32 email list is working with me. He has suggested several
changes to line 57. Now I no longer get an error pointing to line 57. When I
type in a command ( like SHOW DATABASES ) and hit submit Query  all I get
back is a blank box.

While I am working on getting this fixed I made a copy of the file giving me
problems  ( mysql_send.php ) and call it mysql_send_test.php. It is in the
same directory as the original.

I am running Win XP. Apache 2.0.47, PHP 3.3.2 and MySQL 4.0.13

The current code in mysql_send_test.php is as follows:


<!-- Program Name:  mysql_send.php
     Description: PHP program that sends an SQL query to the
                  MySQL server and displays the results.
-->
<html>
<head>
<title>SQL Query Sender</title>
</head>
<body>
<?php
 $user="root";
 $host="localhost";
 $password="";

 /* Section that executes query */
 if (@$form == "yes")
 {
   mysql_connect($host,$user,$password);
   mysql_select_db($database);
   $query = stripSlashes($query) ;
   $result = mysql_query($query);
   echo "Database Selected: <b>$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 = 0; $i < mysql_num_fields($result); $i++)
             {
                 echo("<th>" . mysql_field_name($result,$i) . "</th>");
             }
     echo " </tr>
           </thead>
           <tbody>";
             for ($i = 0; $i < mysql_num_rows($result); $i++)
             {
                echo "<tr>";
                $row = mysql_fetch_row($result);
                for ($j = 0; $j < mysql_num_fields($result); $j++)
                {
                  echo("<td>" . $row[$j] . "</td>");
                }
                echo "</tr>";
             }
             echo "</tbody>
                  </table>";
   }
   echo "<hr><br>
         <form action=" . $_SERVER["PHP_SELF"] .  " method=post>

          <input type=hidden name=query value=\"$query\">
          <input type=hidden name=database value=$database>
          <input type=submit name=\"queryButton\" value=\"New Query\">
          <input type=submit name=\"queryButton\" value=\"Edit Query\">
         </form>";
   unset($form);
   exit();
 }

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

<form action=<?php echo $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 $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>


Also if you would like to see the code of the file mysql_send.php ( DIRECT
off the CD that came with this book  ) is as follow:

<!-- Program Name:  mysql_send.php
     Description: PHP program that sends an SQL query to the
                  MySQL server and displays the results.
-->
<html>
<head>
<title>SQL Query Sender</title>
</head>
<body>
<?php
 $user="root";
 $host="localhost";
 $password="";

 /* Section that executes query */
 if (@$form == "yes")
 {
   mysql_connect($host,$user,$password);
   mysql_select_db($database);
   $query = stripSlashes($query) ;
   $result = mysql_query($query);
   echo "Database Selected: <b>$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 = 0; $i < mysql_num_fields($result); $i++)
             {
                 echo("<th>" . mysql_field_name($result,$i) . "</th>");
             }
     echo " </tr>
           </thead>
           <tbody>";
             for ($i = 0; $i < mysql_num_rows($result); $i++)
             {
                echo "<tr>";
                $row = mysql_fetch_row($result);
                for ($j = 0; $j < mysql_num_fields($result); $j++)
                {
                  echo("<td>" . $row[$j] . "</td>");
                }
                echo "</tr>";
             }
             echo "</tbody>
                  </table>";
   }
   echo "<hr><br>
         <form action=$PHP_SELF method=post>
          <input type=hidden name=query value=\"$query\">
          <input type=hidden name=database value=$database>
          <input type=submit name=\"queryButton\" value=\"New Query\">
          <input type=submit name=\"queryButton\" value=\"Edit Query\">
         </form>";
   unset($form);
   exit();
 }

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

<form action=<?php echo $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 $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>


Thank you.




-- 
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to