[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Trying to get the last value in mysql fetch arry

Posted on 2008-11-17
6
Medium Priority
?
958 Views
Last Modified: 2012-08-14
Hello,
I'm new to php and learning from places like this, so please be patient.

I''m working on an application where a list of items are displayed on the page, ordered by a sequence number (seq).  I have up/down buttons on each record so that users can change the order of the items.  But before I get to that code, I first want to check for the first record and hide the UP button, and then check the last record and hide the DOWN button.  

Below is my attempt to start by finding the first and last records. First I created a new array to contain all my sequence numbers at once (otherwise with PHP I can only fetch one row at a time, right?)   The Min function works nicely to find the first value, but the END function does not seem to be working.  I've been trying to follow advise given at http://www.php.net/end, but I'm not getting anywhere - suspect there is something basic I don't understand?

Sure would appreciate some expert help!
Thx.
<?php
 
$queryAllSeqs = ("SELECT * FROM selections WHERE collectID=3 ORDER BY seq ASC");
mysql_select_db($database_connWebCMS, $connWebCMS);
$rsAllSeqs = mysql_query($queryAllSeqs, $connWebCMS) or die(mysql_error());
$number_of_recs = mysql_num_rows($rsAllSeqs);
echo "number_of_recs is this: $number_of_recs <br />";
 
$new_array = array();
while($row = mysql_fetch_assoc($rsAllSeqs)){
  $new_array[] = $row["seq"];
}
mysql_free_result($rsAllSeqs);
 
/* this shows my new array 1, 2, 3
foreach ($new_array as $seq){
   echo $seq . "<br>";
}*/
 
$start = min($new_array);
echo "start is this: $start <br />";
//reset($new_array);
$end = end($new_array);
echo "last is this: $last <br />";
//reset($new_array);
?>

Open in new window

0
Comment
Question by:samiam80304
  • 3
  • 3
6 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 200 total points
ID: 22977301
see attached code
<?php
$queryAllSeqs = ("SELECT * FROM selections WHERE collectID=3 ORDER BY seq ASC");
mysql_select_db($database_connWebCMS, $connWebCMS);
$rsAllSeqs = mysql_query($queryAllSeqs, $connWebCMS) or die(mysql_error());
$number_of_recs = mysql_num_rows($rsAllSeqs);
echo "number_of_recs is this: $number_of_recs"; 
//to obtain first seq, extract first row
$row = mysql_fetch_assoc($rsAllSeqs);
$first =$row['seq']; 
//then advance pointer to last record;
mysql_data_seek($rsAllSeqs,($number_of_recs-1)); 
//extract last record
$row = mysql_fetch_assoc($rsAllSeqs);
$last = $row['seq']; 
//now you can reset the pointer to the beginning of the recordset
//NOTE: if you want to skip to the second record use mysql_data_seek($rsAllSeqs,1);
mysql_data_seek($rsAllSeqs,0); 
 
/* this shows my new array 1, 2, 3
foreach ($new_array as $seq){
   echo $seq . "<br>";
}*/
  
echo "start is this: $start ";
//reset($new_array); 
//here you can iterate over all the results if you wish
while( $row = mysql_fetch_assoc($rsAllSeqs) )
{
	echo $row['seq']."<br />";
}
echo "last is this: $last ";
//reset($new_array); 
mysql_free_result($rsAllSeqs);
?>

Open in new window

0
 

Author Closing Comment

by:samiam80304
ID: 31517505
Thank you for the quick and functional reply.  You made my day.
0
 

Author Comment

by:samiam80304
ID: 22977587
Hi again,
Solution is perfect, but I wonder about the PHP END function.  Was I using it wrong or does it not work as advertised in documentation?
Thanks again!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 82

Expert Comment

by:hielo
ID: 22977669
>>Was I using it wrong
Yes

>>or does it not work as advertised in documentation?
It does work. You are either not understanding the function OR not understanding what mysql_query does.

From the manual:
"end  Set the internal pointer of an array to its last element"

Clearly it is meant to work on an array. When you do:
$result = mysql_query("SELECT id, firstname FROM Table");

$result is NOT an array. It IS a resource - an identifier that points to the result set. So you cannot use end() with $result because $result is NOT an array.

If you do:
$result = mysql_query("SELECT id, firstname FROM Table");
$row = mysql_fetch_assoc($result);

then $row IS an array, but it contains only the first row. If your SELECT returned this:
1 john
2 mary
3 sally
4 pete

when you do this the first time:
$row = mysql_fetch_assoc($result);
then $row is an array that contains
1 john

so end($row) would give you john. What you need to do was to advance $result not $row.
0
 

Author Comment

by:samiam80304
ID: 22977778
excellent - that explanation helps alot.  Thanks agian.
0
 
LVL 82

Expert Comment

by:hielo
ID: 22978046
you are welcome
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month20 days, 3 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question