Jump to content


Photo

simple question


  • Please log in to reply
4 replies to this topic

#1 eurob

eurob
  • Members
  • Pip
  • Newbie
  • 9 posts
  • LocationOhio

Posted 31 August 2006 - 04:05 PM

$date = '07012006';
$sql = "SELECT subject FROM cal where date='$date'";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
echo sizeof($row);               -------------> shows 2 rows
echo $row[0]; 
echo $row[1];    -------> error:'Undefined offset: 1'   why ?


When I execute this sql in mysql I get two records back.
When I put in my php page echo $row[1] i get an error msg 'undefined offset'. Don't understand this because the array contains 2 rows.
Anyone has an idea ?





#2 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 31 August 2006 - 04:09 PM

you have $row[0] and $row['subject']
try print_r($row);

#3 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 31 August 2006 - 04:10 PM

The result returned may have return two sets of results, however you are only echoing the first set.

This code:
$row = mysql_fetch_array($result);
echo sizeof($row);               -------------> shows 2 rows
echo $row[0]; 
echo $row[1];    -------> error:'Undefined offset: 1'   why ?
Whill only return the first row found by the query. if you want it to return all the rows you'll want to use a while loop
while($row = mysql_fetch_array($result))
echo $row[0] . '<br /><br />'; 
}
Now it'll return the all the results from the query

Also the reason why
echo sizeof($row); 
returns two is because mysql_fetch_array returns the results into different arrays, numrical indecies ($row[0]) or associative indices ($row['submit'])

#4 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 31 August 2006 - 04:12 PM

might be wrong this code not sure.

<?php

//database connection

$sql = "SELECT subject FROM cal where date='$date'";

$result=mysql_query($sql);

while($row= mysql_fetch_assoc($result)){

$date=$row['date'];

$result= date("d-m-y",$date);

echo "<br>$result<br>";
}
?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#5 eurob

eurob
  • Members
  • Pip
  • Newbie
  • 9 posts
  • LocationOhio

Posted 01 September 2006 - 02:54 PM

Thank you so much for the explanation, I worked it out.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users