mysql_fetch_assoc() [ SOMEONE HELP! :(( pls.

Discussion in 'PHP' started by jushiro, Sep 9, 2011.

  1. #1
    Im having a problem coding for our project :(. here's the code
    <?php
    $value = $_POST['p'];
    $host="localhost"; 
    $username="root"; 
    $password="";
    $db_name="dbquiz";
    
    
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");
    
    
    $value = stripslashes($value);
    $value = mysql_real_escape_string($value);
    
    
    $sql='SELECT * FROM `'. $value .'` ORDER BY RAND() LIMIT 100';
    $result=mysql_query($sql) or die(mysql_error());
    
    
    if(result){
    while($row = mysql_fetch_assoc($result)) 
        { 
        $q = $row['question']; 
        $c1= "" .$row['choice1']; 
        $c2 ="" .$row['choice2']; 
        $c3 ="" .$row['choice3']; 
        $c4 ="" .$row['choice4']; 
        $a ="".$row['answer'];
        $questions[] = array($q,$c1,$c2,$c3,$c4,$a);
        } 
    }
    include_once("makequiz.php");
    
    
    ?>
    Code (markup):
    AND FOR THE makequiz
    
    <?php 
    if (isset($_POST['sent'])) {
        for ($i=0;$i<count($questions);$i++) {
            echo($questions[$i][0]." - ");
            if ($_POST['q'.$i]=="c") {
                echo("<b>Correct!</b><br>\n");
                $score++;
            } else {
                echo("<b>Wrong!</b><br>\n");
            }
        }
        $percent = number_format(($score/count($questions))*100,2,".",",");
        echo("<br>".$score." out of ".count($questions)." (".$percent."% right)<br>\n");
    } else {
        echo("<form action=\"#\" method=\"post\">\n");
        echo("<input type=\"hidden\" name=\"sent\">\n");
        for ($i=0;$i<count($questions);$i++) {
            echo("<b>".$questions[$i][0]."</b><br><br>\n");
            if ($questions[$i][5]==1) {
                echo("<input type=\"radio\" name=\"q".$i."\" value=\"c\"> ".$questions[$i][1]."<br>\n");
            } else {
                echo("<input type=\"radio\" name=\"q".$i."\" value=\"w\"> ".$questions[$i][1]."<br>\n");
            }
            if ($questions[$i][5]==2) {
                echo("<input type=\"radio\" name=\"q".$i."\" value=\"c\"> ".$questions[$i][2]."<br>\n");
            } else {
                echo("<input type=\"radio\" name=\"q".$i."\" value=\"w\"> ".$questions[$i][2]."<br>\n");
            }
            if ($questions[$i][5]==3) {
                echo("<input type=\"radio\" name=\"q".$i."\" value=\"c\"> ".$questions[$i][3]."<br>\n");
            } else {
                echo("<input type=\"radio\" name=\"q".$i."\" value=\"w\"> ".$questions[$i][3]."<br>\n");
            }
            if ($questions[$i][5]==4) {
                echo("<input type=\"radio\" name=\"q".$i."\" value=\"c\"> ".$questions[$i][4]."<br><br>\n");
            } else {
                echo("<input type=\"radio\" name=\"q".$i."\" value=\"w\"> ".$questions[$i][4]."<br><br>\n");
            }
        }
        echo("<input type=\"submit\" value=\"Am I Right?!\">");
    } ?>
    Code (markup):
    When you run the first code.. it's working but when i clicked the submit button this error keeps on showing and i dont know why.. "Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in " can someone help me fix this problem pls? thx in advance.
     
    jushiro, Sep 9, 2011 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    I'm guessing, but use Firefox, add the Firebug and FirePHP addons and change your code to
    
    
    $sql='SELECT * FROM `'. $value .'` ORDER BY RAND() LIMIT 100'; $result=mysql_query($sql) or die(mysql_error()); 
    if(!false === $result) {   fb::log($result);
    } else {
      fb::log('Query failed.');
    } 
    if(result){ while($row = mysql_fetch_assoc($result)) 
    
    Code (markup):
    If the query fails, change the code at the top to
    
    $value = $_POST['p'];
    fb::log('"'.$value.'"');
    ...
    $value = stripslashes($value); $value = mysql_real_escape_string($value);
    
    fb::log('"'.$value.'"');
    
    Code (markup):
    to see what's happening. I suspect that you're getting a blank for $value, so your SQL query is
    'SELECT * FROM `` ORDER BY RAND() LIMIT 100';

    That will fail, making $result null, making mysql_fetch_assoc($result) fail.
     
    Rukbat, Sep 9, 2011 IP
  3. jushiro

    jushiro Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It says..

    Fatal error: Class 'fb' not found in

    hmm.. how can i make the $value not null? :(
     
    jushiro, Sep 9, 2011 IP
  4. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #4
    Value is what's in $_POST['p'] when you submit. Make sure the page doesn't submit to your PHP file if the element whose ID is p is blank.

    "Class 'fb' not found" is due to the fact that you didn't add
    
        ob_start();
        require('FirePHPCore/fb.php');
        FB::setEnabled(true);
    
    
    Code (markup):
    at the top of your code. It's in the information on the FirePHP site. Once you learn to use FirePHP, you won't write PHP code without it. (ob_start() is required to prevent a "headers already sent" error.)
     
    Rukbat, Sep 9, 2011 IP
  5. hotnoob

    hotnoob Member

    Messages:
    96
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    28
    #5
    for in the future:
    
    if(result)
    
    Code (markup):
    should be if($result)
    however, you don't need the if statement, because that's exactly what the while statement is doing.


    
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");
    
    Code (markup):
    should be
    
    mysql_connect($host, $username, $password)or die("cannot connect"); 
    mysql_select_db($db_name)or die("cannot select DB");
    
    Code (markup):
    it's just a waste of effort and processing time to put the variables in their own double quotes

    
    $value = stripslashes($value);
    $value = mysql_real_escape_string($value);
    
    Code (markup):
    should be
    
    if(get_magic_quotes_gpc())
    {
       $value = stripslashes($value);
    }
    
    $value = mysql_real_escape_string($value);
    
    Code (markup):
    that way, if you transfer your code to a server that doesn't have magic quotes enabled, it wont screw up your content

    
    $result=mysql_query($sql) or die(mysql_error());
    
    Code (markup):
    might want to look at that, i've never done it that way before.

    and "makequiz.php" shouldn't effect a function called way up at the top.

    as for the error, i'm not sure whats going on, but you could try mysql_fetch_array instead.
    oh and you might also want to consider changing LIMIT 100 to LIMIT 0,100
    if your using an old version of mysql, it could bug out on that.
     
    hotnoob, Sep 9, 2011 IP
  6. jushiro

    jushiro Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    i dont get it.. :( ' im noob at this.
     
    jushiro, Sep 9, 2011 IP