SQL Query problem - How to get the value of IN used

Hi,

i want to know how to capture the value of IN used in a SQL query… so i know the column number it used as i want to use it later to get a related record.

$mysql1 = mysql_query(“SELECT * FROM storydetails WHERE storyno = ‘1’ AND partno IN (‘$a’,‘$b’,‘$c’) AND used <3 LIMIT 1”);

Then i can use it later in : $mysql2 = mysql_query("SELECT * FROM answers WHERE storyno = ‘3’ AND partno = ‘$variable’ ");

where $variable is the column number

any hints or pointers gratefully accepted.

Thanks


&lt;form action="insert_story.php" method="post" onsubmit="return validate()"&gt;
&lt;input type="hidden" name="page_number" value="3" /&gt;

&lt;?PHP

    $user_name = "erer";
    $password = "rerer";
    $database = "rtrtr";
    $server = "rtrtr";

$con = mysql_connect($server ,$user_name ,$password);
$db_found = mysql_select_db($database);

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);

$rchar=chr(rand(65,67));
$a="A";
$b="B";
$c="C";

$mysql =  mysql_query("SELECT * FROM storydetails WHERE storyno = '1' AND partno = '$rchar' AND used &lt;3");
$mysql1 = mysql_query("SELECT * FROM storydetails WHERE storyno = '1' AND partno IN ('$a','$b','$c') AND used &lt;3 LIMIT 1");

if ($mysql='&lt;3')
	
	while($row = mysql_fetch_array($mysql))
  {
  echo $row['text']. "&lt;P/&gt;" . "Story Number:" . $row['storyno']." Part: " . $row['partno']." Used: " . $row['used']. " ID : ". $row['id'];
  $my_id = $row['id'];
  $story_id = $row['storyno'];
  $select_id = 1;
  echo "&lt;br /&gt;";
  }
 		elseif ($mysql1='&lt;3')
		
	while($row = mysql_fetch_array($mysql))
  {
  echo $row['text']. "&lt;P/&gt;" . "Story Number:" . $row['storyno']." Part: " . $row['partno']." Used: " . $row['used']. " ID : ". $row['id'];
  $my_id = $row['id'];
  $story_id = $row['storyno'];
  $select_id = 2;
  echo "&lt;br /&gt;";
  }
		else
	echo "All Questions have been used up.... End of questionaire";

echo "&lt;p/&gt;";
echo "&lt;p/&gt;";

/*
 loop to display answers
*/

if ($select_id=2)
$mysql2 = mysql_query("SELECT * FROM answers WHERE storyno = '3' AND partno = '$rchar' ");
else
$mysql2 = mysql_query("SELECT * FROM answers WHERE storyno = '3' AND partno = '$rchar' ");

while($row1 = mysql_fetch_array($mysql2))
  {
  echo "&lt;input type='radio' value='" . $row1['response'] . "' name='answer1' /&gt;" . $row1['response'] ;
  echo "&lt;br /&gt;";
  }


mysql_close($con);
?&gt;

&lt;br /&gt;

&lt;input type="hidden" name="story1_id" value="&lt;?=$my_id?&gt;" /&gt;
&lt;input type="hidden" name="story1_num" value="&lt;?=$story_id?&gt;" /&gt;
&lt;input type="submit" /&gt;
&lt;/form&gt;
[/code/

$mysql1 = mysql_query(“SELECT * FROM storydetails WHERE storyno = ‘1’ AND partno IN (‘$a’,‘$b’,‘$c’) AND used <3 LIMIT 1”);

Then i can use it later in : $mysql2 = mysql_query("SELECT * FROM answers WHERE storyno = ‘3’ AND partno = ‘$variable’ ");

If both of the tables contain a field called partno, and the values mean exactly the same thing, then you should be able to use a table JOIN therefore getting all the data you need in a single sql statement.

Unfortunately you are using SELECT * from your tables so we do not have much idea about the names of your fields.

But if I imagine that storydetails table contains fields which include title and published_date, and your answerstable contains the fields answer_text and contributor_id then to fetch all the details you need with the answers in one hit you would do something similar to this:



// a) in the next line, change the names after s. and a. to match your tables
$sql = "SELECT s.title, s.published_date, a.answer_text, a.contributor_id 
FROM 
storydetails as s
LEFT JOIN 
answers as a
ON
s.partno = a.partno
WHERE s.partno IN (1,2,3)";  // <- c) put some real values into the IN clause

echo $sql;

If you carefully alter some of those values to match your own tables’ fields I have the feeling that this is what you are trying to accomplish, is it?