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
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);

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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