going true the whole table

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

going true the whole table

Post by ol4pr0 »

Oke this sounds a little dumb maby, why putting mails in somany rows ( well i have actually a reason for that )

but what i am trying to do now is to find a email going true all these fields
i have email untill email24 field.
however i do not get any results neither do i get a error.

Code: Select all

for ($i=1;$i<=24;$i++)
$query = 'SELECT email'.$i.' FROM email WHERE email'.$i.'="'.$_POST ['check'].'"';
#using SELECT id FROM... works but if there are duplicate emails it only
#returns one id.. i need them all 
$result = mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_array($result)) {
	echo $row['id'];
}
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

8O you have 24 fields dedicated to email in this table? You seriously need to rethink your table structure.. yikes..

The code you posted will only search the database for email24.. $row will never contain 'id' because you didn't select it.
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

yea i know, however i got lots and i mean lots of mails lol. (meaning thousands)

Any ideas on how i can archive this?
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Could you not just use something like:

Code: Select all

$query = "SELECT * FROM email";
$result = mysql_query($query) or die(mysql_error());

while(true)
{
 $row = mysql_fetch_assoc($result);
 if ($row == false) break;
 echo $row['id'];
}
in order to view the id's?
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Becuase there are many ids.. and i need the id that belongs to the email so i can find it lots and lots faster.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: going true the whole table

Post by timvw »

ol4pr0 wrote:
but what i am trying to do now is to find a email going true all these fields
i have email untill email24 field.
however i do not get any results neither do i get a error.

Code: Select all

for ($i=1;$i<=24;$i++)
$query = 'SELECT email'.$i.' FROM email WHERE email'.$i.'="'.$_POST ['check'].'"';
#using SELECT id FROM... works but if there are duplicate emails it only
#returns one id.. i need them all 
$result = mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_array($result)) {
	echo $row['id'];
}
}
Meaby i don't understand what you are trying to do, but if i do, this would be how i do it:

Code: Select all

// cleanup input
$post = mysql_escape_string($_POST['check']);

// build query
$query = 'SELECT * FROM email WHERE ';
for ($i=1;$i<=24;$i++)
{
	$query.= 'email' . $i . "='" . $post . ' OR ';
}
$query .= '1=1';

$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_assoc($row))
{
	print_r($row);
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

:? wouldn't that grab every row?
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Code: Select all

$query = 'SELECT * FROM email WHERE ';
for ($i=1;$i<=24;$i++)
{
   $query.= 'email' . $i . '="' . $post . '" OR ';
}
$query .= '1=1';

$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_assoc($row))
{
   print_r($row);
} 
}
Had to alter it a little bit., but it actually cant execute it
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

mysql_fetch_assoc($result)
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Hmm I wonder why you were using $row instead of $result :O
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Lol. must of been late last night

Joe: I am wondering that 2 hahaha..

timvw: sorry br0 that didnt go well, as feyd said "would'nt that grab every row?" ( yes it will )
But it does not get me the right id.

However it might be useful somehow, i am not sure how, There must be a way i can substract the information i need from that array ? wouldnt there.

meaning

output ej:
[email6] => somemail@somedomain.com

What can i use to get the [email6] and somemail@domein.com part

This does get me somwhere i think but, maby someone can help me getting it the way it should

Code: Select all

if (in_array($_POST['check'], $row))
	{
	echo 'fount it<P>';
	print_r($row);
	}
	else
	{
	echo '<br>Not found';
	}
Post Reply