WordPress.org

Ready to get started?Download WordPress

Forums

How can I make a query to a wordpress database? (1 post)

  1. jolona
    Member
    Posted 1 year ago #

    I made a website with wordpress and I need to make a query to a wordpress database to extract data manually. If I make this request to a not wordpress mysql database with simple php all works fine, but if I do it to a wordpress mysql database (with the correct data connection and fields of the table) it doesnt work. I need to do this in order to use the data out of wordpress... I only need to print at screen this data. I neednt to change any information from the wordpress database. How can I do it?

    For example:

    <b>Works:</b>

    This query (from a not wordpress database) works fine as you can see it working in: http://www.radiomania.es/android1/index-work.php

    This is the content of the file index-work.php (I dont provide you the real password, if you need it, do not hesitate to write me):

            <?
    //conectamos con y seleccionamos la base de datos
    
    $connect=mysql_connect("localhost","avidal","password");
    
    //Seleccionamos la base
    
    mysql_select_db("rmania",$connect);
    //hacemos las consultas
    $result=mysql_query("select * from noticias order by fecha DESC limit 0,20", $connect);
    $totalregistros=mysql_num_rows($result);
    //Recogemos las consultas en un array y las mostramos
    while($row=mysql_fetch_array($result))
    { ?>
          </div>
          <table width="100%" border="0" cellspacing="1" cellpadding="1">
            <tr>
              <td class="titulo">
                <? echo $row[titulo]; ?>
              </td>
            </tr>
            <tr>
              <td class="fecha">
                <? echo $row[fecha]; ?>
              </td>
            </tr>
            <tr>
              <td height="9">
                <hr size="1">
              </td>
            </tr>
          </table>
          <?
    							}
    mysql_free_result($result)
    ?>
    

    <b>Doesnt work</b>

    This query (from a wordpress database) doesnt work as you can see it working in: http://www.radiomania.es/android1/index-no-work.php

    This is the content of the file index-no-work.php (I dont provide you the real password, if you need it, do not hesitate to write me):

            <?
    //conectamos con y seleccionamos la base de datos
    
    $connect=mysql_connect("localhost","rmwordpress","password");
    
    //Seleccionamos la base
    
    mysql_select_db("radiomania",$connect);
    //hacemos las consultas
    $result=mysql_query("select * from wp_posts order by fecha DESC limit 0,20", $connect);
    $totalregistros=mysql_num_rows($result);
    //Recogemos las consultas en un array y las mostramos
    while($row=mysql_fetch_array($result))
    { ?>
          </div>
          <table width="100%" border="0" cellspacing="1" cellpadding="1">
            <tr>
              <td class="titulo">
                <? echo $row[post_title]; ?><?php the_title(); ?>
              </td>
            </tr>
            <tr>
              <td class="fecha">
                <? echo $row[post_date]; ?>
              </td>
            </tr>
            <tr>
              <td height="9">
                <hr size="1">
              </td>
            </tr>
          </table>
          <?
    							}
    mysql_free_result($result)
    ?>
    

    Why it doesnt work?
    I need to print results from a wordpress mysql database from the title of the blog, data and content of the blog table (wp_posts).
    Is there any other way to do it?

    Thanks in advance for your answer. I am turning crazy with it.

Topic Closed

This topic has been closed to new replies.

About this Topic