Page 1 of 1

Mysql

Posted: Wed Jan 04, 2006 2:22 pm
by shoxlx
i need to be able to pull get data from a mysql database and put it inot an array. then i need to search that array for a string... iv tried using the mysql_fetch_array and stuff but i wont work like i need it to...


heres the code

Code: Select all

// Connect to MySQL Database
@mysql_connect($dbhost,$dbuser,$dbpass);
@mysql_select_db($dbname) or die("No db");

$query  = "SELECT user FROM status";
$result = mysql_query($query);
$users  = mysql_num_rows($result);

$row = mysql_fetch_array($result);

if (in_array($user, $row)){ $status = 'Online'; }else{ $status = 'Offline'; }

@mysql_free_result($result);

@mysql_close();

Posted: Wed Jan 04, 2006 2:39 pm
by evilmonkey
It doesn't work because you're pulling one row. You need to do this:

Code: Select all

@mysql_connect($dbhost,$dbuser,$dbpass);
@mysql_select_db($dbname) or die("No db");

$query  = "SELECT user FROM status";
$result = mysql_query($query);
$users  = mysql_num_rows($result);

for($i=0; $i<$users; $i++){ 
	       $row[] = mysql_fetch_assoc($result); 
}
//you now have a 2D array that you can do your checks on
In the future, please make your thread titles more informative, and use

Code: Select all

tags instead of

Code: Select all

tags for php code.

Good luck!

Re: Mysql

Posted: Wed Jan 04, 2006 2:40 pm
by s.dot
shoxlx wrote:then i need to search that array for a string
use in_array() if the complete string is an element in the array

Posted: Wed Jan 04, 2006 2:45 pm
by shoxlx
Thank you... and sry about the title and code...

when i tried the code and went to echo the array to see what it would say was in the database for each of the arrays it echos array...

Code: Select all

$query  = "SELECT user FROM status";
$result = mysql_query($query);
$users  = mysql_num_rows($result);

for($i=0; $i<$users; $i++){ 
           $row[] = mysql_fetch_assoc($result); 
} 

echo $users;

$num = count($row);
for($i=0; $i<$num; $i++){
  echo $row[$i].'<br>';
}
that echos
array
array
array
etc...

Posted: Wed Jan 04, 2006 3:03 pm
by evilmonkey
print_r($row) should shed some light on what you're getting. :)

Posted: Wed Jan 04, 2006 3:10 pm
by shoxlx
ok i hate to be a pain but im pretty new at this so... how do i get it to echo just

xxmaddxxxboredxx

and not

[1] => Array ( [user] => xxmaddxxxboredxx )

Posted: Wed Jan 04, 2006 3:21 pm
by pilau
shoxlx wrote:ok i hate to be a pain but im pretty new at this so... how do i get it to echo just

xxmaddxxxboredxx

and not

[1] => Array ( [user] => xxmaddxxxboredxx )
Try this: http://www.hudzilla.org/phpbook/
Check the part that concerns arrays. It's great.

Posted: Wed Jan 04, 2006 4:58 pm
by evilmonkey
More specifically, you access the 2D array like this:

$row[1]['user']
That will have the value of xxmaddxxxboredxx is the above case.

So,
$row[1]['user'] = xxmaddxxxboredxx;