Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/162643-solved-get-2-rows-of-data-separately/
Share on other sites

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.

$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'?

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]

 

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.

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

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];
?>

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

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.