Jump to content

[SOLVED] Array from recordset problem


jim.davidson

Recommended Posts

I'm using php, dreamweaver 8, and mySQL ver 4.1.21

 

I'm teaching myself php, today's lesson, building arrays from recordsets.

 

Why don't I get the first record form from my recordset in my array

 

I've tried setting $i to equal 0,1,-1...but still can't get first record

 

 

mysql_select_db($database_imcrecycle, $imcrecycle);

$query_getStates = "SELECT * FROM states ORDER BY states.country, states.state_name";

$getStates = mysql_query($query_getStates, $imcrecycle) or die(mysql_error());

$row_getStates = mysql_fetch_assoc($getStates);

$totalRows_getStates = mysql_num_rows($getStates);

 

$arrayStates = array();

 

for ($i=1; $i <= $totalRows_getStates; $i++){

  $row = mysql_fetch_array($getStates);

  $arrayStates[$row['state_id']] = $row['state_name'];

}

 

 

print_r($arrayStates);

 

Link to comment
https://forums.phpfreaks.com/topic/54468-solved-array-from-recordset-problem/
Share on other sites

:D make it easy on yourself... lol

 

mysql_select_db($database_imcrecycle, $imcrecycle);
$getStates=mysql_query("SELECT * FROM states ORDER BY states.country, states.state_name", $imcrecycle) or die(mysql_error());

$arrayStates = array();

while($row = mysql_fetch_array($getStates)){
$arrayStates[$row['state_id']] = $row['state_name'];
}

print_r($arrayStates);

I see, that explains it, and the first reply works also. But now with your reply I know why.  Now for the second step, how would I move the pointer back?

 

Like I said I'm learning...right now I'd grade myself about a C+

 

Just a C+ huh? I think I am more of a C++ type guy ^.-

 

mysql_select_db($database_imcrecycle, $imcrecycle);
$query_getStates = "SELECT * FROM states ORDER BY states.country, states.state_name";
$getStates = mysql_query($query_getStates, $imcrecycle) or die(mysql_error());
//$row_getStates = mysql_fetch_assoc($getStates);
$totalRows_getStates = mysql_num_rows($getStates);

$arrayStates = array();

for ($i=0; $i < $totalRows_getStates; $i++){
    $row = mysql_fetch_array($getStates);
    $arrayStates[$row['state_id']] = $row['state_name'];
}


print_r($arrayStates);

 

That is an implementation of that with your original code.

 

As for moving the pointer "back" check into mysql_data_seek. Just do not call the first row outside of the loop and you will be fine.

Archived

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

×
×
  • 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.