numeric string to single digit array

numeric string to single digit array

am 26.03.2008 00:56:53 von Richard Dunne

Can anyone tell me how how to convert a string of integers into an array of single digit integers. i.e. "1223123" into ['1','2,','2','3','1,'2','3'] ? When I retreive a column of single digit integers I end up with a long string of numbers. I think PHP is seeing this as one whole number and therefore is not splitting or exploding it. I hope I am wrong in my thinking and there is a simple solution, though I am not seeing it. Can anyone help me?

Richard.


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: numeric string to single digit array

am 26.03.2008 01:09:57 von dmagick

Richard Dunne wrote:
> Can anyone tell me how how to convert a string of integers into an array of single digit integers. i.e. "1223123" into ['1','2,','2','3','1,'2','3'] ?

$string = '12345';
$array = array();
for ($i = 0; $i < strlen($string); $i++) {
$array[] = $string[$i];
}

I'm sure there's probably a better way but this is simple and easy to read.

> When I retreive a column of single digit integers I end up with a long string of numbers.

From a database or something? What does your code look like?

--
Postgresql & php tutorials
http://www.designmagick.com/

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: **{SPAM}** [PHP-DB] numeric string to single digit array

am 26.03.2008 01:11:45 von Dustin Simpson

Richard,

Someone might have a quicker/better way, but what about:


$numbers = '1223123';
$numberarray = str_split($numbers,1);
print_r($numberarray);
?>

Thanks,
--Dustin

Richard Dunne wrote:
> Can anyone tell me how how to convert a string of integers into an array of single digit integers. i.e. "1223123" into ['1','2,','2','3','1,'2','3'] ? When I retreive a column of single digit integers I end up with a long string of numbers. I think PHP is seeing this as one whole number and therefore is not splitting or exploding it. I hope I am wrong in my thinking and there is a simple solution, though I am not seeing it. Can anyone help me?
>
> Richard.
>
>
>


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: numeric string to single digit array

am 26.03.2008 01:53:46 von dmagick

Please always CC the mailing list so others can learn and also
contribute answers.

Also please don't top-post as it makes it hard to follow discussions.

Richard Dunne wrote:
> I am using MySQL and retrieving a single column which consists of single digit integers. I have been doing a lot of chopping and changing of my code.
>
> $query ="Select answer from answers where studentID ='A123456789'";

What do you get if you run this query manually through phpmyadmin or a
similar tool?

--
Postgresql & php tutorials
http://www.designmagick.com/

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: numeric string to single digit array

am 26.03.2008 02:13:59 von Richard Dunne

Sorry for the top-posting, it's my mail client, not my design. I honestly have not even looked at myphpadmin much, using the CLI mostly.

----- Original Message -----
From: Chris
Date: Wednesday, March 26, 2008 0:53 am
Subject: Re: [PHP-DB] numeric string to single digit array

>
> Please always CC the mailing list so others can learn and also
> contribute answers.
>
> Also please don't top-post as it makes it hard to follow discussions.
>
> Richard Dunne wrote:
> > I am using MySQL and retrieving a single column which consists
> of single digit integers. I have been doing a lot of chopping and
> changing of my code.
> >
> > $query ="Select answer from answers where studentID ='A123456789'";
>
> What do you get if you run this query manually through phpmyadmin
> or a
> similar tool?
>
> --
> Postgresql & php tutorials
> http://www.designmagick.com/
>


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: numeric string to single digit array

am 26.03.2008 03:18:25 von dmagick

Richard Dunne wrote:
> Sorry for the top-posting, it's my mail client, not my design. I honestly have not even looked at myphpadmin much, using the CLI mostly.

It's easy enough to click to another place in your mail client ;)

So what happens when you run that query manually?


--
Postgresql & php tutorials
http://www.designmagick.com/

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: numeric string to single digit array

am 26.03.2008 16:00:15 von Richard Dunne

----- Original Message -----
From: Chris
Date: Wednesday, March 26, 2008 2:18 am
Subject: Re: [PHP-DB] numeric string to single digit array

> Richard Dunne wrote:
> > Sorry for the top-posting, it's my mail client, not my design.
> I honestly have not even looked at myphpadmin much, using the CLI
> mostly.
>
> It's easy enough to click to another place in your mail client ;)
>
> So what happens when you run that query manually?
>
>
> --
> Postgresql & php tutorials
> http://www.designmagick.com/

I ran this

$query ="Select answer from answers where studentID ='A123456789'";
$result = mysql_query($query,$connection);
$resultArray = str_split($result,1);
$count = count($resultArray);

As I have two rows in my table, count should, at least I hope, return a value of two, but it doesn't. It returns 1. From what I can assertain, str_split is seeing a two digit number and not splitting it into two single digit numbers.


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: numeric string to single digit array

am 26.03.2008 16:12:04 von Evert Lammerts

> I ran this
>
> $query ="Select answer from answers where studentID ='A123456789'";
> $result = mysql_query($query,$connection);
> $resultArray = str_split($result,1);
> $count = count($resultArray);
>

Where's the fetch?

|$result = mysql_query("SELECT answer FROM answers WHERE studentID =
'A123456789';");
$count = 0;
while ($row = mysql_fetch_assoc($result)) {
$count++;
}

|Of course you should count in the query if the result itself is not used:

|$result = mysql_query("SELECT COUNT(answer) AS nr_of_results FROM
answers WHERE studentID = 'A123456789';");
$row = mysql_fetch_assoc($result);
$count = $row['||nr_of_results||'];||
|
Evert

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: numeric string to single digit array

am 26.03.2008 16:34:17 von Richard Dunne

----- Original Message -----
From: Evert Lammerts
Date: Wednesday, March 26, 2008 3:12 pm
Subject: Re: [PHP-DB] numeric string to single digit array

>
> > I ran this
> >
> > $query ="Select answer from answers where studentID ='A123456789'";
> > $result = mysql_query($query,$connection);
> > $resultArray = str_split($result,1);
> > $count = count($resultArray);
> >
>
> Where's the fetch?
>
> |$result = mysql_query("SELECT answer FROM answers WHERE studentID
> =
> 'A123456789';");
> $count = 0;
> while ($row = mysql_fetch_assoc($result)) {
> $count++;
> }
>
> |Of course you should count in the query if the result itself is
> not used:
>
> |$result = mysql_query("SELECT COUNT(answer) AS nr_of_results FROM
> answers WHERE studentID = 'A123456789';");
> $row = mysql_fetch_assoc($result);
> $count = $row['||nr_of_results||'];||
> |
> Evert
>

PHP is telling me that the resource I am using for mysql_fetch_assoc is invalid:

$query ="Select answer from answers where studentID ='A123456789'";
$result = mysql_query($query,$connection);
$count=0;
while($row = mysql_fetch_assoc($result));
{
$count++;
}
echo $count;



--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: numeric string to single digit array

am 26.03.2008 16:39:56 von Evert Lammerts

> PHP is telling me that the resource I am using for mysql_fetch_assoc is invalid:
>
> $query ="Select answer from answers where studentID ='A123456789'";
> $result = mysql_query($query,$connection);
> $count=0;
> while($row = mysql_fetch_assoc($result));
> {
> $count++;
> }
> echo $count;
>

Are you sure your database connection has been made?

$connection = mysql_connect('localhost', 'mysql_user', 'mysql_password')
or die(mysql_error());
etc..

If the connection is made alright, try:

$query ="Select answer from answers where studentID ='A123456789'";
$result = mysql_query($query,$connection) or die(mysql_error());
$count=0;
while($row = mysql_fetch_assoc($result));
{
$count++;
}
echo $count;





--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: numeric string to single digit array

am 26.03.2008 16:56:34 von Richard Dunne

----- Original Message -----
From: Evert Lammerts
Date: Wednesday, March 26, 2008 3:39 pm
Subject: Re: [PHP-DB] numeric string to single digit array

>
> > PHP is telling me that the resource I am using for
> mysql_fetch_assoc is invalid:
> >
> > $query ="Select answer from answers where studentID ='A123456789'";
> > $result = mysql_query($query,$connection);
> > $count=0;
> > while($row = mysql_fetch_assoc($result));
> > {
> > $count++;
> > }
> > echo $count;
> >
>
> Are you sure your database connection has been made?
>
> $connection = mysql_connect('localhost', 'mysql_user',
> 'mysql_password')
> or die(mysql_error());
> etc..
>
> If the connection is made alright, try:
>
> $query ="Select answer from answers where studentID ='A123456789'";
> $result = mysql_query($query,$connection) or die(mysql_error());
> $count=0;
> while($row = mysql_fetch_assoc($result));
> {
> $count++;
> }
> echo $count;
>
This is my code. The only error is at line 15 as I stated above.

1 2 DEFINE ("host","localhost");
3 DEFINE ("user","root");
4 DEFINE ("password","password");
5 DEFINE ("database","questions");
6
7 $connection=mysql_connect(host,user,password) or die ('Could not connect' .mysql_error() );
8
9 $dbConnect=mysql_select_db('questions',$connection);
10 if (!$dbConnect) {die ('Could not connect to database' . mysql_error() );}
11
12 $query ="Select answer from answers where studentID ='A123456789'";
13 $result = mysql_query($query,$connection);
14 $count=0;
15 while($row = mysql_fetch_assoc($result));
16 {
17 $count++;
18 }
19 echo $count;
20 ?>


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: numeric string to single digit array

am 26.03.2008 17:04:58 von Evert Lammerts

> This is my code. The only error is at line 15 as I stated above.
>
> 1 > 2 DEFINE ("host","localhost");
> 3 DEFINE ("user","root");
> 4 DEFINE ("password","password");
> 5 DEFINE ("database","questions");
> 6
> 7 $connection=mysql_connect(host,user,password) or die ('Could not connect' .mysql_error() );
> 8
> 9 $dbConnect=mysql_select_db('questions',$connection);
> 10 if (!$dbConnect) {die ('Could not connect to database' . mysql_error() );}
> 11
> 12 $query ="Select answer from answers where studentID ='A123456789'";
> 13 $result = mysql_query($query,$connection);
> 14 $count=0;
> 15 while($row = mysql_fetch_assoc($result));
> 16 {
> 17 $count++;
> 18 }
> 19 echo $count;
> 20 ?>
>
>

Turn line 13 into
$result = mysql_query($query) or die(mysql_error());
, so leave out the connection parameter and append the die() function,
and see what error that produces.

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: numeric string to single digit array

am 26.03.2008 17:12:24 von Richard Dunne

----- Original Message -----
From: Evert Lammerts
Date: Wednesday, March 26, 2008 4:04 pm
Subject: Re: [PHP-DB] numeric string to single digit array

>
> > This is my code. The only error is at line 15 as I stated above.
> >
> > 1 > > 2 DEFINE ("host","localhost");
> > 3 DEFINE ("user","root");
> > 4 DEFINE ("password","password");
> > 5 DEFINE ("database","questions");
> > 6
> > 7 $connection=mysql_connect(host,user,password) or die ('Could
> not connect' .mysql_error() );
> > 8
> > 9 $dbConnect=mysql_select_db('questions',$connection);
> > 10 if (!$dbConnect) {die ('Could not connect to database' .
> mysql_error() );}
> > 11
> > 12 $query ="Select answer from answers where studentID
> ='A123456789'";> 13 $result = mysql_query($query,$connection);
> > 14 $count=0;
> > 15 while($row = mysql_fetch_assoc($result));
> > 16 {
> > 17 $count++;
> > 18 }
> > 19 echo $count;
> > 20 ?>
> >
> >
>
> Turn line 13 into
> $result = mysql_query($query) or die(mysql_error());
> , so leave out the connection parameter and append the die()
> function,
> and see what error that produces.
>

OK. Tried that and count comes back as 1.


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: numeric string to single digit array

am 26.03.2008 17:20:21 von Jason Gerfen

Evert Lammerts wrote:
>
>> This is my code. The only error is at line 15 as I stated above.
>>
>> 1 >> 2 DEFINE ("host","localhost");
>> 3 DEFINE ("user","root");
>> 4 DEFINE ("password","password");
>> 5 DEFINE ("database","questions");
>> 6
>> 7 $connection=mysql_connect(host,user,password) or die ('Could not
>> connect' .mysql_error() );
>> 8
>> 9 $dbConnect=mysql_select_db('questions',$connection);
>> 10 if (!$dbConnect) {die ('Could not connect to database' .
>> mysql_error() );}
>> 11
>> 12 $query ="Select answer from answers where studentID ='A123456789'";
>> 13 $result = mysql_query($query,$connection);
>> 14 $count=0;
>> 15 while($row = mysql_fetch_assoc($result));
remove the semi-colon at the end of line 15
>> 16 {
>> 17 $count++;
>> 18 }
>> 19 echo $count;
>> 20 ?>
>>
>>
>
> Turn line 13 into
> $result = mysql_query($query) or die(mysql_error());
> , so leave out the connection parameter and append the die() function,
> and see what error that produces.
>


--
Jason Gerfen

"I practice my religion
while stepping on your
toes..."
~The Ditty Bops

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: numeric string to single digit array

am 26.03.2008 17:30:41 von Evert Lammerts

>
> OK. Tried that and count comes back as 1.
>

So your query returns only one record. Try

$query ="Select answer from answers";



--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: numeric string to single digit array

am 26.03.2008 17:42:07 von Jeremy McEntire

On Mar 26, 2008, at 12:30 PM, Evert Lammerts wrote:
>>
>> OK. Tried that and count comes back as 1.
>
> So your query returns only one record. Try
>
> $query ="Select answer from answers";



Why not do a var_dump() on $result to verify that it
is a mysql result resource and then verify the count
of rows with: mysql_num_rows($result);

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: numeric string to single digit array

am 26.03.2008 18:03:35 von Richard Dunne

----- Original Message -----
From: Evert Lammerts
Date: Wednesday, March 26, 2008 4:30 pm
Subject: Re: [PHP-DB] numeric string to single digit array

>
> >
> > OK. Tried that and count comes back as 1.
> >
>
> So your query returns only one record. Try
>
> $query ="Select answer from answers";
>
>
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

Tried that as well and got the same result.
I tried "Select count(answer) as total from answers where studentID='A123456789'";
from the CLI and got total = 2 as a result.


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: numeric string to single digit array

am 26.03.2008 18:22:53 von Evert Lammerts

> Tried that as well and got the same result.
> I tried "Select count(answer) as total from answers where studentID='A123456789'";
> from the CLI and got total = 2 as a result.
>

So, we got rid of the Invalid Resource error and we know that the
student id you use occurs in both rows in your table and that your query
works fine.

Did you get rid of the semicolon @ line 15 "while($row =
mysql_fetch_assoc($result));", as Jason suggested? Also, an:
error_reporting(E_ALL);
at the top of your code might help in backtracing.

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: numeric string to single digit array

am 26.03.2008 18:50:48 von Richard Dunne

----- Original Message -----
From: Evert Lammerts
Date: Wednesday, March 26, 2008 5:22 pm
Subject: Re: [PHP-DB] numeric string to single digit array

>
> > Tried that as well and got the same result.
> > I tried "Select count(answer) as total from answers where
> studentID='A123456789'";> from the CLI and got total = 2 as a
> result.
> >
>
> So, we got rid of the Invalid Resource error and we know that the
> student id you use occurs in both rows in your table and that your
> query
> works fine.
>
> Did you get rid of the semicolon @ line 15 "while($row =
> mysql_fetch_assoc($result));", as Jason suggested? Also, an:
> error_reporting(E_ALL);
> at the top of your code might help in backtracing.
>

The semi-colon is gone, although I didn't even notice it! I am using two different queries, one for count and the other to access the data itself. After running mysql_fetch_assoc, is foreach ok for accessing array members, or is there a more subtle approach?







--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: numeric string to single digit array

am 26.03.2008 19:26:44 von Evert Lammerts

> The semi-colon is gone, although I didn't even notice it! I am using two different queries, one for count and the other to access the data itself. After running mysql_fetch_assoc, is foreach ok for accessing array members, or is there a more subtle approach?
So it's working now? If it is, you won't need to do a separate COUNT()
query... you could use either a count / sizeof after fetching all the
data, or a mysql_num_rows call.

Foreach'll work fine, but in general I access the array's members by
their associated column name's. If you don't need the column names as
keys you can use mysql_fetch_row instead of mysql_fetch_assoc.

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Fwd: Re: numeric string to single digit array

am 26.03.2008 19:35:14 von Richard Dunne

--Boundary_(ID_Zr/Ct5qS/ckin4oswkn8tw)
Content-type: text/plain; charset=us-ascii
Content-transfer-encoding: 7BIT
Content-disposition: inline

Using this extract from http://ie.php.net/manual/en/control-structures.foreach.php
Amaroq
09-Mar-2008 06:40
Even if an array has only one value, it is still an array and foreach will run it.

$arr[] = "I'm an array.";

if(is_array($arr))
{
foreach($arr as $val)
{
echo $val;
}
}
?>

The above code outputs:
I'm an array.
-------------------------------------
So if I use:

$query = "Select answer from answers where studentID='A123456789'";
$result = mysql_query($query,$connection) or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
foreach($row as $answer)
{
echo $answer . "\n";
}
}
I thought I would be able to print out each array element, but I am not getting any output. Has anyone got a better idea?

--Boundary_(ID_Zr/Ct5qS/ckin4oswkn8tw)
Content-type: message/rfc822
Content-disposition: inline

Return-path:
Return-path:
Received: from mail1.o2.ie (mail1.o2.ie [62.40.33.135])
by msgstore1.o2.ie (Sun ONE Messaging Server)
with ESMTP id <0JYC00J6YLS6KW@msgstore1.o2.ie> for richarddunne1971@o2.ie;
Wed, 26 Mar 2008 17:26:30 +0000 (GMT)
Received: from fk-out-0910.google.com ([209.85.128.188])
by mail1.o2.ie (Sun ONE Messaging Server)
with ESMTP id <0JYC00MLQLQ79W00@mail1.o2.ie> for richarddunne1971@o2.ie
(ORCPT richarddunne1971@o2.ie); Wed, 26 Mar 2008 17:26:30 +0000 (GMT)
Received: by fk-out-0910.google.com with SMTP id f33so4631490fkf.7 for
; Wed, 26 Mar 2008 10:22:57 -0700 (PDT)
Received: by 10.82.115.8 with SMTP id n8mr531032buc.10.1206552176997; Wed,
26 Mar 2008 10:22:56 -0700 (PDT)
Received: from ?192.168.1.3? ( [88.209.234.153]) by mx.google.com with ESMTPS
id l19sm11400995fgb.0.2008.03.26.10.22.55 (version=TLSv1/SSLv3 cipher=RC4-MD5)
; Wed, 26 Mar 2008 10:22:56 -0700 (PDT)
Date: Wed, 26 Mar 2008 18:22:53 +0100
From: Evert Lammerts
Subject: Re: [PHP-DB] numeric string to single digit array
In-reply-to: <10c44010d267.10d26710c440@o2.ie>
To: Richard Dunne
Cc: Chris , php-db@lists.php.net
Message-id: <47EA866D.7040405@gmail.com>
MIME-version: 1.0
Content-type: text/plain; format=flowed; charset=ISO-8859-1
Content-transfer-encoding: 7BIT
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com;
s=beta;
h=domainkey-signature:received:received:message-id:date:from :user-agent:mime-version:to:cc:subject:references:in-reply-t o:content-type:content-transfer-encoding;
bh=x36wQqO3fizWAKyHRz4Vn6gb18CqRF0VGkjdRwIAVCI=;
b=PHKhX3hXs5ODalkKYPavk++2xTWp433jEl6pOP2CWIUAyu+oTCKGu2FMBN HNbayPrJbO9Cbcb+C/GWtV8KK1wl5rRpXHRdq/KC/6xbPasMT6N/nZ3qOb+e aYfYuzY88DZphM5XoGTmDy7Wmk9iZ9St7T+12Rz1Z9ee/QhJd4JsM=
DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta;
h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encodi ng;
b=VflQ83Fuxv3zME/XG5QngYkf7tP3/MYvHkefnZdjap3alZA/S86B1LB4H4 7nt5WHG66jzJQsXJro6Jyxz09nUEQKtZxuJ5n6gvRSOTJjPgI7P3ZaUd+hnD sCCSs+4g4HHXMTqEXGNy62TR1N0tm3hk/BSQFvcIIJ8tkNUqMJUa8=
X-Spam-test: False ; 0.0 / 8.0 ; BAYES_50
References: <10c44010d267.10d26710c440@o2.ie>
User-Agent: Thunderbird 2.0.0.12 (Windows/20080213)
Original-recipient: rfc822;richarddunne1971@o2.ie


> Tried that as well and got the same result.
> I tried "Select count(answer) as total from answers where studentID='A123456789'";
> from the CLI and got total = 2 as a result.
>

So, we got rid of the Invalid Resource error and we know that the
student id you use occurs in both rows in your table and that your query
works fine.

Did you get rid of the semicolon @ line 15 "while($row =
mysql_fetch_assoc($result));", as Jason suggested? Also, an:
error_reporting(E_ALL);
at the top of your code might help in backtracing.


--Boundary_(ID_Zr/Ct5qS/ckin4oswkn8tw)
Content-Type: text/plain; charset=us-ascii

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--Boundary_(ID_Zr/Ct5qS/ckin4oswkn8tw)--

Re: Re: numeric string to single digit array

am 26.03.2008 20:46:40 von jonllmsed

------=_Part_23959_8398723.1206560800464
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Not sure if this is relevant anymore, but...


> i.e. "1223123" into ['1','2,','2','3','1,'2','3'] ?

$num = "1223123";
$nums = array_filter(preg_split('//', $nums));


Or you can use this function.
It's probably better since the array_filter will probably get rid of any 0's
in the string.

function string_to_array($str) {
$arr = preg_split('//', $str);
array_shift($arr); // removing leading null from split
array_pop($arr); // remove training null from split
return $arr;
}


Or, if you just want the numbers from your student id (and you wanted only
numbers):

$num = "A123456789";
$nums = string_to_array(preg_replace("/[^0-9]+/", "", $nums));


- Jon L.



On Wed, Mar 26, 2008 at 1:35 PM, Richard Dunne
wrote:

> Using this extract from
> http://ie.php.net/manual/en/control-structures.foreach.php
> Amaroq
> 09-Mar-2008 06:40
> Even if an array has only one value, it is still an array and foreach will
> run it.
>
> > $arr[] = "I'm an array.";
>
> if(is_array($arr))
> {
> foreach($arr as $val)
> {
> echo $val;
> }
> }
> ?>
>
> The above code outputs:
> I'm an array.
> -------------------------------------
> So if I use:
>
> $query = "Select answer from answers where studentID='A123456789'";
> $result = mysql_query($query,$connection) or die(mysql_error());
> while($row = mysql_fetch_assoc($result))
> {
> foreach($row as $answer)
> {
> echo $answer . "\n";
> }
> }
> I thought I would be able to print out each array element, but I am not
> getting any output. Has anyone got a better idea?
>
>
> ---------- Forwarded message ----------
> From: Evert Lammerts
> To: Richard Dunne
> Date: Wed, 26 Mar 2008 18:22:53 +0100
> Subject: Re: [PHP-DB] numeric string to single digit array
>
> > Tried that as well and got the same result.
> > I tried "Select count(answer) as total from answers where
> studentID='A123456789'";
> > from the CLI and got total = 2 as a result.
> >
>
> So, we got rid of the Invalid Resource error and we know that the
> student id you use occurs in both rows in your table and that your query
> works fine.
>
> Did you get rid of the semicolon @ line 15 "while($row =
> mysql_fetch_assoc($result));", as Jason suggested? Also, an:
> error_reporting(E_ALL);
> at the top of your code might help in backtracing.
>
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

------=_Part_23959_8398723.1206560800464--

Fwd: Re: Re: numeric string to single digit array

am 26.03.2008 23:23:58 von Richard Dunne

--Boundary_(ID_vAQYgLiM1SwVxAXE2LmpjQ)
Content-type: text/plain; charset=us-ascii
Content-transfer-encoding: 7BIT
Content-disposition: inline

Just as a aside to the list, by replying with "reply all" I was sending a reply to the original sender twice, individually and via the list with Cc so apologies to those concerned. If you want to send a reply to the sender and the list as well, then I suggest just forwarding to the list. I found I was getting the same message twice and so I am guessing were/are others, and since it is helpful to clue the list on any little nuances this might alleviate double hits, for anyone concerned.

--Boundary_(ID_vAQYgLiM1SwVxAXE2LmpjQ)
Content-type: message/rfc822
Content-disposition: inline

Return-path:
Received: from mail2.o2.ie (mail2.o2.ie [62.40.36.103])
by msgstore1.o2.ie (Sun ONE Messaging Server)
with ESMTP id <0JYC006Z9SAVQW@msgstore1.o2.ie> for richarddunne1971@o2.ie;
Wed, 26 Mar 2008 19:47:19 +0000 (GMT)
Received: from rv-out-0910.google.com ([209.85.198.189])
by mail2.o2.ie (Sun ONE Messaging Server)
with ESMTP id <0JYC00E7MS9TKCA0@mail2.o2.ie> for richarddunne1971@o2.ie
(ORCPT richarddunne1971@o2.ie); Wed, 26 Mar 2008 19:47:19 +0000 (GMT)
Received: by rv-out-0910.google.com with SMTP id k20so2010478rvb.3 for
; Wed, 26 Mar 2008 12:46:41 -0700 (PDT)
Received: by 10.141.198.9 with SMTP id a9mr403134rvq.219.1206560800461; Wed,
26 Mar 2008 12:46:40 -0700 (PDT)
Received: by 10.141.37.12 with HTTP; Wed, 26 Mar 2008 12:46:40 -0700 (PDT)
Date: Wed, 26 Mar 2008 14:46:40 -0500
From: "Jon L."
Subject: Re: Re: [PHP-DB] numeric string to single digit array
In-reply-to: <11531e116d2a.116d2a11531e@o2.ie>
To: Richard Dunne
Cc: php-db@lists.php.net
Message-id: <5ad2dbbf0803261246r66eae681x8e60f02281857063@mail.gmail.com>
MIME-version: 1.0
Content-type: multipart/alternative;
boundary="Boundary_(ID_KCt6hjHsdQIzllLwKE06VA)"
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com;
s=beta;
h=domainkey-signature:received:received:message-id:date:from :to:subject:cc:in-reply-to:mime-version:content-type:referen ces;
bh=jawhWRnvSfB+Uv7vixBkkBK6OwG4aUWqmPOZggCSqTQ=;
b=gtzKnC5Ct07px2qp/31JCrtpiY/ERbmnfa9236fXz4lUIDQkRC6l0Tw9qM wciMbeNcY3mEBWQ4IbcrNNenWP3kWjbAlf/z4AMWS79mwewn/Xw1+ncEkqWf RSA9+Ai7kQIVJaEe7Xr7TvJ+BdAbikup+bLe20NuYd/jrdZKmvtAI=
DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta;
h=message-id:date:from:to:subject:cc:in-reply-to:mime-versio n:content-type:references;
b=eXHxUOkn5tJg7dApDNg234ZueorwcOEyINQjQUh0jj5M4JdK4X3Ov2sT9g Ttk+/0ad4R8jO48UPbiLMvgeLOJ4ay2UUPwzkmlSLG3DwgVeG4vjkgiGmdC1 3J8jbvQENg1OFdbiJnE7U+njPBDW+iiNrR3cBEgNPOA8I2lfyvlZg=
X-Spam-test: False ; 0.4 / 8.0 ; BAYES_50,HTML_30_40,HTML_MESSAGE,NO_RELAYS
References: <11531e116d2a.116d2a11531e@o2.ie>
Original-recipient: rfc822;richarddunne1971@o2.ie


--Boundary_(ID_KCt6hjHsdQIzllLwKE06VA)
Content-type: text/plain; charset=ISO-8859-1
Content-transfer-encoding: 7BIT
Content-disposition: inline

Not sure if this is relevant anymore, but...


> i.e. "1223123" into ['1','2,','2','3','1,'2','3'] ?

$num = "1223123";
$nums = array_filter(preg_split('//', $nums));


Or you can use this function.
It's probably better since the array_filter will probably get rid of any 0's
in the string.

function string_to_array($str) {
$arr = preg_split('//', $str);
array_shift($arr); // removing leading null from split
array_pop($arr); // remove training null from split
return $arr;
}


Or, if you just want the numbers from your student id (and you wanted only
numbers):

$num = "A123456789";
$nums = string_to_array(preg_replace("/[^0-9]+/", "", $nums));


- Jon L.



On Wed, Mar 26, 2008 at 1:35 PM, Richard Dunne
wrote:

> Using this extract from
> http://ie.php.net/manual/en/control-structures.foreach.php
> Amaroq
> 09-Mar-2008 06:40
> Even if an array has only one value, it is still an array and foreach will
> run it.
>
> > $arr[] = "I'm an array.";
>
> if(is_array($arr))
> {
> foreach($arr as $val)
> {
> echo $val;
> }
> }
> ?>
>
> The above code outputs:
> I'm an array.
> -------------------------------------
> So if I use:
>
> $query = "Select answer from answers where studentID='A123456789'";
> $result = mysql_query($query,$connection) or die(mysql_error());
> while($row = mysql_fetch_assoc($result))
> {
> foreach($row as $answer)
> {
> echo $answer . "\n";
> }
> }
> I thought I would be able to print out each array element, but I am not
> getting any output. Has anyone got a better idea?
>
>
> ---------- Forwarded message ----------
> From: Evert Lammerts
> To: Richard Dunne
> Date: Wed, 26 Mar 2008 18:22:53 +0100
> Subject: Re: [PHP-DB] numeric string to single digit array
>
> > Tried that as well and got the same result.
> > I tried "Select count(answer) as total from answers where
> studentID='A123456789'";
> > from the CLI and got total = 2 as a result.
> >
>
> So, we got rid of the Invalid Resource error and we know that the
> student id you use occurs in both rows in your table and that your query
> works fine.
>
> Did you get rid of the semicolon @ line 15 "while($row =
> mysql_fetch_assoc($result));", as Jason suggested? Also, an:
> error_reporting(E_ALL);
> at the top of your code might help in backtracing.
>
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

--Boundary_(ID_KCt6hjHsdQIzllLwKE06VA)
Content-type: text/html; charset=ISO-8859-1
Content-transfer-encoding: 7BIT
Content-disposition: inline

Not sure if this is relevant anymore, but...


> i.e. "1223123" into ['1','2,','2','3','1,'2','3'] ?

$num = "1223123";
$nums = array_filter(preg_split('//', $nums));



Or you can use this function.
It's probably better since the array_filter will probably get rid of any 0's in the string.

function string_to_array($str) {
    $arr = preg_split('//', $str);

    array_shift($arr); // removing leading null from split
    array_pop($arr); // remove training null from split
    return $arr;
}


Or, if you just want the numbers from your student id (and you wanted only numbers):


$num = "A123456789";

$nums = string_to_array(preg_replace("/[^0-9]+/", "", $nums));


- Jon L.



On Wed, Mar 26, 2008 at 1:35 PM, Richard Dunne <> wrote:

Using this extract from


Amaroq

09-Mar-2008 06:40

Even if an array has only one value, it is still an array and foreach will run it.



<?php

$arr[] = "I'm an array.";



if(is_array($arr))

{

   foreach($arr as $val)

   {

       echo $val;

   }

}

?>



The above code outputs:

I'm an array.

-------------------------------------

So if I use:



$query = "Select answer from answers where studentID='A123456789'";

$result = mysql_query($query,$connection) or die(mysql_error());

while($row = mysql_fetch_assoc($result))

{

       foreach($row as $answer)

       {

               echo $answer . "\n";

       }

}

I thought I would be able to print out each array element, but I am not getting any output.  Has anyone got a better idea?



---------- Forwarded message ----------
From: Evert Lammerts <>

Date: Wed, 26 Mar 2008 18:22:53 +0100
Subject: Re: [PHP-DB] numeric string to single digit array


> Tried that as well and got the same result.

> I tried "Select count(answer) as total from answers where studentID='A123456789'";

> from the CLI and got total = 2 as a result.

>



So, we got rid of the Invalid Resource error and we know that the

student id you use occurs in both rows in your table and that your query

works fine.



Did you get rid of the semicolon @ line 15 "while($row =

mysql_fetch_assoc($result));", as Jason suggested? Also, an:

error_reporting(E_ALL);

at the top of your code might help in backtracing.




--

PHP Database Mailing List ()

To unsubscribe, visit:



--Boundary_(ID_KCt6hjHsdQIzllLwKE06VA)--


--Boundary_(ID_vAQYgLiM1SwVxAXE2LmpjQ)
Content-Type: text/plain; charset=us-ascii

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--Boundary_(ID_vAQYgLiM1SwVxAXE2LmpjQ)--

Fwd: Re: numeric string to single digit array

am 27.03.2008 00:15:38 von Richard Dunne

--Boundary_(ID_K8qQXFravtjIRXH3yAWKmQ)
Content-type: text/plain; charset=us-ascii
Content-transfer-encoding: 7BIT
Content-disposition: inline

I did var_dump on the result resource and I got resource(5) of type (mysql result).

--Boundary_(ID_K8qQXFravtjIRXH3yAWKmQ)
Content-type: message/rfc822
Content-disposition: inline

Return-path:
Return-path:
Received: from mail1.o2.ie (mail1.o2.ie [62.40.33.135])
by msgstore1.o2.ie (Sun ONE Messaging Server)
with ESMTP id <0JYC009JLJQSH8@msgstore1.o2.ie> for richarddunne1971@o2.ie;
Wed, 26 Mar 2008 16:42:28 +0000 (GMT)
Received: from an-out-0708.google.com ([209.85.132.242])
by mail1.o2.ie (Sun ONE Messaging Server)
with ESMTP id <0JYC009WWJQCRN60@mail1.o2.ie> for richarddunne1971@o2.ie
(ORCPT richarddunne1971@o2.ie); Wed, 26 Mar 2008 16:42:28 +0000 (GMT)
Received: by an-out-0708.google.com with SMTP id b20so10324ana.14 for
; Wed, 26 Mar 2008 09:42:12 -0700 (PDT)
Received: by 10.100.110.16 with SMTP id i16mr316921anc.109.1206549732066; Wed,
26 Mar 2008 09:42:12 -0700 (PDT)
Received: from jmcentire.plaza.corp ( [74.94.38.129])
by mx.google.com with ESMTPS id 38sm14055420agd.38.2008.03.26.09.42.08
(version=TLSv1/SSLv3 cipher=OTHER); Wed, 26 Mar 2008 09:42:09 -0700 (PDT)
Date: Wed, 26 Mar 2008 12:42:07 -0400
From: Jeremy Mcentire
Subject: Re: [PHP-DB] numeric string to single digit array
In-reply-to: <47EA7A31.9060606@gmail.com>
To: Evert Lammerts
Cc: Richard Dunne , Chris ,
php-db@lists.php.net
Message-id:
MIME-version: 1.0 (Apple Message framework v919.2)
X-Mailer: Apple Mail (2.919.2)
Content-type: text/plain; format=flowed; charset=US-ASCII
Content-transfer-encoding: 7BIT
References: <10764c10978b.10978b10764c@o2.ie> <47EA7A31.9060606@gmail.com>
Original-recipient: rfc822;richarddunne1971@o2.ie

On Mar 26, 2008, at 12:30 PM, Evert Lammerts wrote:
>>
>> OK. Tried that and count comes back as 1.
>
> So your query returns only one record. Try
>
> $query ="Select answer from answers";



Why not do a var_dump() on $result to verify that it
is a mysql result resource and then verify the count
of rows with: mysql_num_rows($result);


--Boundary_(ID_K8qQXFravtjIRXH3yAWKmQ)
Content-Type: text/plain; charset=us-ascii

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--Boundary_(ID_K8qQXFravtjIRXH3yAWKmQ)--

Re: Fwd: Re: numeric string to single digit array

am 27.03.2008 00:33:00 von dmagick

Richard Dunne wrote:
> Using this extract from http://ie.php.net/manual/en/control-structures.foreach.php
> Amaroq
> 09-Mar-2008 06:40
> Even if an array has only one value, it is still an array and foreach will run it.
>
> > $arr[] = "I'm an array.";
>
> if(is_array($arr))
> {
> foreach($arr as $val)
> {
> echo $val;
> }
> }
> ?>
>
> The above code outputs:
> I'm an array.
> -------------------------------------
> So if I use:
>
> $query = "Select answer from answers where studentID='A123456789'";
> $result = mysql_query($query,$connection) or die(mysql_error());
> while($row = mysql_fetch_assoc($result))
> {
> foreach($row as $answer)
> {
> echo $answer . "\n";
> }
> }
> I thought I would be able to print out each array element, but I am not getting any output. Has anyone got a better idea?

Instead of using a foreach inside the while loop, just access the array
key directly. The name of the key is the name of the column (or alias)
from your query.

It should be simply:

$query = "Select answer from answers where studentID='A123456789'";
$result = mysql_query($query,$connection) or die(mysql_error());
while ($row = mysql_fetch_assoc($result))
{
print_r($row);
echo "Answer is ", $row['answer'], "\n";
}

If a result set has no results (ie your query does not return anything -
there are no matching rows), then php won't actually get into the while
loop.

You can see how many rows are returned by using:

$query = "Select answer from answers where studentID='A123456789'";
$result = mysql_query($query,$connection) or die(mysql_error());
$number_of_results = mysql_num_rows($result);


Though I suggest only doing this for small result sets - otherwise mysql
has to actually process all of the query results which are then stored
in memory on the server.

--
Postgresql & php tutorials
http://www.designmagick.com/

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php