我将Woody片段插件用于WordPress中的PHP代码。我正在尝试显示数据库中的数据,但是它只显示警告。它可以在我的本地xampp服务器上正常运行,而无需使用wordpress。
$conn = mysqli_connect("localhost", "root", "", "myshop");
if ($conn) {
echo "connection success<br>";
} else {
echo "error!!!!!";
}
function getPosts()
{
global $conn;
$get_products = "SELECT * FROM products";
$run_products = mysqli_query($conn, $get_products);
$counter = 1;
while ($row_products = mysqli_fetch_array($run_products)) {
$pro_title = $row_products['product_title'];
echo "<b>$counter" . " $pro_title</b><br>";
$counter++;
}
}
getPosts();
我已经尝试过mysqli_num_rows
,但是它没有在WordPress中执行,它显示“连接成功”,之后我收到警告:
警告:mysqli_query()期望参数1为mysqli,在Z:\ xampp \ htdocs \ wordpress \ wp-content \ plugins \ insert-php \ includes \ shortcodes \ shortcode-php.php(45)中给出空值: eval()在第15行的代码
警告:mysqli_fetch_array()期望参数1为mysqli_result,在Z:\ xampp \ htdocs \ wordpress \ wp-content \ plugins \ insert-php \ includes \ shortcodes \ shortcode-php.php(45)中给出空值: eval()在第17行的代码
答案 0 :(得分:0)
$ conn似乎什么也没返回,请在您的方法或构造函数中的下一行以在每种方法中重用
$conn=mysqli_connect("localhost","root","","myshop");