microbert Posted March 7, 2014 Share Posted March 7, 2014 Hi, you all know that from phpmyadmin you can run an SQL Query for example "select * from table where code like '0011'" 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 Quote Link to comment https://forums.phpfreaks.com/topic/286791-run-sql-queries/ Share on other sites More sharing options...
jairathnem Posted March 7, 2014 Share Posted March 7, 2014 use textarea HTML tag. get entered query via GET or POST and process it. but if you want highlighting you may have to write your own js for that. Quote Link to comment https://forums.phpfreaks.com/topic/286791-run-sql-queries/#findComment-1471743 Share on other sites More sharing options...
microbert Posted March 7, 2014 Author Share Posted March 7, 2014 I have tried that but it is giving me an error: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean Quote Link to comment https://forums.phpfreaks.com/topic/286791-run-sql-queries/#findComment-1471748 Share on other sites More sharing options...
jairathnem Posted March 7, 2014 Share Posted March 7, 2014 post your code. Quote Link to comment https://forums.phpfreaks.com/topic/286791-run-sql-queries/#findComment-1471750 Share on other sites More sharing options...
microbert Posted March 7, 2014 Author Share Posted March 7, 2014 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> Quote Link to comment https://forums.phpfreaks.com/topic/286791-run-sql-queries/#findComment-1471757 Share on other sites More sharing options...
jairathnem Posted March 7, 2014 Share Posted March 7, 2014 use mysql_fetch_assoc() to get the data.But instead, look into PDO instead of mysql. Quote Link to comment https://forums.phpfreaks.com/topic/286791-run-sql-queries/#findComment-1471760 Share on other sites More sharing options...
microbert Posted March 7, 2014 Author Share Posted March 7, 2014 can you please tell me what PDO is and how to use it? sorry but I am a beginner Quote Link to comment https://forums.phpfreaks.com/topic/286791-run-sql-queries/#findComment-1471765 Share on other sites More sharing options...
mac_gyver Posted March 7, 2014 Share Posted March 7, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/286791-run-sql-queries/#findComment-1471768 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.