homepage Welcome to WebmasterWorld Guest from 23.23.12.202
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
I have a mysql fetch array, but need a "real" array
ensute

5+ Year Member



 
Msg#: 4434680 posted 6:44 am on Mar 29, 2012 (gmt 0)

I need to set a variable whish the value is the last one from an given array.
So, I figured I would use PHP's "end" function.

Unfortunately my array is a function returning an mysql_fetch_array.
You know, the famous

$result = mysql_query("SELECT id_product FROM product WHERE id_category = '2' AND active = '1' ");

while($row = mysql_fetch_array($result ) ) {
echo $row['id_product'];
}).


This dont work with the "end" function. I need a real array. Like (melbourne, tokyo, sidney, washington, berlin) with or without keys.
I guess there is a workarround. Can someone point me in the right direction. Thank you a lot.

 

enigma1

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4434680 posted 9:17 am on Mar 29, 2012 (gmt 0)

After the mysql_query statement try something like this:


$last_result = $result_array = array();
// First check if the query returned rows
if( mysql_num_rows($result) ) {
while( $result_array[] = mysql_fetch_array($result ) );
array_pop($result_array);
// Get last row
$last_entry = $result_array[count($result_array)-1];
}

if( !empty($last_entry) ) {
// Last row in $last_entry
} else {
// Handle error
}


It loads the mysql returned rows into an array and fetches the last result in $last_entry or returns an empty array if no rows returned. If you need to process all the rows returned for other purposes they're loaded in the $result_array

eelixduppy

WebmasterWorld Senior Member eelixduppy us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4434680 posted 12:14 pm on Mar 29, 2012 (gmt 0)

Without context into what you are trying to do it is somewhat difficult to see what the best solution is in this case; however, solving this problem by itself seems to me that it should be accomplished via modifying your sql to only produce the row that you want. This can perhaps be accomplished by correctly limiting and ordering the result set.

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4434680 posted 3:36 pm on Mar 29, 2012 (gmt 0)

^ ^ Right . . . is it the last item in this result set you want? It's more efficient to select only what you want. One way,

$query = "SELECT id_product FROM product WHERE id_category = '2' AND active = '1' order by id_product desc limit 1";

$result = mysql_query($query);

In which case, it's more accurate to use if, not while . . .

if ($row=mysql_fetch_array($result)) {
//
}

penders

WebmasterWorld Senior Member penders us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



 
Msg#: 4434680 posted 4:04 pm on Mar 29, 2012 (gmt 0)

I need a real array.


Just to add... it is a real array, but it is an array of columns, not an array of rows.

ensute

5+ Year Member



 
Msg#: 4434680 posted 6:28 pm on Mar 29, 2012 (gmt 0)

oh thank you so much for your help. The only point why i figured to need a real array was to manipulate my records to get the last one. But since there is a another way... Thank you.
i want to build a link to my last addition in my database (in fact the last product in the shop).

@penders
Still dont know how to transform an array fetched from an database into a real array. With mysql_fetch_array you can just put the last record in a variable if i'm right. But there must be a way to put every record from an while loop in an array. Without guarantee...

eelixduppy

WebmasterWorld Senior Member eelixduppy us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4434680 posted 6:51 pm on Mar 29, 2012 (gmt 0)

i want to build a link to my last addition in my database


As already mentioned, you should order by the insert timestamp and limit to 1. This would look something like what rocknbil suggested:


SELECT id_product FROM product WHERE id_category = '2' AND active = '1' ORDER BY insert_timestamp DESC LIMIT 1


Where insert_timestamp is a column for the timestamp that the row was entered into the database.

With mysql_fetch_array you can just put the last record in a variable if i'm right.


mysql_fetch_array/mysql_fetch_row are only getting the current row, hence the reason why you have the while loop. With each loop's iteration you are advancing to the next row, fetching that row's data into an array, and processing it. If you wanted all of the data into a multi-dimension array, you could move through it and with each iteration add it to an array, much like enigma1's solution.

cffrost2

5+ Year Member



 
Msg#: 4434680 posted 9:11 pm on Mar 29, 2012 (gmt 0)

I' guessing at this but I'm thinking you want to pull all the rows, insert them into an array and then be able to access all of them, including the last record?

I typed this on the fly, mind any errors;

$query = "SELECT id_product FROM product WHERE id_category = '2' AND active = '1' order by id_product asc";

$result = mysql_query($query);

$data = array();

while($row = mysql_fetch_array($result))
{
$i=1;//start the count
$count = mysql_num_rows($result);//count the rows
foreach($row as $id)
{
$data['row_'.$i] = $id['id_product'];//insert each record in the $data array
$i++;//move to the next sequential number
}
}

//this would create an array such as
$data['row_1'] = '1234';//record 1
$data['row_2'] = '1235';//record 2
$data['row_3'] = '1236';//record 3

//to access the last record use the $count var. Say $count = 5:
echo 'The last product id in the system is: '.$data['row_'.$count];
//to show all the records:
$new_i = 1;
while($count > 1)
{
echo 'Here is a list of all the prod id\'s in the system: '.$data['row_'.$new_i].'<br />';
$new_i++;
}


Hope this helps. There are others on here with much better skills than I that may could clean that code up a bit but it does what I thnk you may be after.

cffrost2

5+ Year Member



 
Msg#: 4434680 posted 9:17 pm on Mar 29, 2012 (gmt 0)

My bad. I think I coded the last while loop wrong. That wouldn't work to show all the records, but this, I believe would:


//to show all the records:
for($new_i=1; $new_i<=$count; $new_i++)
{
echo 'Here is a list of all the prod id\'s in the system: '.$data['row_'.$new_i];
}


Sorry bout that!

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4434680 posted 4:58 pm on Mar 30, 2012 (gmt 0)

<OT tangent>
There is no reason to count into a fetched array in this fashion. Think about it: while there are rows, output. Once there are no more rows, no more output.

echo "Here is a list of all the prod id's in the system:<br>\n";
while($row = mysql_fetch_array($result)) {
echo $row['id'] . "<br>\n";
}
</OT tangent>

But if you DO want to do this,
But there must be a way to put every record from an while loop in an array.


you can. Simply change the above example

echo $row['id'] . "<br>\n";

to
array_push($row['id'],$arrayname);

or similar. You can even do associative:

$arrayname[$row['id']] = $row['email_address'];

will give you

1 => 'there@there.com',
2 => 'this@that.com,
3 => 'me@my.com'

echo $arrayname['3'];

Be sure this is what you want - one of the sources of PHP memory-hogginess (IMO/IME) is the blatant use of sometimes ginormous arrays, which take up memory, when there's a more efficient way to to the same thing.

ensute

5+ Year Member



 
Msg#: 4434680 posted 9:00 am on Apr 11, 2012 (gmt 0)

Thank you all for your replies. It works perfect

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved