0

Trying to: get all users by using the query in the below script into an array

Looking to get output like this:

$existing_users=array('value','value','value', 'value'...)

Php Script:

<?php
require_once('../../../inc/db/dbc.php');
$connect = mysql_connect($h, $u, $p) or die ("Cant Connect to Database.");
mysql_select_db($db);

$q = mysql_query("SELECT uUName FROM User");

$existing_users=array(
    while ($row = mysql_fetch_array($q, MYSQL_NUM)) { 
        echo " '$row'.','";
    }
);

// $existing_users=array('joe','warren','tim'); 
# ^^^^ manual way of doing it

//value got from the get metho
$user_name=$_POST['user_name'];
//checking weather user exists or not in $existing_users array
if (in_array($user_name, $existing_users))
{
    //user name is not availble
    echo "no";
} 
else
{
    //user name is available
    echo "yes";
}
?>

How do I accomplish this? I know my while/array formation is a bit off there, how do I fix this?

Thanks

1
  • If you want to check if the username is available, you can make a select like this: SELECT uUName FROM User WHERE uUName = '$user_name'; If the select has data, the username is not available. Of course, you have to validate your input data first. Commented Nov 22, 2011 at 0:41

1 Answer 1

1
$existing_users = Array();

while(list($username) = mysql_fetch_row($q)) $existing_users[] = $username;

mysql_fetch_row($q) will give you Array(0 => result:uUName)

so list($xxx) = mysql_fetch_row($q) - $xxx is first element of Array(0 => result:uUName) equals your result:uUName.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.