Community management insight at ManagingCommunities.com

Bookmark and Share

How to Add/Edit Your Own SQL Queries Without phpmyadmin

How to Add/Edit Your Own SQL Queries Without phpmyadmin

Postby TailstheEnchidna » November 3rd 2004, 9:18 pm

This tutorial will help users make or edit their own SQL queries without using phpmyadmin. All you have to do is simply create a new .php file, upload anywhere on your server, and execute it from there. This is what you have to do:

Step 1: Make a new .php file, named whatever you want (I name it mysql_send.php), with the code below:

Code: Select all
<html>
<head>
<title>SQL Query</title>
</head>
<body>
<?php
 $user="";
 $host="";
 $password="";


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();
 }

@$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>


All you would have to change there are the variables user(username of database), host of database, and database password.

Step 2: Upload the file onto yor server. Once there, run it off of your browser.

You should see a page with 2 forms to fill out: Database name and Query

Step 3: Fill out the database name and SQL query and hit Submit Query. If everything went well, the following message should appear "Query completed". It might also say "Query completed. No results returned." if it doesn't need to show you anything.

Well that's the end of my tutorial. I hope you find this helpful.

~TailstheEnchidna
TailstheEnchidna
Not So New User
 
Posts: 72
Joined: December 26th 2003, 8:41 am

Return to phpBB 2: Tools and Related Topics

Who is online

Users browsing this forum: No registered users and 1 guest