mysql_fetch_array() problem

Discussion in 'PHP' started by gilgalbiblewheel, Apr 20, 2011.

  1. #1
    This gives a plank page:
    <?php
    include("../includefiles/dbconnection.php");
    $bk=isset($_GET["book".$no]) ? $_GET["book".$no] : "";
    $ch=isset($_GET["chapter".$no]) ? $_GET["chapter".$no] : "";
    $vs=isset($_GET["verse".$no]) ? $_GET["verse".$no] : "";
    $source=isset($_GET["source".$no]) ? $_GET["source".$no] : "";
    $sourceAbb=Array("kjv", "mt", "mtnv", "tr");
    $SourceName=Array("King James", "Masoretic Text", "Masoretic No Vowels", "Textus Receptus");
    $SourceTable=Array($dbTable3, $dbTable7, $dbTable9, $dbTable8);
    for($i=0; $i<count($SourceTable); $i++){
    	if($source==$sourceAbb[$i]){
    		$sql = "SELECT * FROM ".$SourceTable[$i]." WHERE book = '".$bk."' AND chapter= '".$ch."'";
    		if($vs != 'all'){
    			$sql .= " AND verse= '".$vs."'";
    		}
    		$sql .= " ORDER BY id ASC";
    		//echo $sql;
    	}
    }
    if($result){
    	$result = mysql_query($query);
    	while($row = mysql_fetch_array($result)){
    		$id[] = $row['id'];
    		$bookTitle[] = $row['book_title'];
    		$book[] = $row['book'];
    		$bookAbb[] = $row['book_abb'];
    		$chapter[] = $row['chapter'];
    		$verse[] = $row['verse'];
    		$textData[] = $row['text_data'];
    	}
    ?>
    <div style="float: left; margin: 5px 0px 0px 0px; background-color: #7A1010; width: 183px; height: 19px; border: 1px solid #7A1010; color: white; font-weight: bold; text-align: left; padding: 0px 0px 0px 5px;">
    	<span style="float: left; padding: 2px 10px 0px 2px; color: #FFFFFF; font-family: arial; font-weight:bold; font-size: 13px"><input id="chk_all" name="checkallresults" type="checkbox" style="float: left;" onClick="if(this.checked == true){for (i=0; i<document.getElementsByName('checkresult_<?php echo $no; ?>').length; i++){document.getElementsByName('checkresult_<?php echo $no; ?>')[i].checked = true ;
    }}else{for (i=0;i<document.getElementsByName('checkresult_<?php echo $no; ?>').length; i++){document.getElementsByName('checkresult_<?php echo $no; ?>')[i].checked = false;}}" />all</span>
    	<span style="float: left; height: 10px; padding: 2px 10px 0px 2px; color: #FFFFFF; font-family: arial; font-weight: bold; font-size: 13px"><?php echo $bookTitle[0]. " " .$chapter[0];?></span>
    </div>
    <div id="txt_<?php echo $book[0]."_".$chapter[0]."_".$no; ?>" style="float: left; background-color: #EAE8C8; margin: 0px 0px 0px 0px; width: 178px; height: 407px; border: 1px solid #7A1010; padding: 5px 5px 0px 5px; overflow-y: auto; overflow-x: hidden;">
    <?php 
    	for($tdid=0; $tdid < count($id); $tdid++){
    		echo "\t\t\t\t\t<p id=\"regular[]\" name=\"bibletext\" style=\"float: left; text-align: left; width: 155px; display: block; padding: 0px 2px 0px 2px; font-size: 12px;\"><input id=\"check".$no."_".$tdid."\" name=\"checkresult_".$no."\" type=\"checkbox\" onclick=\"\" value=\"".$id[$tdid]."\" /><span style=\"font-weight: bold; margin: 2px;\">"."[".$id[$tdid]."] - ".$verse[$tdid]."</span>";
    		$strText = stripslashes(mysql_real_escape_string($textData[$tdid]));
    		include("../includefiles/highlight_getText.php");
    		echo "</p>\n";
    	}
    	echo "\t\t\t\t\t</div>\n";  
    }
    mysql_close($con);
    ?>
    PHP:
    But when I remove the if($result) then it would point out:
    What's the solution?
     
    gilgalbiblewheel, Apr 20, 2011 IP
  2. littlejohn199

    littlejohn199 Peon

    Messages:
    42
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The error that you got means that the result set you got from mysql_query is not a result. Usually this is caused by a malformed SQL statement. Change your code like this



    if($result){
    $result = mysql_query($query);


    ....
    }



    TO

    echo $sql;

    $result = mysql_query($query) or die(mysql_error());

    if($result)
    {

    ......
    }

    Basically move the $result part on top of the if statement

    Also, print out the SQL query in your code right after the for loop as I added above.

    Hope this will help
    Little John
     
    littlejohn199, Apr 22, 2011 IP
  3. job0107

    job0107 Peon

    Messages:
    23
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I imagine it should be like this:
    
    $result = mysql_query($sql);
    if($result){
        while($row = mysql_fetch_array($result)){
            $id[] = $row['id'];
            $bookTitle[] = $row['book_title'];
            $book[] = $row['book'];
            $bookAbb[] = $row['book_abb'];
            $chapter[] = $row['chapter'];
            $verse[] = $row['verse'];
            $textData[] = $row['text_data'];
        }
    PHP:
     
    job0107, Apr 22, 2011 IP
  4. littlejohn199

    littlejohn199 Peon

    Messages:
    42
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yes, you got it right. Also, it would be helpful to change this line
    $result = mysql_query($sql);


    To
    $result = mysql_query($query) or die(mysql_error());

    So you can see SQL error if there is one. This is for debugging purposes only. Once your code is working, remove or die(mysql_error());

    Hope this helps!

    Little John
     
    littlejohn199, Apr 23, 2011 IP
  5. rajeev_seo

    rajeev_seo Peon

    Messages:
    211
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yes, you got it right. Also, it would be helpful to change this line
    $result = mysql_query($sql);


    To
    first on the php error() function and then check that thread to solve that problem

    $result = mysql_query($query) or die(mysql_error());

    So you can see SQL error if there is one. This is for debugging purposes only. Once your code is working, remove or die(mysql_error());
     
    rajeev_seo, Apr 23, 2011 IP