The MU forums have moved to WordPress.org

Connect to db (6 posts)

  1. thtang
    Member
    Posted 17 years ago #

    I would like to know how to connect to the db and get the result of select query in wordpress. I mean i dont want to write the php db connection from scratch but use the wordpress way to the select query.

    Thank you.

  2. drmike
    Member
    Posted 17 years ago #

    What query are you trying to do?

  3. thtang
    Member
    Posted 17 years ago #

    i am actually doing a search function, search by post, user or tag.

    I would like to write the query on selecting the crieteria..but i dont know how to start since i want to make use of the db setting in the existing wp.

    Please advice.

    Thank you.

  4. lunabyte
    Member
    Posted 17 years ago #

    Um, can't you look at one of the files and see how WP forms its queries?

    If it's a plugin or whatever, the connection is already there.

  5. SteveAtty
    Member
    Posted 17 years ago #

    put

    require( dirname(__FILE__) . '/wp-config.php' );

    at the top of your php file and then you can just use the mysql_query functions:

    
    $sql = "select blog_id, path from wp_blogs where public=1 and spam=0";
    $blogs = mysql_query($sql);
    while ($blog = mysql_fetch_array($blogs)) {
    ..
    ..
    ..
    }
    
  6. drmike
    Member
    Posted 17 years ago #

    Can't do the require as that file pulls wp-settings.php and off we go into wordpress. With errors. :)

    I just add in the mysql connection manually myself. That's how I do all those little scripts that I do that sit outside of the mu install.

About this Topic