473,320 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Searching through an array

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
10 1464
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
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
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
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
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
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
>
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Sims | last post by:
Hi, Given a string $txt and an array of strings $txt_array what would be the best/fastest way to search in _insensitive_ case if $txt is in $text_array and, if it is, where is it? Because I...
3
by: hivie | last post by:
I have a problem that is causing me problems. I have a text file that stores 5 lines of crap (stuff that I dont need( for the user only)). After that there is data that is in three columns...
5
by: Alan Mackenzie | last post by:
I've recently moved onto a C++ project with a large number of directories (several hundred) containing an even larger number of C++ source files. There are vastly more ways in C++ to obfuscate a...
1
by: romain.jouin | last post by:
Hi everyone, I just wanted to propose this piece of code which is searching recursively a value in an array... and let you indicate me if there is an error... function...
6
by: Bernie Yaeger | last post by:
I need a little help with an algorithm. Let's say I have an array with 15 items. I want to find "Fern" where there are "Able", "Me Also", "Zimmerman", etc in no particular order. Forget about...
2
by: Carlos K | last post by:
Hello I'm having some difficulty searching a set of rows in a DataRow collection.. This is my question What would be an efficient way to search any column of an DataRow array Let me try to...
33
by: Geoff Jones | last post by:
Hiya I have a DataTable containing thousands of records. Each record has a primary key field called "ID" and another field called "PRODUCT" I want to retrieve the rows that satisy the following...
3
by: Aaron | last post by:
I'm trying to parse a table on a webpage to pull down some data I need. The page is based off of information entered into a form. when you submit the data from the form it displays a...
5
by: lemlimlee | last post by:
hello, this is the task i need to do: For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The...
3
by: jac130 | last post by:
the program runs, and user is prompted via inputbox to enter an integer-this is the size of the array, then the user fills the array with that many values...but as the user enters the values, i need...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.