简体   繁体   中英

How to display Posts in wordpress page using mysql query?

I want to display Posts details in WordPress page. I have added MySQL query in WordPress page(All Pages -> Add New Page --> Added the Code ). I am using INSERT PHP WordPress plugin for that.. Still it displays the error..

How to do this..? The Code which I have used is given below...

[insert_php]
$con=mysqli_connect("localhost","DB_NAME","DB_PW((","DB_NAME");
$sqli="select * from cre_term_relationships where term_taxonomy_id=1";
$res=mysqli_query($sqli) or die(mysqli_error());
while($row=mysqli_fetch_array($res,MYSQLI_ASSOC))
{
echo $row["object_id"];
}
[/insert_php]

There are a few problems with your code.

Firstly, you're not passing your DB connection parameter to the query.

$res=mysqli_query($sqli)
                  ^ missing the 1st parameter.

Modify it to read as:

$res=mysqli_query($con, $sqli)

and mysqli_error() requires DB connection be passed as a parameter

  • mysqli_error($con)

References:

However, WordPress has its own MySQL core functions. I don't know why you're using mysql_


This line:

$con=mysqli_connect("localhost","DB_NAME","DB_PW((","DB_NAME");

remove the excess brackets and the order:

$con=mysqli_connect("localhost","DB_USERNAME","DB_PW","DB_NAME");

assuming DB_USERNAME is a pre-defined constant that isn't showing in your question.

Syntax is:

  1. host
  2. username
  3. password
  4. database

However, constants do not get wrapped in quotes:

$con=mysqli_connect("localhost", DB_USERNAME, DB_PW, DB_NAME);

if you have in fact defined your constants.

You will need to elaborate on this, and to update your question, should my answer not provide the solution.

Consult the following regarding the use of constants:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM