Jump to content

SQL and AJAX

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
23 replies to this topic

#1
Jubal

Jubal

    Newbie

  • Members
  • PipPip
  • 22 posts
<?php

// retrieve the user name

$name = $_GET['name'];

// generate output depending on the user name received from client

$userNames = array('JAMES', 'BUBBLES', 'SUDOBAAL');

header('Content-Type: text/javascript');

if (in_array(strtoupper($name), $userNames))

  echo json_encode("1");

else if (trim($name) == '')

   echo json_encode("2");

else

   echo json_encode("3");

?>
This script takes a name keyed in by the user, checks it against its list, and if it matches passes a variable to the main javascript program. However, I need to edit it to check aganst the user data in my SQL table, or to put it simply, I need to replace the userNames array with the 'name' column of an SQL table. How might I do that?

Edited by Jaan, 18 January 2009 - 12:42 PM.


#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
You'll need to connect to your database and do a select statement. I will assume you are using PHP5 and MySQL:

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if ($mysqli->connect_error) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

/* Select queries return a resultset */
if ($result = $mysqli->query("SELECT name FROM <yourtable>")) {
    while($obj = $result->fetch_object()){
            $userName[]=$obj->name;
        }
    /* free result set */
    $result->close();
}

/* close connection */
$mysqli->close();

// Use the userName array in code
?>

I'm also going to move this to the PHP section since it is about PHP, rather than an AJAX issue.

#3
Jubal

Jubal

    Newbie

  • Members
  • PipPip
  • 22 posts
Well it's working, but seemingly there's still something wrong since when I type in a correct name it still comes up as if i'd keyed in a wrong or nonexistent name, so I guess there must be something wrong with my connection...

TO check;
- I put localhost where you put localhost
- 'World' I changed to 'test' as that is my db name
- I put the name of my table in where it said '<yourtable>'

<?php

// retrieve the user name

$name = $_GET['name'];

// generate output depending on the user name received from client

$mysqli = new mysqli("localhost", "", "", "test");


/* check connection */

if ($mysqli->connect_error) {

    printf("Connect failed: %s\n", mysqli_connect_error());

    exit();

}


/* Select queries return a resultset */

if ($result = $mysqli->query("SELECT name FROM raiders")) {

    while($obj = $result->fetch_object()){

            $userName[]=$obj->name;

        }

    /* free result set */

    $result->close();

}


/* close connection */

$mysqli->close();


//$userNames = array('JAMES', 'BUBBLES', 'SUDOBAAL');


header('Content-Type: text/javascript');

if (in_array(strtoupper($name), $userName))

  echo json_encode("1");

else if (trim($name) == '')

   echo json_encode("2");

else

   echo json_encode("3");

?>


In terms of checking the connection, this definitely does work (it just loads some data from the server);


<?php

$conn = mysql_connect("localhost", "", "");

$name = $_GET['name'];

mysql_select_db("test", $conn);

$sql = "SELECT * FROM raiders where name = '$name' ";

$result = mysql_query($sql, $conn) or die(mysql_error());

while ($newArray = mysql_fetch_array($result)){

$x = $newArray['sl'];

}

header('Content-Type: text/javascript');

echo json_encode($x);

?>


Many thanks,

Jubal

#4
Guest_Jordan_*

Guest_Jordan_*
  • Guests
I would do one of two things (maybe both) to see that the $userName[] array is actually being populated.

1) Add a echo statement in the while loop
/* Select queries return a resultset */
if ($result = $mysqli->query("SELECT name FROM <yourtable>")) {
    while($obj = $result->fetch_object()){
             echo $obj->name;
            $userName[]=$obj->name;
        }
    /* free result set */
    $result->close();
}
2) Print the entire $userName array out to display using print_r (or var_dump):
/* close connection */
$mysqli->close();

//$userNames = array('JAMES', 'BUBBLES', 'SUDOBAAL');

print_r($userNames);

/*header('Content-Type: text/javascript');
if (in_array(strtoupper($name), $userName))
  echo json_encode("1");
else if (trim($name) == '')
   echo json_encode("2");
else
   echo json_encode("3");*/
?>
I commented out your header/json function here so that you could see the print_r results.

These two tests will verify that you are connected to MySQL, that your SQL Statement Works and that the $userName array is being filled. After you do this let me know the results.

#5
Jubal

Jubal

    Newbie

  • Members
  • PipPip
  • 22 posts
I did the echo loop, and ran the PHP on its own, I got this;

Quote

jamesbubbles"2"
The "2" is because I forgot to comment out the json_encode section, but the names are the ones from my table.

So it does look like it's finding the data, but not matching it to the strings sent in by the javascript... how odd...

THe second test doesn't seem to be working, perhaps I'm using an old version of PHP or somthing.. I'll check.

EDIT; PHP 5.2.4...

EDIT#2; Done it.

Quote

Array ( [0] => james [1] => bubbles )


#6
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Good, we've eliminated that problem. It must be whatever is passing the value to $name via GET. Try loading this php file directly in your browser (rather than loading the script that issues JavaScript to pass this value) and putting script.php?name=james - then you should see the json encoded 1 value. If that works fine (which I feel it should) then you will know the problem is in your JavaScript portion and you can then paste that code. Perhaps create a new thread though to get it out of the PHP section and into the JavaScript or AJAX section.

#7
Jubal

Jubal

    Newbie

  • Members
  • PipPip
  • 22 posts
It comes up with 3 still...

I'd guess there's still some problem with data/data types; for some reason, what we're getting from the SQL isn't matching the strings I had in the original array or the input variable.
I may look like an idiot - and I may talklike an idiot - but don't ever let that fool you. I really am an idiot.

#8
Jubal

Jubal

    Newbie

  • Members
  • PipPip
  • 22 posts
Still working on this... I just can't see what could possibly not be working.
I may look like an idiot - and I may talklike an idiot - but don't ever let that fool you. I really am an idiot.

#9
Guest_Jordan_*

Guest_Jordan_*
  • Guests
I just noticed...

if (in_array(strtoupper($name), $userName))

In your test above you are converting $name (which is your GET variable) into upper case but you said when you did an echo on the $userName array (extracted from your database) that the output was "Array ( [0] => james [1] => bubbles )" which is all lowercase.

#10
Jubal

Jubal

    Newbie

  • Members
  • PipPip
  • 22 posts
Ohh... so if I put the variables into the SQL table in uppercase it might work?
I may look like an idiot - and I may talklike an idiot - but don't ever let that fool you. I really am an idiot.

#11
Guest_Jordan_*

Guest_Jordan_*
  • Guests
or strtolower() your variables rather than strtoupper() since they are already lower in your DB.

#12
Jubal

Jubal

    Newbie

  • Members
  • PipPip
  • 22 posts
OK... I'll try that tomorrow, I need to get some sleep now for once.

Thanks
I may look like an idiot - and I may talklike an idiot - but don't ever let that fool you. I really am an idiot.