Re: [PHP-DB] numeric string to single digit array

2008-03-26 Thread Richard Dunne
- Original Message -
From: Chris [EMAIL PROTECTED]
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: [PHP-DB] numeric string to single digit array

2008-03-26 Thread 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: [PHP-DB] numeric string to single digit array

2008-03-26 Thread Richard Dunne


- Original Message -
From: Evert Lammerts [EMAIL PROTECTED]
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: [PHP-DB] numeric string to single digit array

2008-03-26 Thread Richard Dunne


- Original Message -
From: Evert Lammerts [EMAIL PROTECTED]
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 ?PHP
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: [PHP-DB] numeric string to single digit array

2008-03-26 Thread 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: [PHP-DB] numeric string to single digit array

2008-03-26 Thread Evert Lammerts



This is my code.  The only error is at line 15 as I stated above.

1 ?PHP
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: [PHP-DB] numeric string to single digit array

2008-03-26 Thread Jason Gerfen
Evert Lammerts wrote:
 
 This is my code.  The only error is at line 15 as I stated above.

 1 ?PHP
 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: [PHP-DB] numeric string to single digit array

2008-03-26 Thread Richard Dunne


- Original Message -
From: Evert Lammerts [EMAIL PROTECTED]
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 ?PHP
  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: [PHP-DB] numeric string to single digit array

2008-03-26 Thread 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: [PHP-DB] numeric string to single digit array

2008-03-26 Thread 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: [PHP-DB] numeric string to single digit array

2008-03-26 Thread Richard Dunne


- Original Message -
From: Evert Lammerts [EMAIL PROTECTED]
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: [PHP-DB] numeric string to single digit array

2008-03-26 Thread 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: [PHP-DB] numeric string to single digit array

2008-03-26 Thread Richard Dunne


- Original Message -
From: Evert Lammerts [EMAIL PROTECTED]
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



Fwd: Re: [PHP-DB] numeric string to single digit array

2008-03-26 Thread Richard Dunne
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.

?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?
---BeginMessage---


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.

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

Re: Re: [PHP-DB] numeric string to single digit array

2008-03-26 Thread Jon L.
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 [EMAIL PROTECTED]
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.

 ?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 [EMAIL PROTECTED]
 To: Richard Dunne [EMAIL PROTECTED]
 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



Re: Fwd: Re: [PHP-DB] numeric string to single digit array

2008-03-26 Thread Chris

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.


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


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



Fwd: Re: [PHP-DB] numeric string to single digit array

2008-03-26 Thread Richard Dunne
I did  var_dump on the result resource and I got resource(5) of type (mysql 
result).  
---BeginMessage---

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

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

Re: [PHP-DB] numeric string to single digit array

2008-03-25 Thread Chris

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: [PHP-DB] numeric string to single digit array

2008-03-25 Thread Chris


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: [PHP-DB] numeric string to single digit array

2008-03-25 Thread 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 [EMAIL PROTECTED]
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: [PHP-DB] numeric string to single digit array

2008-03-25 Thread Chris

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