473,788 Members | 2,861 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

An extra last empty field in an 'mysql_fetch_ar ray' result array?

Hello,

I seem to get an extra empty field in every 'mysql_fetch_ar ray' 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("SE LECT ID from tblName WHERE Name='Jane'");
if (!$oCursor)
{
$bGo = false;
}
else
{
$aRow = mysql_fetch_arr ay($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 2965

"Marcel Brekelmans" <ma****@marce l-art.com> wrote in message
news:29******** *************** *******@giganew s.com...
.....
$oCursor = mysql_query("SE LECT ID from tblName WHERE Name='Jane'");
if (!$oCursor)
{
$bGo = false;
}
else
{
$aRow = mysql_fetch_arr ay($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($s ql) or die("Error: " . mysql_error()." sql was
".$sql);
$ret = array();
while($row = mysql_fetch_ass oc($result)) {
$ret[] = $row;
}
mysql_free_resu lt($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("SE LECT ID from tblName WHERE Name='Jane'");
if (!$oCursor)
{
$bGo = false;
}
else
{
$aRow = mysql_fetch_arr ay($oCursor);
}

results in:

count($aRow) = 2;

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


Marcel,

mysql_fetch_arr ay 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("SE LECT ID from tblName WHERE Name='Jane'");
if (!$oCursor)
{
$bGo = false;
}
else
{
$aRow = mysql_fetch_arr ay($oCursor);
}

results in:

count($aRow) = 2;

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


Marcel,

mysql_fetch_arr ay 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_ar ray' 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("SE LECT ID from tblName WHERE Name='Jane'");
if (!$oCursor)
{
$bGo = false;
}
else
{
$aRow = mysql_fetch_arr ay($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_arr ay by default returns by column name and
by number...

Use mysql_fetch_arr ay($oCursor,MYS QL_NUM) or
mysql_fetch_arr ay($oCursor,MYS QL_ASSOC) instead.

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

(see "Return Values" section)

--
Justin Koivisto, ZCE - ju****@koivi.co m
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
2029
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 $_SESSION = $_GET;
8
16979
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 this: figure out how many array values there are then out put all but the last one, then write some text, then write the last value of the array. Thanks in advance.
15
2689
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
6107
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); I can then use mysql_fetch_array in a while loop to access each row in the result. This all works fine.
7
6015
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 = mysql_fetch_row($result) ) { echo $x; } but it seems kinda silly to fetch an entire array, when only the first index is used. is there a better/more efficient way?
2
1933
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 database. I have it working with just the username field but I'm confused on how to go about adding another field. I'm pretty new to PHP so don't beat me up too much for this example code, I borrowed and hacked it together in a very short period...
1
1315
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 use php to enter the data into in the mysql database, it enters empty data even when a field is null. html <form method='POST' action='entry2.php' name='entry'> <table> <tr><td colspan='4' align='center'>Mailing Address</td></tr>...
5
6648
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 */ function myClass(){
8
3909
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 using php and jquery through a mysql database will not empty the td elements. There is a table underneath the button in the initial html. if you click on a box in the table, it will empty. the same does not happen for the table that is generated by...
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10172
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.