简体   繁体   English

如何使用mysql查询在wordpress页面中显示帖子?

[英]How to display Posts in wordpress page using mysql query?

I want to display Posts details in WordPress page. 我想在WordPress页面中显示帖子详细信息。 I have added MySQL query in WordPress page(All Pages -> Add New Page --> Added the Code ). 我已经在WordPress页面中添加了MySQL查询(所有Pages -> Add New Page --> Added the Code )。 I am using INSERT PHP WordPress plugin for that.. Still it displays the error.. 我为此使用INSERT PHP WordPress插件。仍然显示错误。

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()要求将数据库连接作为参数传递

  • mysqli_error($con)

References: 参考文献:

However, WordPress has its own MySQL core functions. 但是,WordPress具有自己的MySQL核心功能。 I don't know why you're using mysql_ 我不知道你为什么要使用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. 假设DB_USERNAME是一个未在您的问题中显示的预定义常量。

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: 有关常量的使用,请查阅以下内容:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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