Connecting Tech Pros Worldwide Forums | Help | Site Map

Mysql doesnt return all results of a query

Börni
Guest
 
Posts: n/a
#1: Jul 17 '05
Hi,
I have an sql query like this:
SELECT column FROM table WHERE column1="3" AND column2="1"
This query works perfectly if i run it in the command line, to be
exactly it return two results.
But if i run it from php i just get the first of the two results.
Any ideas?

Mysql 4.1.8
php 5.0.3

I have a second problem. But its more of a question.
if i run the foreach loop on an array like this
array("id" => "78"), then it splits 78 up in 7 and 8.
Now you say its stupid to use a foreach for this array. But in the
function there i use it the array could also be two dimensional.
Is this behavior normal? I looked in the php manual but found nothing.

Greetings

Richards Noah \(IFR LIT MET\)
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Mysql doesnt return all results of a query



"Börni" <b.reiter@onlinehome.de> wrote in message
news:crc3te$qmq$1@online.de...[color=blue]
> Hi,
> I have an sql query like this:
> SELECT column FROM table WHERE column1="3" AND column2="1"
> This query works perfectly if i run it in the command line, to be
> exactly it return two results.
> But if i run it from php i just get the first of the two results.
> Any ideas?
>[/color]
Please show us how you're accessing the elements in the array. The general
form is:

$result = mysql_query($somequery) or die('Invalid query: ' . mysql_error());

while($row = mysql_fetch_row($results))
{
//Do whatever you want with each row here
}
[color=blue]
> Mysql 4.1.8
> php 5.0.3
>
> I have a second problem. But its more of a question.
> if i run the foreach loop on an array like this
> array("id" => "78"), then it splits 78 up in 7 and 8.
> Now you say its stupid to use a foreach for this array. But in the
> function there i use it the array could also be two dimensional.
> Is this behavior normal? I looked in the php manual but found nothing.
>
> Greetings[/color]

Again, please provide an example. I'm not sure exactly how you are doing
it, but the usual way is:

foreach($somearray as $key => $value)
{
print("Key: $key \tValue: $value");
}

For the array you listed above, it would print:

Key: id Value: 78

I'm not sure whether or not this helps, but if you provide a little more
information, we can help you more. Provide us code snippets for the two
problems and you'll be all set :)


Richards Noah \(IFR LIT MET\)
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Mysql doesnt return all results of a query



"Richards Noah (IFR LIT MET)" <Noah.Richards@infineon.com> wrote in message
news:crc86p$mfj$1@athen03.muc.infineon.com...[color=blue]
>
> "Börni" <b.reiter@onlinehome.de> wrote in message
> news:crc3te$qmq$1@online.de...[color=green]
> > Hi,
> > I have an sql query like this:
> > SELECT column FROM table WHERE column1="3" AND column2="1"
> > This query works perfectly if i run it in the command line, to be
> > exactly it return two results.
> > But if i run it from php i just get the first of the two results.
> > Any ideas?
> >[/color]
> Please show us how you're accessing the elements in the array. The[/color]
general[color=blue]
> form is:
>
> $result = mysql_query($somequery) or die('Invalid query: ' .[/color]
mysql_error());[color=blue]
>
> while($row = mysql_fetch_row($results))[/color]

Grrr, this should be $result, not $results. Sorry about that.
[color=blue]
> {
> //Do whatever you want with each row here
> }
>[/color]

<snip>


Börni
Guest
 
Posts: n/a
#4: Jul 17 '05

re: Mysql doesnt return all results of a query


Richards Noah (IFR LIT MET) wrote:[color=blue]
>
> Please show us how you're accessing the elements in the array. The general
> form is:[/color]
Well i'm just doing it like this:
$result = $mysqli->query($sql);
$array = $result->fetch_assoc();
var_dump($array);

The var_dump shows me just one element in the array. But it should be two.

[color=blue]
> Again, please provide an example. I'm not sure exactly how you are doing
> it, but the usual way is:
>[/color]
Ok this problem is solved, but still its weird. Look at the example.

$test = array("id" => "92");
var_dump($test);
foreach ($test as $id) {
echo $id['id']."<br />";
}

Trying this your output is "9"

$test = array("id" => "92");
var_dump($test);
foreach ($test as $id) {
echo $id."<br />";
}

And now its "92".
Richards Noah \(IFR LIT MET\)
Guest
 
Posts: n/a
#5: Jul 17 '05

re: Mysql doesnt return all results of a query



"Börni" <b.reiter@onlinehome.de> wrote in message
news:crcbct$53p$1@online.de...[color=blue]
> Richards Noah (IFR LIT MET) wrote:[color=green]
> >
> > Please show us how you're accessing the elements in the array. The[/color][/color]
general[color=blue][color=green]
> > form is:[/color]
> Well i'm just doing it like this:
> $result = $mysqli->query($sql);
> $array = $result->fetch_assoc();
> var_dump($array);
>
> The var_dump shows me just one element in the array. But it should be two.
>[/color]
You are misinterpreting what mysql_fetch_assoc() (and other calls such as
mysql_fetch_row() and mysql_fetch_array()) do. They return only one row at
a time, and each row they return is an array of all fields for that row.

So, when you say (as taken from your original post):

"SELECT column FROM table WHERE column1="3" AND column2="1""

You are selecting only 1 piece of data (column) for every row returned.

Run this code instead:


$result = $mysql_query($sql);
while($array = mysql_fetch_assoc($result))
var_dump($array);


This will var_dump each row. The while loop will continue until FALSE is
returned by mysql_fetch_assoc().

I would suggest reading up on these functions
(http://www.php.net/manual/en/functio...etch-assoc.php,
http://www.php.net/manual/en/functio...etch-array.php,
http://www.php.net/manual/en/functio...fetch-row.php).

[color=blue]
>[color=green]
> > Again, please provide an example. I'm not sure exactly how you are[/color][/color]
doing[color=blue][color=green]
> > it, but the usual way is:
> >[/color]
> Ok this problem is solved, but still its weird. Look at the example.
>
> $test = array("id" => "92");
> var_dump($test);
> foreach ($test as $id) {
> echo $id['id']."<br />";
> }
>
> Trying this your output is "9"[/color]

Which is the intended behavior :) Here's what you are doing:

1) On each iteration, $id becomes the _value_ of one of the array elements.
So, on the first iteration, it becomes "92".
2) when you say $id['id'], you are using indexing on a string. So 'id' (as
the index) is evaluated as an integer, which is 0.
3) You access $id[0], which is the first character of the string "92",
which is "9".

Try it with any other string ("$id['ilikepie']" or "$id['astring']") and it
will yield the same result. Try it with $id[0] and $id[1] and you'll get
"9" and "2", respectively.
[color=blue]
>
> $test = array("id" => "92");
> var_dump($test);
> foreach ($test as $id) {
> echo $id."<br />";
> }
>
> And now its "92".[/color]

This is the correct way to use foreach. As I mentioned before, if you need
to access both the key and value, use foreach like:

foreach($test as $id = > $value)
{
print("Key of $id has Value of $value");
}

This will print:
Key of id has Value of 92.

If you need more explanation, consult the doc (http://www.php.net/foreach).


Richards Noah \(IFR LIT MET\)
Guest
 
Posts: n/a
#6: Jul 17 '05

re: Mysql doesnt return all results of a query



"Richards Noah (IFR LIT MET)" <Noah.Richards@infineon.com> wrote in message
news:crcda1$qp3$1@athen03.muc.infineon.com...[color=blue]
>[/color]

<snip>
[color=blue]
> foreach($test as $id = > $value)
> {[/color]

There shouldn't be a space inbetween the = and the >

Should be:

foreach($test as $id => $value)

Sorry 'bout that.

<snip>


porneL
Guest
 
Posts: n/a
#7: Jul 17 '05

re: Mysql doesnt return all results of a query


[color=blue]
> $test = array("id" => "92");
> var_dump($test);
> foreach ($test as $id) {
> echo $id['id']."<br />";
> }
>
> Trying this your output is "9"[/color]

It's weird that it prints anything.

You're using foreach in a wrong way.
Read docs, especially about foreach($array as $key => $value);


--
* html {redirect-to: url(http://browsehappy.pl);}
dakeyras@gmail.com
Guest
 
Posts: n/a
#8: Jul 17 '05

re: Mysql doesnt return all results of a query


The fact that it prints isn't "weird". Check up on the documentation
for strings (http://us3.php.net/manual/en/language.types.string.php)
under "String access and modification by character", which is
essentially what he is doing. The only thing to note is that using
array-brackets is deprecated as of PHP4 (the preffered way is curly
brackets, i. e. $string{4}, but PHP still lets you use [4] for
backwards compatibility).

Börni
Guest
 
Posts: n/a
#9: Jul 17 '05

re: Mysql doesnt return all results of a query


Thank you all for your answers, you helped me a lot.
Pedro Graca
Guest
 
Posts: n/a
#10: Jul 17 '05

re: Mysql doesnt return all results of a query


porneL wrote [ with attribution snipped; please don't do that ]:[color=blue]
> Börni wrote:[color=green]
>> $test = array("id" => "92");
>> var_dump($test);
>> foreach ($test as $id) {
>> echo $id['id']."<br />";
>> }
>>
>> Trying this your output is "9"[/color]
>
> It's weird that it prints anything.[/color]

To add to dakeyras@gmail.com post let me just point out that

'id' taken in a numeric context is the same as 0, and therefore
$id['id'] (since $id is not an array) is taken as the first char of $id.

--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!
Closed Thread