Mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shoxlx
Forum Newbie
Posts: 23
Joined: Tue Oct 11, 2005 10:42 pm

Mysql

Post 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();
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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!
Last edited by evilmonkey on Wed Jan 04, 2006 2:42 pm, edited 1 time in total.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Mysql

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
shoxlx
Forum Newbie
Posts: 23
Joined: Tue Oct 11, 2005 10:42 pm

Post 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...
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

print_r($row) should shed some light on what you're getting. :)
shoxlx
Forum Newbie
Posts: 23
Joined: Tue Oct 11, 2005 10:42 pm

Post 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 )
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post 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.
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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;
Post Reply