Jump to content

Archived

This topic is now archived and is closed to further replies.

eurob

simple question

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 ?



Share this post


Link to post
Share on other sites
you have $row[0] and $row['subject']
try print_r($row);

Share this post


Link to post
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'])

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

×

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.