0

echo 'DESCRIPTION: '.$row['DESCRIPTION']; in the code below is not displaying at all...It runs the first statement but the second one doesn't seem to be executing. I don't understand why. I'm not getting any error which is the worse kind of error. Some help please!

PHP FILE:

$sql="SELECT * FROM $tbl_name WHERE `ID` != '67' && `ID` != '68' ORDER BY SORT_ORDER ASC";
$result=mysql_query($sql);
if ($result === false) { echo "An error occurred."; }
?>

<?php
//mysql_connect("$host", "$username", "$password")or die("cannot connect");
//mysql_select_db("$db_name")or die("cannot select DB");

      echo $_POST['book'];
      echo $_POST['chapter'];
      $book = $_POST['book'];
      $chapter = $_POST['chapter'];
      $tbl_link = "PLD_LINK";

      $sql1 = "SELECT * FROM $tbl_link WHERE `TITLE` like '%$book%' && `SORT_ORD` = '%$chapter%'";
      $result1=mysql_query($sql);
      if ($result1 === false) { echo "An error occurred."; }
      while ($row = mysql_fetch_array($sql1)){
      echo 'DESCRIPTION: '.$row['DESCRIPTION'];
      }
?>

<?php
while($rows=mysql_fetch_array($result)){
{ $results[] = $rows; }
$tpl->assign('results', $results); 
?>
<?php
}
mysql_close();
unset ($username, $password, $db_name, $tbl_name);
?>

TPL FILE:

<form action="search_bible.php" method="post">
<p><select name='book'><option value="">Select Book</option></p>
{foreach item=rows from=$results}<p><option value='{$rows.TITLE}'>{$rows.TITLE}</option></p>{/foreach}
</select>
&nbsp;
<span style="font-size:12px;">Chapter<input type="text" name="chapter" value="1" size="2" />
&nbsp;
<input type="submit" value="GO">
</form>

2 Answers 2

3
while ($row = mysql_fetch_array($sql1)){
      echo 'DESCRIPTION: '.$row['DESCRIPTION'];
      }

has to be

while ($row = mysql_fetch_array($result1)){
  echo 'DESCRIPTION: '.$row['DESCRIPTION'];
  }

and

$result1=mysql_query($sql);

has to be

 $result1=mysql_query($sql1);

You might want to consider giving variabels a name more suited for the result that you will get.

This will not only make it easier to tell which will have what result, it will also be easier to pick up where you left off if you ever want to pick up a project that you haven't touched in a while or if someone else has ever to work with your code.

I'll end with some wise words that I had from a teacher a while ago.

'If you have a variable named beers' which represents an array of numbers, the result will always dissapoint you'

Sign up to request clarification or add additional context in comments.

Comments

1

You must change your while loop to

while ($row = mysql_fetch_array($result1)) {
...
}

because, $sql1 is just a string and not the result of the previous mysql_query.

You should consider switching to mysqli or PDO, because mysql_* functions are deprecated by now.

1 Comment

Thanks for informing me. I switched to PDO.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.