473,408 Members | 2,477 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,408 software developers and data experts.

Mysql doesnt return all results of a query

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
9 2447

"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

"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
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

"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

"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
$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
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
Thank you all for your answers, you helped me a lot.
Jul 17 '05 #9
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Xenophobe | last post by:
I know this isn't a MySQL forum, but my question is related to a PHP project. I have two tables. table1 table2 "table1" contains 2 columns, ID and FirstName:
3
by: Mike Cocker | last post by:
Hello, I'm quite weak at PHP, so I was hoping to get some help understanding the below code. First off, I'm trying to create a "query form" that will allow me to display the results on my...
0
by: Phil Powell | last post by:
The table already has a fulltext index and from there I can use the MySQL fulltext search query to get results as well as the relevancy score. The problem I have is that MySQL has a default...
39
by: Mairhtin O'Feannag | last post by:
Hello, I have a client (customer) who asked the question : "Why would I buy and use UDB, when MySql is free?" I had to say I was stunned. I have no experience with MySql, so I was left sort...
1
by: Good Man | last post by:
Hi there I've noticed some very weird things happening with my current MySQL setup on my XP Laptop, a development machine. For a while, I have been trying to get the MySQL cache to work....
7
by: Daz | last post by:
Hi. I am trying to select data from two separate MySQL tables, where I cannot use join, but when I put the two select queries into a single query, I get an error telling me to check my syntax. Both...
3
by: Me Alone | last post by:
Hello: I am trying to edit some C code I found in "The definitive guide to using, programming, and administering MySQL" by Paul DuBois. This C client program connects and then segfaults when...
15
by: harvey | last post by:
How do I make PHP create a database for mysql please? I can see how to make tables and I have read all the documents I can find but I don't understand how to make the database itself. All...
21
by: bruno_guedesav | last post by:
I've made a function to fetch all results as an array of result- arrays. Getting the result arrays is easy, via mysql_fetch_array, and function itself is quite simple, as follows: function...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.