Jump to content

Problem with echoing some vars


topshelfbleu

Recommended Posts

Hi

I am attempting to request a single row from a table that has 20 fields.

I want to turn each field into it's own variable (you'll see oc1, oc2 etc in the code) and then echo the variable just to check it has worked.

 

When I run the qry below - it picks up the first one correctly but then places the same value in all the other fields. (The $question var should be a long text string but is echoing the first variable instead.

There's clearly something wrong with the way I have constructed this - but I don't know at which point it has gone wrong.

 

Can anyone help?

 

$result = mysql_query("SELECT oid,qid,question,posneg,oc1,oc2,oc3,oc4,oc5,oc6,oc7,psc1,psc2,psc3,psc4,psc5,psc6,psc7,psc8,psc9 FROM tblquestions WHERE oid = '1'");
if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}

$oid=mysql_result($result,"oid");
$qid=mysql_result($result,"qid");
$question=mysql_result($result,"question");
$posneg=mysql_result($result,"posneg");
$oc1=mysql_result($result,"oc1");
$oc2=mysql_result($result,"oc2");
$oc3=mysql_result($result,"oc3");
$oc4=mysql_result($result,"oc4");
$oc5=mysql_result($result,"oc5");
$oc6=mysql_result($result,"oc6");
$oc7=mysql_result($result,"oc7");
$psc1=mysql_result($result,"psc1");
$psc2=mysql_result($result,"psc2");
$psc3=mysql_result($result,"psc3");
$psc4=mysql_result($result,"psc4");
$psc5=mysql_result($result,"psc5");
$psc6=mysql_result($result,"psc6");
$psc7=mysql_result($result,"psc7");
$psc8=mysql_result($result,"psc8");
$psc9=mysql_result($result,"psc9");
?>
  
<?php echo $oid; // Question Number?>
<?php echo $qid; // Question Number?>
<?php echo $question; // Question Number?>
<?php echo $posneg; // Question Number?>
<?php echo $oc1; // Question Number?>
<?php echo $oc2; // Question Number?>
<?php echo $oc3; // Question Number?>
<?php echo $oc4; // Question Number?>
<?php echo $oc5; // Question Number?>
<?php echo $oc6; // Question Number?>
<?php echo $oc7; // Question Number?>
<?php echo $psc1; // Question Number?>
<?php echo $psc2; // Question Number?>
<?php echo $psc3; // Question Number?>
<?php echo $psc4; // Question Number?>
<?php echo $psc5; // Question Number?>
<?php echo $psc6; // Question Number?>
<?php echo $psc7; // Question Number?>
<?php echo $psc8; // Question Number?>
<?php echo $psc9; // Question Number?>

Link to comment
https://forums.phpfreaks.com/topic/215922-problem-with-echoing-some-vars/
Share on other sites

Hi-

Thanks that appears to have worked - but could you spend a second explaining why? I'm not sure what that middle comma is doing?

 

Really grateful thank you.

 

Nick

 

You are welcome.

 

Its basic. If you search php manual http://php.net/manual/en/function.mysql-result.php, the syntax is

 

mysql_result  ( resource $result  , int $row  [, mixed $field = 0  ] )

 

which means, the first argument should be mysql $result resource (which you have put correctly), 2nd argument is result row number and 3rd argument is field name (which is optional, means if your query have only one field, you can omit this).

 

Now, you have skipped the mandatory 2nd argument (put a irrelevant text in place), so it just ignore that and show the default 1st row (which is fine as you dont have 2nd row). But the problem is, you dont have 3rd argument then, so, it by default showing you the first field which is "oid". I think this helps.

 

Thanks,

Archived

This topic is now archived and is closed to further replies.

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