simon551 Posted June 17, 2009 Share Posted June 17, 2009 Hi. I need to be able to pull out two rows of data from my query and then manipulate that data separately in a page. I would like to do something like this: <?php mysql_select_db($database_conn_org, $conn_org); $query="SELECT etDetId FROM et_details "; $rs = mysql_query($query, $conn_org) or die(mysql_error()); $row = mysql_fetch_array($rs); $totalRows = mysql_num_rows($rs); while ($row < $totalRows) { //transaction information $id1= $row[0]['etDetId']; $id2= $row[1]['etDetId']; } echo $id1.'<br />'.$id2; I'm not really sure what I'm doing with the array in the $row[0]['etDetId'], that was a total guess and it didn't work. The query I'm using will return a maximum of 2 rows but could just be one. Let me know if I'm not explaining my problem well. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/162643-solved-get-2-rows-of-data-separately/ Share on other sites More sharing options...
ldougherty Posted June 17, 2009 Share Posted June 17, 2009 I'm not sure I understand your question but here is an example of how you would loop through the results of a query. $result=mysql_query("SELECT * FROM mytable"); while ($row = mysql_fetch_array($result)) { $name = $row[name]; $email = $row; echo "name is $name and email is $email<br>"; } This assumes you have fields for name and email and would assign a value and output each through the loop until it reaches the end of the database. Quote Link to comment https://forums.phpfreaks.com/topic/162643-solved-get-2-rows-of-data-separately/#findComment-858380 Share on other sites More sharing options...
simon551 Posted June 17, 2009 Author Share Posted June 17, 2009 No. I understand that part. I'm trying to modify it to accomplish something else. I actually need to have a variable $id2 that I can use somewhere on the page outside of the <?php while ?> loop. Quote Link to comment https://forums.phpfreaks.com/topic/162643-solved-get-2-rows-of-data-separately/#findComment-858383 Share on other sites More sharing options...
ldougherty Posted June 17, 2009 Share Posted June 17, 2009 If you assign the variable inside the while loop you can use it anywhere on the page inside the PHP tags, or inside HTML by opening the php tag as <? echo $name ?> Quote Link to comment https://forums.phpfreaks.com/topic/162643-solved-get-2-rows-of-data-separately/#findComment-858385 Share on other sites More sharing options...
simon551 Posted June 17, 2009 Author Share Posted June 17, 2009 $result=mysql_query("SELECT * FROM mytable"); while ($row = mysql_fetch_array($result)) { $name = $row[name]; $email = $row; echo "name is $name and email is $email<br>"; } I got you that I can echo $name anywhere on the page. But how do I echo (exactly) the second name in 'myTable'? Quote Link to comment https://forums.phpfreaks.com/topic/162643-solved-get-2-rows-of-data-separately/#findComment-858394 Share on other sites More sharing options...
ldougherty Posted June 17, 2009 Share Posted June 17, 2009 http://us2.php.net/manual/en/function.mysql-fetch-row.php You can use mysql_fetch_row to return a single row instead of an array. This is a good way to pull out individual values from the database. The other option would be to use mysql_fetch_array and then reference the array object. For example assume you return 10 items in the array and you want item 2 and 3 you would reference them as $name[2] and $name[3] Quote Link to comment https://forums.phpfreaks.com/topic/162643-solved-get-2-rows-of-data-separately/#findComment-858397 Share on other sites More sharing options...
simon551 Posted June 17, 2009 Author Share Posted June 17, 2009 The other option would be to use mysql_fetch_array and then reference the array object. For example assume you return 10 items in the array and you want item 2 and 3 you would reference them as $name[2] and $name[3] That's what I'm trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/162643-solved-get-2-rows-of-data-separately/#findComment-858401 Share on other sites More sharing options...
simon551 Posted June 17, 2009 Author Share Posted June 17, 2009 This returns 0 8 but I'm expecting it to return, according to my database records 10856 10857 <?php mysql_select_db($database_conn_org, $conn_org); $query="SELECT etDetId FROM et_details WHERE etmId=3865"; $rs = mysql_query($query, $conn_org) or die(mysql_error()); $row = mysql_fetch_array($rs); $totalRows = mysql_num_rows($rs); while ($row = mysql_fetch_array($rs)) { //transaction information $id= $row['etDetId']; } echo $id[1].'<br />'.$id[2]; Quote Link to comment https://forums.phpfreaks.com/topic/162643-solved-get-2-rows-of-data-separately/#findComment-858406 Share on other sites More sharing options...
pkedpker Posted June 17, 2009 Share Posted June 17, 2009 yah i know what you are trying to do access data outside loops then you have to store it somewhere.. I'd use sessions or globals for this <?php function blahblah() { global $id; while(some shit) { $id[0] = "hommie"; $id[1] = "some shit i dont know"; } echo $id[0]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/162643-solved-get-2-rows-of-data-separately/#findComment-858419 Share on other sites More sharing options...
simon551 Posted June 17, 2009 Author Share Posted June 17, 2009 figured it out. <?php mysql_select_db($database_conn_org, $conn_org); $query="SELECT etDetId FROM et_details WHERE etmId=3865"; $rs = mysql_query($query, $conn_org) or die(mysql_error()); $totalRows = mysql_num_rows($rs); $i=0; while ($row = mysql_fetch_array($rs)) { $i++; //transaction information $id[$i]= $row['etDetId']; } echo $id[1].'<br />'.$id[2]; Quote Link to comment https://forums.phpfreaks.com/topic/162643-solved-get-2-rows-of-data-separately/#findComment-858421 Share on other sites More sharing options...
pkedpker Posted June 17, 2009 Share Posted June 17, 2009 ooooo u wanted to do i like that.. then use foreach() <?php colors foreach($arr as $key => $value){ $id[$key] = $value; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/162643-solved-get-2-rows-of-data-separately/#findComment-858422 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.