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

Mysql doesnt return all results of a query

P: n/a
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
Jul 17 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a

"Börni" <b.******@onlinehome.de> wrote in message
news:cr**********@online.de...
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?
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
}
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


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 :)
Jul 17 '05 #2

P: n/a

"Richards Noah (IFR LIT MET)" <No***********@infineon.com> wrote in message
news:cr**********@athen03.muc.infineon.com...

"Börni" <b.******@onlinehome.de> wrote in message
news:cr**********@online.de...
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?
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))
Grrr, this should be $result, not $results. Sorry about that.
{
//Do whatever you want with each row here
}


<snip>
Jul 17 '05 #3

P: n/a
Richards Noah (IFR LIT MET) wrote:

Please show us how you're accessing the elements in the array. The general
form is: 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.

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

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".
Jul 17 '05 #4

P: n/a

"Börni" <b.******@onlinehome.de> wrote in message
news:cr**********@online.de...
Richards Noah (IFR LIT MET) wrote:

Please show us how you're accessing the elements in the array. The general form is: 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.

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).

Again, please provide an example. I'm not sure exactly how you are doing it, but the usual way is:
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"


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.

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

And now its "92".


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).
Jul 17 '05 #5

P: n/a

"Richards Noah (IFR LIT MET)" <No***********@infineon.com> wrote in message
news:cr**********@athen03.muc.infineon.com...

<snip>
foreach($test as $id = > $value)
{


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

Should be:

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

Sorry 'bout that.

<snip>
Jul 17 '05 #6

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

Trying this your output is "9"


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);}
Jul 17 '05 #7

P: n/a
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).

Jul 17 '05 #8

P: n/a
Thank you all for your answers, you helped me a lot.
Jul 17 '05 #9

P: n/a
porneL wrote [ with attribution snipped; please don't do that ]:
Börni wrote:
$test = array("id" => "92");
var_dump($test);
foreach ($test as $id) {
echo $id['id']."<br />";
}

Trying this your output is "9"


It's weird that it prints anything.


To add to da******@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!
Jul 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.