Jump to content

The quivilant of a standard array but coming from a database...


suttercain

Recommended Posts

I know the answer is simple, but I have been under the weather and it's affecting my thinking  :-[

 

I am doing a form check via ajax, php and mysql. I think the problem I am running into is on the database query.

 

The code basically allows a user to input some data in a form field, the ajax checks to see if that data exists in the database or not. "That username is already taken" type of thing.

 

Here's the PHP code:

<?php

require('connection.php');
$q=$_REQUEST['username'];
$statement = "SELECT distinct(engine_family_name) FROM eo_engine_family WHERE engine_family_name = '".$q."'";
$results = mysql_query($statement) or die(mysql_error());
while($row=mysql_fetch_assoc($results))
{
$taken_usernames=$row;
} 

    /*$taken_usernames = array(
        'remy',
        'julie',
        'andrew',
        'andy',
        'simon',
        'chris',
        'nick'
    );*/

?>

 

Note: When I use the standard array (the commented out part), the code works. It'll say 'nick' is already taken, etc. But when I instead try and replace it with a sql query result, it doesn't work. How do I get the mysql result to act like that of the regular 'static' array?

 

Any suggestions?

 

Thanks.

You don't need to fetch the query, just check if it returns one record.

 

Try this:

<?php

// Chech if mysql returns one row
if(mysql_num_rows($results) == 1) {

   // One record returned
   echo 'username taken';

} else {

    // username available
    echo 'username available';

}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.