eurob Posted August 31, 2006 Share Posted August 31, 2006 [code]$date = '07012006';$sql = "SELECT subject FROM cal where date='$date'";$result=mysql_query($sql);$row = mysql_fetch_array($result);echo sizeof($row); -------------> shows 2 rowsecho $row[0]; echo $row[1]; -------> error:'Undefined offset: 1' why ?[/code]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 ? Quote Link to comment https://forums.phpfreaks.com/topic/19271-simple-question/ Share on other sites More sharing options...
sasa Posted August 31, 2006 Share Posted August 31, 2006 you have $row[0] and $row['subject']try print_r($row); Quote Link to comment https://forums.phpfreaks.com/topic/19271-simple-question/#findComment-83558 Share on other sites More sharing options...
wildteen88 Posted August 31, 2006 Share Posted August 31, 2006 The result returned may have return two sets of results, however you are only echoing the first set.This code:[code]$row = mysql_fetch_array($result);echo sizeof($row); -------------> shows 2 rowsecho $row[0]; echo $row[1]; -------> error:'Undefined offset: 1' why ?[/code]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[code]while($row = mysql_fetch_array($result))echo $row[0] . '<br /><br />'; }[/code]Now it'll return the all the results from the queryAlso the reason why[code]echo sizeof($row); [/code]returns two is because mysql_fetch_array returns the results into different arrays, numrical indecies ($row[0]) or associative indices ($row['submit']) Quote Link to comment https://forums.phpfreaks.com/topic/19271-simple-question/#findComment-83560 Share on other sites More sharing options...
redarrow Posted August 31, 2006 Share Posted August 31, 2006 might be wrong this code not sure.[code]<?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>";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/19271-simple-question/#findComment-83561 Share on other sites More sharing options...
eurob Posted September 1, 2006 Author Share Posted September 1, 2006 Thank you so much for the explanation, I worked it out. Quote Link to comment https://forums.phpfreaks.com/topic/19271-simple-question/#findComment-84056 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.