View Full Version : How to make database values into arrays?

04-13-2008, 04:56 PM
My learning curve of Arrays is climbing, but keep getting stuck!

How to turn database values into arrays, then loop, to display that they exist?

$db = mysql_connect("host", "user", "pass");
$query = "SELECT * FROM table";
$result = mysql_query($query) or die ('<p>' . mysql_error());
while ($row = mysql_fetch_array($result)) {
//check to see if the data exists
if (!empty($row['field']))
echo '<br>',$row['field'];
$usernames = $row['field'];

//this varible contains the array of existing users

//loop to show the data
foreach ( $row as $existing_users ){
//$existing_users=array('roshan','mike','jason'); // this one gave no errors.


//checking weather user exists or not in $existing_users array

//error on line 25
if (in_array($user_name, $existing_users)) // this is LINE 25
//error on line 25

//user name does not exist
echo "no";
//user name exists
echo "yes";

Line 25 gives an error:

Warning: in_array() [function.in-array (http://localhost:81/furkids-php/pass/jq/function.in-array)]: Wrong datatype for second argument in user.php on line 25


04-13-2008, 06:51 PM
$row is already an array

04-13-2008, 06:58 PM
Cool, but I can't comment out this line:
It gives me this error:
Warning: Invalid argument supplied for foreach() in user.php on line 20I am not that good at arrays just yet, well not with the array / database part.

How else would / could I change the array name to "$existing_users" and loop to display the data?

04-13-2008, 07:08 PM
you are trying to create an array into something which is already an array

also to create an array from scratch use...

$varArray = array(a,b,c,d);

$row is already an array so you dont need to do anything with it

to access your array use a do while loop...

im still confused what you want

04-13-2008, 07:09 PM
oh ic what your wanting

you wanting to check if a user exists


04-13-2008, 07:12 PM
What I would like is to convert the database values into an array.
Once I have the array, loop through the array with an echo, print or print_r

Select * from table
$row['fieldname'] into an array

YES to the last post you posted.

04-13-2008, 07:15 PM
$ROW is an array !!!!!!!!!!!

'fieldname' is the key of the array

you can loop through it using a do while loop

04-13-2008, 07:19 PM
I have that part already.
while ($row = mysql_fetch_array($result)) {
But to convert the array name I am using this:foreach ( $row as $existing_users ){
But it's not working.

04-13-2008, 07:23 PM
ok a foreach will loop through the array but wont move the mysql pointer

all you will get is a loop displaying the first row of the db

you need to use a do while to loop through th db

04-13-2008, 07:26 PM
Where do I add the new do while loop?
It already displays the data from the db at the top of the code.

Oh I see, you mean search with a "do while loop" in the array part?

04-13-2008, 07:35 PM
I have no clue where to add this:
do {
//before or after the foreach?
} while ($row = mysql_fetch_assoc($row)); ?>

04-13-2008, 08:45 PM
Once something becomes a hassle, sleep on it.
I am sure I can figure this one out after I wake up.



04-13-2008, 10:12 PM
// do db loop and add to seperate array

do {

$username[] = $row['fieldname'];

} while ($row = mysql_fetch_assoc($row));

$table = "<table>";

foreach($username as $values){

$table .= "<tr><td>$values</td></tr>";


$table .= "</table>";

echo $table;

04-14-2008, 12:51 AM
That was my next choice, a bit longer but basically reset the MySQL pointer and loop through the database field.

Then I thought about it in my not so sleepy sleep, :shock: scrap arrays and just use a plain old SQL query :mrgreen:

Here it is:

//connect to database, blah blah

$query = "SELECT * FROM table where fieldname = '{$_POST['username']}'";

$result = mysql_query($query);
//username already exists
echo "yes";
echo "no";

04-14-2008, 05:50 AM
i could have told you that but i thought you wanted to display the names from an array?

if you look at my login tut video you will see that im doing exactly that when checking if the user exists before allowing them in

04-14-2008, 06:05 AM
Thanks will do, after I am done with the arrays bit.
You say it checks it during the login phase, what about the important registration phase?

== Busy downloading EMS SQL manager 2007, hope I enjoy this one ==

04-14-2008, 06:09 AM
what about the important registration phase?

yes you would use the same example to do that to

04-14-2008, 06:37 AM
Cool, will do it then.