WordPress.org

Ready to get started?Download WordPress

Forums

Using $wpdb (2 posts)

  1. roiabrazaldo
    Member
    Posted 9 months ago #

    I'm using a $wpdb and it is not recognize, am I missing something out?

    my page calls an ajax to a certain php file which is I name get_place.php

    this is my ajax, its working

    $.ajax({
    cache: false,
    url: baseurl + '/php/get_place.php',
    type: 'post',
    data: 'latlang=' + place.geometry.location + '&q=' + 1
    });

    and this is the get_place.php file

    if (isset($_POST['latlang'])){
    	$latlang = $_POST['latlang'];
    }
    
    if (isset($_POST['q'])){
    	$q = $_POST['q'];
    }
    
    if($q==1){
            $wpdb -> query("INSERT IGNORE INTO <code>tbl_branch</code> SET <code>br_latlang</code> = '".$latlang."';");
    }
    else{
    	$result = $wpdb -> get_results( "SELECT REPLACE(<code>br_title</code>, ' ', '&nbsp;'), <code>br_content</code> FROM <code>tbl_branch</code> WHERE <code>br_latlang</code> = '".$latlang."';" );
    	$row = mysql_fetch_array($result);
    	echo "<div id=\"infobox_content\">
    		<div id=\"siteNotice\"></div>
    		<h3 id=\"firstHeading\" class=\"firstHeading\">".$row[0]."</h3>
    		<div id=\"bodyContent\">
    		".$row[1]."
    		</div>
    		</div>";
    }

    it gives me an error that $wpdb is not recognized

    Notice: Undefined variable: wpdb in C:\xampp\htdocs\public\lianas\wp-content\themes\lianas\php\get_place.php on line 15

    Fatal error: Call to a member function get_results() on a non-object in C:\xampp\htdocs\public\lianas\wp-content\themes\lianas\php\get_place.php on line 15

    any ideas?

  2. roiabrazaldo
    Member
    Posted 9 months ago #

    solved guys, by adding this

    global $wpdb;

    if(!isset($wpdb))
    {
    require_once('../../../../wp-config.php');
    require_once('../../../../wp-includes/wp-db.php');
    }

    my problem now is how to loop on get results

Reply

You must log in to post.

About this Topic

Tags

No tags yet.