SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Mar 2003
    Location
    Ireland
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Querying MySQL Database

    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.

  2. #2
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    If I understand you correctly, something along these lines will work
    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>';
    }
    ?>
    Remeber to change all the names/variables to reflect your settings.

    HTH

    -Helge
    Last edited by Helge; Mar 31, 2003 at 09:24. Reason: Typo

  3. #3
    SitePoint Member
    Join Date
    Mar 2003
    Location
    Ireland
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  4. #4
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by JenniferKearns
    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 time 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.

    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?

  5. #5
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also do a search at www.wdvl.com on mysql - should give a suitable list of mySQL tutorials for you 8)

  6. #6
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    be very, very careful with that if you're letting anyone enter queries!
    - Matt ** Ignore old signature for now... **
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •