Topic: Can't return result of MySQL Querry

Hey everyone,

I am brand new to this and only have a little experience with php. I'm trying to learn xajax by reading and picking apart the examples. I think I have a very basic understanding  but I have a bit of an issue that I just can't figure out. Hopefully someone can help, I have been banging my head on my desk to 3 days now.

On my page I have 1 text field and a button. What I am trying to do is enter the device's ID from a mySQL database into the text field, then when I click the button return the model of the device into the div with the ID "added".

I have the following code in 3 pages, I tried to mirror the "multiply" example as exact as possible.

The issue I am having is that I am not able to return the result of the query. on the getDevice.php page I have a query to test my sql and make sure that't not the issue. I have no problem with that, I get the model of the device returned. If I try to return the result of the query in my ajax response I get nothing. If I set the variable of the query ($sql), then my string returns fine with the correct data passed from the text box.

Below is the code that I have.

/* getDevice.php*/


<?php
require("getDevice.common.php");
echo '<?xml version="1.0" encoding="UTF-8"?>'
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <?php $xajax->printJavascript(); ?>
    </head>
    <body>
        
        <input type="text", id = "y" name="y"><br>
        <input type="button", value ="Press Me", onclick="xajax_returnData(document.getElementById('y').value);return false;" />

/*This is the area where I want the ajax response to go*/
<div id="added"></div>

        <div>    
          <?php
            
            /*I run the query below to make sure that I see output on the screen to make sure there are no problems with my query*/
            /*This works fine and I can see the model of the phone on the page*/

            $sql = "SELECT model FROM tbl_phone WHERE deviceID= '5' ";
            $cxn = mysqli_connect("localhost","user","pw","db")
                    or die("Query died: connect");
            $result = mysqli_query($cxn,$sql)
                    or die('Could not connect: ' . mysql_error());

            $data = mysqli_fetch_assoc($result);
            $data = $data['model'];
            echo "$data";
           ?>
        </div>
    </body>
</html>

<?php
/* getDevice.common.php*/

    require_once ("xajax/xajax_core/xajax.inc.php");
    $xajax = new xajax("getDevice.server.php");
    $xajax->configure('javascript URI','xajax/');
    $xajax->register(XAJAX_FUNCTION,"returnData");
    $xajax->processRequest();
?>    

/* getDevice.server.php*/

<?php
function returnData($y) 
{
    $objResponse = new xajaxResponse();
    $sql = "SELECT model FROM tbl_phone WHERE deviceID= '$y' ";
    $cxn = mysqli_connect("localhost","user","pw","db")
           or die("Query died: connect");
    $result = mysqli_query($cxn,$sql)
           or die('Could not connect: ' . mysql_error());

    $data = mysqli_fetch_assoc($result);
    $myData = $data['model'];


/*I get nothing returned if returning $myData*/
/*If I change the variable to $sql it will return the string of my query successfully into the "added" div*/

    $objResponse->assign("added", "innerHTML", $myData);
    return $objResponse;
}

require("getDevice.common.php");
$xajax->processRequest();
?>

Re: Can't return result of MySQL Querry

Here is a version that works. I juggled the code into a single page and used mysql instead of mysqli but I think you can see what's happening. Also fit the sql to my database...

<?php
    require_once ("xajax/xajax_core/xajax.inc.php");
    $xajax = new xajax();
    $xajax->configure('javascript URI','xajax/');
    $xajax->register(XAJAX_FUNCTION,"returnData");
    $xajax->configure('debug', true);
    $xajax->processRequest();

    function returnData($y) 
    {
        $resp = new xajaxResponse();
        $sql = "SELECT model FROM tbl_phone WHERE device_id = '$y' ";
        $cxn = mysql_connect("localhost","user","password");
        if (!$cxn)
        {
          $resp->alert('Query died: connect');
          return $resp;
        }
        mysql_select_db('database');
        //$resp->alert($cxn);
        $result = mysql_query($sql,$cxn);
        if(!$result)
        {
          $resp->alert('Query error: ' . mysql_error());
          return $resp;
        }
        $data = mysql_fetch_assoc($result);
        $resp->assign("added", "innerHTML", $data['model']);
        return $resp;
    }
?>
<!DOCTYPE html>
<html>
    <head>
        <?php $xajax->printJavascript(); ?>
    </head>
    <body>
        
        <input type="text", id = "y" name="y"/><br>
        <input type="button", value ="Press Me", onclick="xajax_returnData(document.getElementById('y').value)" /><br/>
        <label>Added</label><div id="added"></div>
</body>
</html>

Ed

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.