Ravens PHP Scripts: Forums
 

 

View next topic
View previous topic
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> MySQL
Author Message
Donovan
Client



Joined: Oct 07, 2003
Posts: 735
Location: Ohio

PostPosted: Thu Jun 19, 2008 9:13 am Reply with quote

I have code that I am trying to update and improve.

The original developer used the db/db.php abstraction layers, however the last guy to mess with this code did not. He created another mysql_connect.php and placed them where he needed them which has caused me utter confusion.

The original developer used $db->sql_query($sql) which I am most familar with.

The last guy used stuff like
Code:
$res = @mysql_query($query);


or
Code:
$media = mysql_result($res, 0, "media_dir");


I have been trying to understand what mysql_result($res, 0, "media_dir") would do and what the equivalent would be.

Here is what the last guy did.
Code:


require_once('mysql_connect.php');
//include($somCBT_root_path . 'includes/page_header.'.$phpEx);
$query = "SELECT media_dir FROM course WHERE course_id = " .$cid." LIMIT 1";
$res = @mysql_query($query);
$media = mysql_result($res, 0, "media_dir");
$media = addslashes($media);
//Here, we are joining the quiz, quiz_questions, and questions tables in order to have a single recordset with all of the information that we will need to
//post on the print page. This is required in order to ensure that we are only dealing with a single test for a single course - SMQ
if($lock=='y') { 
$query1 = "SELECT * FROM quiz, quiz_questions WHERE quiz.quiz_id=".$qid." AND quiz.course_id=".$cid." AND quiz.quiz_id=quiz_questions.quiz_id AND quiz.course_id=quiz_questions.course_id ORDER BY quiz_questions.ques_order" ;
}else{
$query1 = "SELECT quiz.quiz_name, quiz.year_used, quiz_questions.ques_order, quiz_questions.ques_id, questions.* FROM quiz, quiz_questions, questions WHERE quiz.quiz_id=".$qid." AND quiz.course_id=".$cid." AND quiz.quiz_id=quiz_questions.quiz_id AND quiz_questions.ques_id=questions.question_id ORDER BY quiz_questions.ques_order" ;
}
$result=mysql_query($query1);
//Count the number of records returned from the SQL query, and use this to tell the loop when to stop iterating through the associative array created from the
//database query - SMQ
$num=mysql_num_rows($result);
mysql_close();
//print_r($result);
$med = $root_path.$media;
//}else{
//   break();
//}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Print Test</title>
</head>

<body>
<p><a href="javascript:history.back();"><strong>return</strong></a></p>
<table width="100%" border="0">
  <!--before entering the loop, grab the test name, qtype, and year_used from the array and write these to the table at the top - SMQ -->
  <?php $quizname=mysql_result($result, 0, "quiz_name");?>
  <?php $year=mysql_result($result, 0, "year_used");?>
  <tr>
    <td width="4%"><strong>Quiz Name:</strong> <?php echo $quizname;?></td>
    <td width="50%"><div align="center"><strong>Quiz Year:</strong> <?php echo $year;?></div></td>
  </tr>
<!--Start looping through the array and grabbing the pertinent fields from each row we encounter. Next, write these variables to the table - SMQ -->
<?php $i=0; ?>
<?php while($i < $num) {?>
<?php $imgfl=''; ?>
<?php $dbno=mysql_result($result, $i, "ques_id");?>
<?php $qtitle=mysql_result($result, $i, "ques_name");?>
<?php $qstem=nl2br(mysql_result($result, $i, "stem"))."<br>";?>
<?php $c1=mysql_result($result, $i, "choice1");?>
<?php $c2=mysql_result($result, $i, "choice2");?>
<?php $c3=mysql_result($result, $i, "choice3");?>
<?php $c4=mysql_result($result, $i, "choice4");?>
<?php $c5=mysql_result($result, $i, "choice5");?>
<?php $imgfl=mysql_result($result, $i, "media_name");?>
<?php $feed=mysql_result($result, $i, "feedback");?>
<?php $opt=mysql_result($result, $i, "points");?>
<?php $type=mysql_result($result, $i, "qtype");?>
<?php $qnum = $i+1; ?>
<?php $opt_arr = explode("#", $opt);?>
<?php $qimage = $med.$imgfl;?>
  <tr>
    <td colspan="3"><hr /></td>
  </tr>
  <tr>
    <td colspan="3"><strong>Question #: <?php echo $qnum;?></strong> </td>
  </tr>
  <tr>
    <td colspan="3"><strong>DB Rec.#: <?php echo $dbno;?></strong> </td>
  </tr>
  <tr>
    <td colspan="3"><strong>Title: <?php echo $qtitle;?></strong> </td>
  </tr>
    <tr>
      <td colspan="3">&nbsp;</td>
    </tr>
    <tr>
    <td colspan="3"><?php if($imgfl=='') { echo $qstem; }else{ echo $qstem.'<br><br><img src = "'.$qimage.'">'; }?> </td>
  </tr>
    <tr>
      <td colspan="3">&nbsp;</td>
    </tr>
   <?php if($type =='1') {?>
    <tr>
    <td colspan="3"><strong>A.</strong>&nbsp;<?php echo $c1;?> </td>
  </tr>
    <tr>
    <td colspan="3"><strong>B.</strong>&nbsp;<?php echo $c2;?> </td>
  </tr>
    <tr>
    <td colspan="3"><strong>C.</strong>&nbsp;<?php echo $c3;?> </td>
  </tr>
    <tr>
    <td colspan="3"><strong>D.</strong>&nbsp;<?php echo $c4;?> </td>
  </tr>
    <tr>
    <td colspan="3"><strong>E.</strong>&nbsp;<?php echo $c5;?> </td>
  </tr>
    <tr>
      <td colspan="3">&nbsp;</td>
    </tr>
   <?php for ($j=0; $j < (count($opt_arr)-1); $j++) {?>
   <?php if($j==0) {$ans="A";}elseif($j==1) {$ans="B";}elseif($j==2) {$ans="C";}elseif($j==3) {$ans="D";}else{$ans="E";}?>
   <?php if($opt_arr[$j] == 1) {?>
    <tr>
     
    <td colspan="3"><strong>Correct Answer:</strong> <?php echo $ans;?></td>
    </tr>
   <?php } ?>
   <?php } ?>
    <tr>
      <td colspan="3">&nbsp;</td>
    </tr>
   <?php } ?>
    <tr>
      <td colspan="3"><strong>Feedback: </strong><?php echo $feed;?></td>
    </tr>
    <tr>
      <td colspan="3">&nbsp;</td>
    </tr>
  <?php $i++;?>
  <?php } ?>
</table>
</body>


I would like to replace much of this code with some that is easier understood.

This part here looks more like a sql_fetchrow
Code:


<?php $i=0; ?>
<?php while($i < $num) {?>
<?php $imgfl=''; ?>
<?php $dbno=mysql_result($result, $i, "ques_id");?>
<?php $qtitle=mysql_result($result, $i, "ques_name");?>
<?php $qstem=nl2br(mysql_result($result, $i, "stem"))."<br>";?>
<?php $c1=mysql_result($result, $i, "choice1");?>
<?php $c2=mysql_result($result, $i, "choice2");?>
<?php $c3=mysql_result($result, $i, "choice3");?>
<?php $c4=mysql_result($result, $i, "choice4");?>
<?php $c5=mysql_result($result, $i, "choice5");?>
<?php $imgfl=mysql_result($result, $i, "media_name");?>
<?php $feed=mysql_result($result, $i, "feedback");?>
<?php $opt=mysql_result($result, $i, "points");?>
<?php $type=mysql_result($result, $i, "qtype");?>
<?php $qnum = $i+1; ?>
<?php $opt_arr = explode("#", $opt);?>
<?php $qimage = $med.$imgfl;?>
  <tr>
 
View user's profile Send private message Visit poster's website ICQ Number
evaders99
Former Moderator in Good Standing



Joined: Apr 30, 2004
Posts: 3221

PostPosted: Thu Jun 19, 2008 4:23 pm Reply with quote

mysql_result grabs a specific row and column
http://us.php.net/mysql_result

The database abstraction layer provides the following equivalent
Code:


sql_fetchfield($field, $rownum = -1, $query_id = 0)


In pretty much all cases, it is better to grab the entire row using sql_fetch_row and use PHP's array structure to numerate through the fields.

_________________
- Only registered users can see links on this board! Get registered or login! -

Need help? Only registered users can see links on this board! Get registered or login! 
View user's profile Send private message Visit poster's website
Display posts from previous:       
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> MySQL

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2007 phpBB Group
All times are GMT - 6 Hours
 
Forums ©