jim.davidson Posted June 6, 2007 Share Posted June 6, 2007 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 More sharing options...
taith Posted June 6, 2007 Share Posted June 6, 2007 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); Link to comment https://forums.phpfreaks.com/topic/54468-solved-array-from-recordset-problem/#findComment-269398 Share on other sites More sharing options...
eric1235711 Posted June 6, 2007 Share Posted June 6, 2007 you're doing an mysql_fetch_assoc just after the mysql_query line: It's reading the first reccord and moving the pointer to the next reccord. Link to comment https://forums.phpfreaks.com/topic/54468-solved-array-from-recordset-problem/#findComment-269410 Share on other sites More sharing options...
jim.davidson Posted June 6, 2007 Author Share Posted June 6, 2007 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+ Link to comment https://forums.phpfreaks.com/topic/54468-solved-array-from-recordset-problem/#findComment-269415 Share on other sites More sharing options...
Barand Posted June 6, 2007 Share Posted June 6, 2007 www.php.net/mysql_data_seek mysql_data_seek(0) returns pointer to first row Link to comment https://forums.phpfreaks.com/topic/54468-solved-array-from-recordset-problem/#findComment-269430 Share on other sites More sharing options...
per1os Posted June 6, 2007 Share Posted June 6, 2007 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. Link to comment https://forums.phpfreaks.com/topic/54468-solved-array-from-recordset-problem/#findComment-269434 Share on other sites More sharing options...
jim.davidson Posted June 6, 2007 Author Share Posted June 6, 2007 Thanks one and all for the help, I'm sure I'll have more questions. This is a great forum!! Link to comment https://forums.phpfreaks.com/topic/54468-solved-array-from-recordset-problem/#findComment-269448 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.