Jump to content

Recommended Posts

[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 rows
echo $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 ?



Link to comment
https://forums.phpfreaks.com/topic/19271-simple-question/
Share on other sites

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 rows
echo $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 query

Also 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'])
Link to comment
https://forums.phpfreaks.com/topic/19271-simple-question/#findComment-83560
Share on other sites

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]
Link to comment
https://forums.phpfreaks.com/topic/19271-simple-question/#findComment-83561
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.