gpt4 book ai didi

php - 代码wordpress插件保存到Mysql数据库

转载 作者:行者123 更新时间:2023-11-29 18:35:23 27 4
gpt4 key购买 nike

创建 WordPress 插件的最佳方法是什么。我已经开始学习制作插件,我想制作一个插件来计算 2 个数字并保存到 mysql 数据库。

因为我不知道 PHP 中的 OOP,所以我决定编写我所知道的代码。

我的代码看起来像这样,只是我的导师希望我做一些很棒的事情......比我之前编写的代码更好。

编辑

现在我正在尝试添加此内容,然后发送到数据库。

代码现在看起来像这样

  <?php

/*
Plugin Name: Addition DB Plugin
Description: Plugin to Add to Database and call back from database
Author: James Ibrahimov
*/

global $wpdb;

function add2numbersandsavetoDB()
{
$num1 = '5';
$num2 = '11';

$ans = $num1 + $num2;

$wpdb -> insert('answer',array(
'id' => 'id',
'ans'=> $ans
),
array('%d','%s') );
}

add_action('main_menu', 'add2numbersandsavetoDB');

function displayDatafromDB()
{
$results = $wpdb-> get_results("select * from answer");
foreach($results as $result){

echo $result-> id;
echo $result-> ans;
}

}

add_action('main_menu','displayDatafromDB');
?>

最佳答案

您正在使用 php 将 dta 插入数据库。使用wordpress函数将数据插入数据。

change
$host = '127.0.0.1';
$user = 'root';
$pass = '';
$db = 'wordpress_tut';
to
global $wpdb;

use this to insert your data
$wpdb->insert(
'table_name',
array(
'column1' => 'value1',
'column2' => 123
),
array(
'%s',
'%d'
)
);

有关如何插入或获取数据的更多信息click here

关于php - 代码wordpress插件保存到Mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45351184/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com