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

An extra last empty field in an 'mysql_fetch_array' result array?

Hello,

I seem to get an extra empty field in every 'mysql_fetch_array' command I issue. For example:

I have a simple table 'tblName':

ID Name
1 Jane
2 Joe
2 Doe

The following code:

$oCursor = mysql_query("SELECT ID from tblName WHERE Name='Jane'");
if (!$oCursor)
{
$bGo = false;
}
else
{
$aRow = mysql_fetch_array($oCursor);
}

results in:

count($aRow) = 2;

$aRow[0] = 1;
$aRow[1] = '';

Am I missing something, doing something wrong, a wrong PHP setting?

Thanks,
Marcel Brekelmans

Oct 13 '05 #1
4 2933

"Marcel Brekelmans" <ma****@marcel-art.com> wrote in message
news:29******************************@giganews.com ...
.....
$oCursor = mysql_query("SELECT ID from tblName WHERE Name='Jane'");
if (!$oCursor)
{
$bGo = false;
}
else
{
$aRow = mysql_fetch_array($oCursor);
} results in: count($aRow) = 2; $aRow[0] = 1;
$aRow[1] = ''; Am I missing something, doing something wrong, a wrong PHP setting?


Hoi Marcel

This is the function I use:

function getdata($sql) {
$result = @mysql_query($sql) or die("Error: " . mysql_error()." sql was
".$sql);
$ret = array();
while($row = mysql_fetch_assoc($result)) {
$ret[] = $row;
}
mysql_free_result($result);
return $ret;
}

then, in my code, I used:

$sql = "SELECT ID from tblName WHERE Name='Jane'";
$data = getdata($sql)
foreach ($data as $ds) {
echo $ds["Name"];
}

Once you use it a few times, it just makes life really easy, especially
because you refer to fields by name rather than position (e.g.$ds[0]).

Having said that, I am not sure what you are doing wrong ;-) I think that
you only fetch one row. A better way to do this is

while($row = mysql_fetch_row($query)) {

}

Note the single "is teken".

- Nicolaas
Oct 13 '05 #2
Marcel Brekelmans wrote:
The following code:

$oCursor = mysql_query("SELECT ID from tblName WHERE Name='Jane'");
if (!$oCursor)
{
$bGo = false;
}
else
{
$aRow = mysql_fetch_array($oCursor);
}

results in:

count($aRow) = 2;

$aRow[0] = 1;
$aRow[1] = '';


Marcel,

mysql_fetch_array by default fetches the result both as an associative
array and a numeric array. It has two parameters, the second parameter
is either MYSQL_ASSOC, MYSQL_NUM or MYSQL_BOTH, default is MYSQL_BOTH.
Doing a print_r($aRow) will show this too.

HTH.
Peter.
--
http://www.phpforums.nl
Oct 13 '05 #3

Peter van Schie schreef:
Marcel Brekelmans wrote:
The following code:

$oCursor = mysql_query("SELECT ID from tblName WHERE Name='Jane'");
if (!$oCursor)
{
$bGo = false;
}
else
{
$aRow = mysql_fetch_array($oCursor);
}

results in:

count($aRow) = 2;

$aRow[0] = 1;
$aRow[1] = '';


Marcel,

mysql_fetch_array by default fetches the result both as an associative
array and a numeric array. It has two parameters, the second parameter
is either MYSQL_ASSOC, MYSQL_NUM or MYSQL_BOTH, default is MYSQL_BOTH.
Doing a print_r($aRow) will show this too.

HTH.
Peter.
--
http://www.phpforums.nl


Thanks Peter, that was the solution: using the MYSQL_NUM parameter
restricted the output to the single value I expected.

Oct 14 '05 #4
Please... post in plain text & do it only once (there are 6 identical
messages)

Marcel Brekelmans wrote:
I seem to get an extra empty field in every 'mysql_fetch_array' command
I issue. For example:

I have a simple table 'tblName':

ID Name
1 Jane
2 Joe
2 Doe

The following code:

$oCursor = mysql_query("SELECT ID from tblName WHERE Name='Jane'");
if (!$oCursor)
{
$bGo = false;
}
else
{
$aRow = mysql_fetch_array($oCursor);
}

results in:

count($aRow) = 2;

$aRow[0] = 1;
$aRow[1] = '';

Am I missing something, doing something wrong, a wrong PHP setting?


What is actually set is the following:

Array
(
[0] => 1
[ID] => 1
)

That is because mysql_fetch_array by default returns by column name and
by number...

Use mysql_fetch_array($oCursor,MYSQL_NUM) or
mysql_fetch_array($oCursor,MYSQL_ASSOC) instead.

http://us2.php.net/manual/en/functio...etch-array.php

(see "Return Values" section)

--
Justin Koivisto, ZCE - ju****@koivi.com
http://koivi.com
Oct 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: james | last post by:
I am new to PHP and am trying to run a simple query and display the result, with no luck. Here is the code I am using. <?php //start session session_start(); //store cmpid from querystring...
8
by: Adam Carolla | last post by:
Hello, I have a script that is echo'ing the values of a mysql query using a while. What I need to do is right before the last value is echo'd I need to write "last item". Is there a way to do...
15
by: Good Man | last post by:
Hey there I have a dumb question.... Let's say i have a database full of 4000 people.... I select everything from the database by: $result = mysql_query("SELECT * FROM People");
2
by: Dave Moore | last post by:
Hi All, I've got a simple query hopefully somebody can clear up for me. I need to make a query on a database to select a set of table rows, using something like: $result = mysql_query($query);...
7
by: Mark | last post by:
let's say i have a table in my database, with only one column. all i want to do is retrieve a list of the entries.. i could do it like this $result = mysql_query("SELECT * FROM t1"); while( $x =...
2
by: dylanhughes | last post by:
I'm looking for an example of a login system that has multiple fields (2 to be exact) + password. e.g username, company name and password, the user, company and password are checked against a mysql...
1
by: fishctr | last post by:
Hi There, I am building a form that allows a business to enter at most 2 mailing addresses. i have the form set up so both inputs are there, storing as a post array. the problem is, when i try to...
5
by: jmDesktop | last post by:
In my code I cannot figure out how to retrieve multple rows from my returned array from a class method. I have tried: <?php class myClass { private $connection; /* Class constructor */...
8
by: thatcollegeguy | last post by:
http://smarterfootball.com/exits/theHTML.html I am not sure what is wrong w/ this code. The main issue is that the table that is in the initial html will empty its td but the table that I load...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.