Jump to content

Run SQL Queries


microbert

Recommended Posts

Hi,

 

you all know that from phpmyadmin you can run an SQL Query for example "select * from table where code like '0011'"

 

sqlquery.png

 

now my question is can I add a query box like this directly into my website, so I can run a query without login into phpmyadmin?

 

your help is very appreciated since I cannot make this happen.

thanks

Link to comment
Share on other sites

This is my code for the form:

<form action="sqlquery.php" method="GET">
   <textarea name="SQLquery" rows="20" cols="40"></textarea>
      <br />
      <input type="submit" name="SQLexecute" value="Execute Query" style="height: 30px; width: 350px; font-size: 16px" />
</form>

and this is the submit form:

<!DOCTYPE HTML>
<html>
<body>
    <?php
        mysql_connect("server", "username", "password") or die("Error connecting to database: ".mysql_error());
    ?>

    <table width="100%" cellspacing="0" cellpading="0" valign="top" border="0">
        <tr>
            <td>
                <?php
                    $query = $_GET['SQLquery'];

                    if($query == "")
                    {
                        echo("
                            <table align='center' border='1' width='1000px' bgcolor='#FFFFFF'><tr>
                            	<td align='center' width='150px'>
                            		<font size='+2'>Opps! You forgot to enter query.</font>
                            	</td>
                            </tr></table>
                        ");
                    }
                    else
                    {
						echo("
                            <table bgcolor='#FFF' align='center' border='1' width='1000px' cellspacing='0' cellpading='0'>
                                <tr>
                                    <td align='center' width='150px'><b>Picture</b></td>
                                    <td align='center' width='530px'><b>Description</b></td>
                                    <td align='center' width='170px'><b>Stock</b></td>
                                </tr>
                        ");

                                $sql = mysql_query("$query");

								while($data = mysql_fetch_array($sql))
		                        {
		                            $scode = $data['code'];
		    						$sname = $data['name'];
		    						$sqty = $data['qty'];
		    						$sDateModified = $data['DateModified'];
		    						$sDateCreated = $data['DateCreated'];

		                                include("ListView.php");
		                        }
								$i--;
                            	$x++;
                            }
                ?>
            </td>
        </tr>
    </table>
</body>
</html>
Link to comment
Share on other sites

the reason you have to log into phpmyadmin before you can enter and run a query is to prevent someone from visiting your web site and either grabbing all your database contents and/or dropping all your database tables.

 

if you are going to do this, your first priority would be to have a login system that prevent access to the form and the form processing code.

 

next, the error is because the query failed with an error of some kind. you would need to have error checking logic in your code to get php/mysql to tell you why the query failed.

 

lastly, to do this in general, you don't know what columns are being selected by the query, so there's no way of writing any specific code like - $scode = $data['code'];. you would need to extract the column information from the result set and use the actual column names that were selected in the query for any display purposes.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.