get_meta, set_meta custom in MYSQL , PHP
1. CREATE TABLE USING MYSQL
DROP TABLE IF EXISTS `options`;
CREATE TABLE `options` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`metakey` varchar(255) NOT NULL,
`metavalue` varchar(255) NOT NULL,
`metaupdated` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
PRIMARY KEY (`id`)
);
2. ADD FUNCTION TO CLASS
public function getMeta( $metakey ) {
$sql="SELECT * from options where metakey='$metakey'";
$result=$this->conn->query($sql);
if($result->num_rows>0){
$row = $result->fetch_assoc();
}else{
$row=false;
}
return $row;
}
public function setMeta( $metakey, $metaValue) {
$existornot=$this->getMeta($metakey);
if($existornot){
$sql="UPDATE `options` SET
`metavalue` = '$metaValue',
`metaupdated` = date('Y-m-d H:i:s')
WHERE `metakey` = '$metakey'";
$this->conn->query($sql);
}else{
$sql="INSERT INTO `options` (`metakey`, `metavalue`, `metaupdated`)
VALUES ('$metakey', '$metaValue', date('Y-m-d H:i:s'))";;
$this->conn->query($sql);
}
return 1;
}
3. usage
$init->setMeta( 'weekly_price', "10");
$pricings = $init->getMeta('weekly_price');