By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,456 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

Searching through an array

P: n/a
I have an array created but I need to search it. The array is created
from a mysql call. Code as follows:

******************
function respond_check($qid)
{
include "db.inc.php";
$postchk2 = mysql_query("SELECT * FROM `answers` , `users` WHERE `qid`
= $qid AND `a_uid` = `userid` LIMIT 0, 30",$db);

while ($userx=mysql_fetch_array($postchk2))
{
$poster2 = $u_row22['username'];
}

}

******************
I've tried including various *if* statements to search for $X in the
array (which does exist) but I keep failing miserably. Suggestions?

May 17 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Message-ID: <11*********************@n59g2000hsh.googlegroups. comfrom
Akhenaten contained the following:
>I've tried including various *if* statements to search for $X in the
array (which does exist) but I keep failing miserably. Suggestions?
http://uk.php.net/manual/en/function.in-array.php

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
May 17 '07 #2

P: n/a
On May 17, 6:18 pm, Geoff Berrow <blthe...@ckdog.co.ukwrote:
Message-ID: <11*********************@n59g2000hsh.googlegroups. comfrom

http://uk.php.net/manual/en/function.in-array.php

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDshttp://www.ckdog.co.uk/rfdmaker/

I played around with the above for quite a bit. I don't think I'm
callingthe array correctly when I use in_array. All the examples given
on the in_array page work ok if you are manually creating the array.
I'm stumbling because I'm creating mine via a sql call.

May 17 '07 #3

P: n/a
Message-ID: <11**********************@l77g2000hsb.googlegroups .comfrom
Akhenaten contained the following:
>I played around with the above for quite a bit. I don't think I'm
callingthe array correctly when I use in_array. All the examples given
on the in_array page work ok if you are manually creating the array.
I'm stumbling because I'm creating mine via a sql call.
That shouldn't make a difference. We need to see code.
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
May 17 '07 #4

P: n/a
On May 17, 6:45 pm, Geoff Berrow <blthe...@ckdog.co.ukwrote:
>
That shouldn't make a difference. We need to see code.
--

function respond_check($qid)
{
include "db.inc.php";
$postchk2 = mysql_query("SELECT * FROM `answers` , `users` WHERE `qid`
= $qid AND `a_uid` = `userid` LIMIT 0, 30",$db);
while ($userx=mysql_fetch_array($postchk2))
{
if (in_array($username, $userx)) {
echo "Got It!";
}
}
}

?>

May 17 '07 #5

P: n/a
Message-ID: <11**********************@k79g2000hse.googlegroups .comfrom
Akhenaten contained the following:
>
function respond_check($qid)
{
include "db.inc.php";
$postchk2 = mysql_query("SELECT * FROM `answers` , `users` WHERE `qid`
= $qid AND `a_uid` = `userid` LIMIT 0, 30",$db);
while ($userx=mysql_fetch_array($postchk2))
{
if (in_array($username, $userx)) {
echo "Got It!";
}
}
}

Can't see why that won't work offhand. Could it be a case sensitive
thing?

But why don't you use the query to search for the username?
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
May 18 '07 #6

P: n/a
Akhenaten wrote:
function respond_check($qid)
{
include "db.inc.php";
$postchk2 = mysql_query("SELECT * FROM `answers` , `users` WHERE `qid`
= $qid AND `a_uid` = `userid` LIMIT 0, 30",$db);
while ($userx=mysql_fetch_array($postchk2))
{
if (in_array($username, $userx)) {
echo "Got It!";
}
}
}
Never use PHP to do MySQL work.
Select only data you need. Something like this.

$sql="SELECT * FROM answers INNER JOIN users ON a_uid=userid WHERE
qid=$qid AND username IN (".implode(',',$quotedUserNamesArray).")";
May 18 '07 #7

P: n/a
>
But why don't you use the query to search for the username?
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDshttp://www.ckdog.co.uk/rfdmaker/

I actually did adjust the query to pull just the username (for
simplicity) but it still doesn't pull. The odd thing making pull my
hair out is that I can echo out all the variables involved just fine
and can even dump the array. For some reason I just can't seem to
search it or compare X to what's inside it.

May 18 '07 #8

P: n/a
Akhenaten wrote:
I have an array created but I need to search it. The array is created
from a mysql call. Code as follows:

******************
function respond_check($qid)
{
include "db.inc.php";
$postchk2 = mysql_query("SELECT * FROM `answers` , `users` WHERE `qid`
= $qid AND `a_uid` = `userid` LIMIT 0, 30",$db);

while ($userx=mysql_fetch_array($postchk2))
{
$poster2 = $u_row22['username'];
}

}

******************
I've tried including various *if* statements to search for $X in the
array (which does exist) but I keep failing miserably. Suggestions?
I think maybe your problem is $postchk2 is a resource, not an array.
You get a single-dimensional array when you call mysql_fetch_array - but
only the current rows. If you want to search the entire contents
retrieved by MySQL, you need to do something like read everything
returned into an array in PHP then search it. Beware this can take huge
amounts of memory in the case of large rows and/or number of rows.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
May 18 '07 #9

P: n/a
Message-ID: <11**********************@p77g2000hsh.googlegroups .comfrom
Akhenaten contained the following:
>I actually did adjust the query to pull just the username (for
simplicity) but it still doesn't pull.
Well it should so keep looking, you have a mistake somewhere. Check the
original data. Is it really the same as the data you are comparing it
with? Furthermore, have you checked that the username is unique before
inserting it in the first place?

Assuming there is a column in the users table called username, something
like:
$sql="SELECT * FROM `answers` , `users` WHERE `qid`
= $qid AND `a_uid` = `userid` AND `users.username`='$username'";
//echo $sql;
$postchk2 = mysql_query($sql,$db);
while($userx=mysql_fetch_array($postchk2)){
print_r($userx);
}

Should produce some results. If not then you can uncomment the $sql and
take a look at it. I usually use MySql and phpMyadmin and so I can just
paste the SQL in to check it.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
May 18 '07 #10

P: n/a
Akhenaten wrote:
function respond_check($qid)
{
include "db.inc.php";
$postchk2 = mysql_query("SELECT * FROM `answers` , `users` WHERE `qid`
= $qid AND `a_uid` = `userid` LIMIT 0, 30",$db);
while ($userx=mysql_fetch_array($postchk2))
{
if (in_array($username, $userx)) {
echo "Got It!";
}
}
}
You haven't defined the variable $username anywhere, unless it's defined
somewhere in the included file.

--
Toby A Inkster BSc (Hons) ARCS
http://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux
May 18 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.