Why wpdb not working?

[ad_1]

<?php
if($_SERVER[‘REQUEST_METHOD’] == “GET”){
global $wpdb, $table_prefix;
    $tableName = $table_prefix . “contact_ways”;
    $id = $_GET[‘id’];
echo $id.”<br>”.$tableName.”<br>”;
    $row = $wpdb->get_results(“SELECT * FROM $tableName WHERE id=$id”);
}

it retunes

1
contact_ways

Fatal error: Uncaught Error: Call to a member function get_results() on null in C:\xampp\htdocs\wordpress\wp-content\plugins\connectUs\state_action.php:7 Stack trace: #0 {main} thrown in C:\xampp\htdocs\wordpress\wp-content\plugins\connectUs\state_action.php on line 7

after that i try this

<?php
define(‘WP_USE_THEMES’, false);
global $wpdb;
require( ‘wp-load.php’ );
if($_SERVER[‘REQUEST_METHOD’] == “GET”){
global $wpdb, $table_prefix;
$tableName = $table_prefix . “contact_ways”;
$id = $_GET[‘id’];
echo $id.”<br>”.$tableName.”<br>”;
$row = $wpdb->get_results(“SELECT * FROM $tableName WHERE id=$id”);
}

it retunes

Warning: require(wp-load.php): failed to open stream: No such file or directory in C:\xampp\htdocs\wordpress\wp-content\plugins\connectUs\state_action.php on line 4

Warning: require(wp-load.php): failed to open stream: No such file or directory in C:\xampp\htdocs\wordpress\wp-content\plugins\connectUs\state_action.php on line 4

Fatal error: require(): Failed opening required ‘wp-load.php’ (include_path=’C:\xampp\php\PEAR’) in C:\xampp\htdocs\wordpress\wp-content\plugins\connectUs\state_action.php on line 4

How can i fix this?!

[ad_2]
1 Comment
  1. What file is your code in and where is the file located? The way you’re loading wp-load.php means your file must be in the web root. If your code is located somewhere else then you need to change the path parameter in your require() call.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer