Bulvak
Bulvak

Reputation: 1804

how do I choose selected rows in MySQL with PHP?

MySQL

How can I make those dates line up with start date and end date or selectively echo the dates rather than have the while loop echo them?

For example if I can echo "Start Date" then the actual date then the "end date" and then the date.

Basically how may I echo the dates from mySQL selectively rather than using the loop to echo them?

for example if I want to echo row 2 can I do echo $row'date' (to echo the 2nd row)

On HTML Page PHP CODE:

Start Date:

End Date:

<?php


mysql_connect("host","username","password") or die(mysql_error());
mysql_select_db("UserLogins") or die(mysql_error());

$query = "SELECT * FROM Date1"; 

$result = mysql_query($query) or die(mysql_error());


while($row = mysql_fetch_array($result)){
    echo "<li>".$row['date']."</li>";
    echo "<br />";
}

?>

Upvotes: 0

Views: 229

Answers (4)

nobody
nobody

Reputation: 1

err...

<?php


mysql_connect("host","username","password") or die(mysql_error());
mysql_select_db("UserLogins") or die(mysql_error());

$query = "SELECT * FROM Date1"; 

$result = mysql_query($query) or die(mysql_error());


echo "<table border=\"0\">\n<tr><th>Start Date</th><th>End Date</th></tr>";

$startRow = true;
while($row = mysql_fetch_array($result)) {
    if($startRow) {
        echo "\n<tr><td>";
    } else {
        echo "<td>";
    }

    echo $row['date'];

    if($startRow) {
        echo "</td>";
    } else {
        echo "</td></tr>";         
    }

    $startRow = !$startRow;
}

if(!$startRow) {
    echo "</tr>";
}

echo "\n</table>\n";

?>

???

Upvotes: 0

nobody
nobody

Reputation: 1

also...

<?php


mysql_connect("host","username","password") or die(mysql_error());
mysql_select_db("UserLogins") or die(mysql_error());

$query = "SELECT min(date) as startdate, max(date) as enddate FROM Date1"; 

$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);
if($row) {
    echo "Start Date: " . $row['startdate'] . "<br />\n";
    echo "End Date: " . $row['enddate'] . "<br />\n";
}
?>

???

Upvotes: 0

Cᴏʀʏ
Cᴏʀʏ

Reputation: 107626

It sounds like to you need to constrain your query results. If you only want certain records from your database then you should filter out the ones you don't want right in the SQL.

$query = "SELECT * FROM Date1 WHERE date >= ? AND date <= ?"; 

If you use prepared statements you can plug in values for the two questions marks.

Upvotes: 2

Peter
Peter

Reputation: 16943

first solution

$arr = Array();
while($row = mysql_fetch_array($result)){
    $arr[] = $row;
}

echo $arr[0]['date']; // first
echo $arr[3]['date'];
echo $arr[7]['date'];
echo $arr[count($arr)-1]['date']; // last

second solution

$first = @mysql_fetch_assoc(mysql_query("SELECT * FROM Date1 ORDER BY date ASC LIMIT1")); 
$last = @mysql_fetch_assoc(mysql_query("SELECT * FROM Date1 ORDER BY date DESC LIMIT1")); 
echo $first['date'].'<br>';
echo $last['date'];

if you want to use second solution remember to put index on date column

Upvotes: 1

Related Questions