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