PHP Web Host - Quality Web Hosting For All PHP Applications Sign up for PayPal and start accepting credit card payments instantly
  Login or Register
 • Home • Downloads • Your Account • Forums • 

View next topic
View previous topic


Google
 
Web RavenPHPScripts (This Site)
Post new topic   Reply to topic
Author Message
Donovan
Client


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

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

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
Moderator


Joined: Apr 30, 2004
Posts: 2844

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

mysql_result grabs a specific row and column
Only registered users can see links on this board!
Get registered or login to the forums!


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.
View user's profile Send private message Visit poster's website
Display posts from previous:       
Post new topic   Reply to topic

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
Forums ©
 

All logos and trademarks in this site are property of their respective owner.
The comments are property of their posters, all the rest © 2002-2008 by Raven
Proud to be listed at Lobo Links Web Directory

You can syndicate our news using the file xml

CSE HTML Validator Helped Clean up This Page! [Valid RSS] valid RSS 2.0 Valid robots.txt Stop Spam Harvesters, Join Project Honey Pot

Website engines core code is © copyright by PHP-Nuke but has been heavily patched and modified by myself and others.
PHP-Nuke is a free software released under the GNU/GPL.


:: fisubice phpbb2 style by Daz :: PHP-Nuke theme by www.nukemods.com ::

:: fisubice Theme Recoded To 100% W3C CSS & HTML 4.01 Transitional Compliance by Raven and 64bitguy ::

:: W3C CSS Compliance Validation :: W3C HTML 4.01 Transitional Compliance Validation ::

zerosum