Hi
Im just wondering if anyone has any idea about how to execute a query to a mysql database. The query is input as an SQL statement from a HTML page. I've tried a few things but nothing is working.
If anyone has any ideas, please help
Thanks.
| SitePoint Sponsor |
Hi
Im just wondering if anyone has any idea about how to execute a query to a mysql database. The query is input as an SQL statement from a HTML page. I've tried a few things but nothing is working.
If anyone has any ideas, please help
Thanks.





Hi,
If I understand you correctly, something along these lines will workRemeber to change all the names/variables to reflect your settings.PHP Code:<?php
$link = mysql_connect("localhost", "mysql_user", "mysql_password")
or die('Could not connect: ' . mysql_error());
mysql_select_db("your_database")
or die('Could not select databse: ' . mysql_error());
$query = $_POST['query']; // Get sql from html form.
$result = mysql_query($query)
or die('Error while executing query: ' . mysql_error());
while($row = mysql_fetch_array($result)) {
echo '<br>field1: ' . $row['field1'];
echo '<br>field2: ' . $row['field2'];
echo '<br>field3: ' . $row['field3'];
echo '<br>';
}
?>
HTH
-Helge
Last edited by Helge; Mar 31, 2003 at 09:24. Reason: Typo
Thanks that seems to be working. I'm a bit confused about the part that returns the results to the screen. If the user is entering a different SQL query each tome what do i need to do?





That's kind of a broad question so I am not sure how to answer it exactly. If you make it more specific or give some examples, it may be easier to help.Originally Posted by JenniferKearns
However, I suggest you look at this tutorial, which I found quite useful in the past: http://www.mysql.com/articles/ddws/20.html
Actually, instead go to the sitepoint books page and download the four free chapters... same tutorial, a little more up-to-date. Skim over the parts you already know.![]()
Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?





Also do a search at www.wdvl.com on mysql - should give a suitable list of mySQL tutorials for you 8)
be very, very careful with that if you're letting anyone enter queries!![]()
- Matt
Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
"Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR
Bookmarks