Need help! I need to fix this error...

Discussion in 'PHP' started by Carsen, Aug 2, 2008.

  1. #1
    Ok, So I am working on a script, I get this error -

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/qaklock/public_html/add_answer.php on line 10
    Code (markup):
    Here is line 7-10 :

    // Find highest answer number.
    $sql="SELECT MAX(a_id) AS `maxa_id` FROM `$tbl_name` WHERE `question_id`='$id'";
    $result=mysql_query($sql);
    $rows=mysql_fetch_array($result);
    Code (markup):
    I think it is giving the error due to the query being empty, but how can I fix it from erroring and letting the script do what it is suppose to do. Here is the full part of this piece of script -

    <?php
    include("connect.php");
    
    // Get value of id that sent from hidden field
    $id=$_POST['id'];
    
    // Find highest answer number.
    $sql="SELECT MAX(a_id) AS `maxa_id` FROM `$tbl_name` WHERE `question_id`='$id'";
    $result=mysql_query($sql);
    $rows=mysql_fetch_array($result);
    
    // add + 1 to highest answer number and keep it in variable name "$Max_id". if there no answer yet set it = 1
    if ($rows) {
    $max_id = $rows['Maxa_id']+1;
    }
    else {
    $max_id = 1;
    }
    
    // get values that sent from form
    $a_name=$_POST['a_name'];
    $a_email=$_POST['a_email'];
    $a_answer=$_POST['a_answer'];
    
    $datetime=date("d/m/y H:i:s"); // create date and time
    
    // Insert answer
    $sql2="INSERT INTO $tbl_name(question_id, a_id, a_name, a_email, a_answer, a_datetime)VALUES('$id', '$Max_id', '$a_name', '$a_email', '$a_answer', '$datetime')";
    $result2=mysql_query($sql2);
    
    if($result2){
    echo "Successful<BR>";
    echo "<a href='view_topic.php?id=".$id."'>View your answer</a>";
    
    // If added new answer, add value +1 in reply column
    $tbl_name2="forum_question";
    $sql3="UPDATE $tbl_name2 SET reply='$max_id' WHERE id='$id'";
    $result3=mysql_query($sql3);
    
    }
    else {
    echo $query;
    $results = MySQL_Query($query) or die(MySQL_Error());
    }
    
    mysql_close();
    ?>
    Code (markup):
     
    Carsen, Aug 2, 2008 IP
  2. Hallent

    Hallent Peon

    Messages:
    65
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    after:
    $result=mysql_query($sql);

    add:
    echo mysql_error();

    and paste what it echos
     
    Hallent, Aug 2, 2008 IP
  3. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #3
    In order to better help you out can u post here sample DB results in table which match this query

    Does this query allways gives error or sometimes

    to remove error change this line
    $rows=mysql_fetch_array($result);

    to

    $rows=@mysql_fetch_array($result);

    Regards

    Alex
     
    kmap, Aug 2, 2008 IP
  4. Carsen

    Carsen Well-Known Member

    Messages:
    427
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    140
    #4
    It says - Query was empty

    and

    @kmap - I just tried that and it just doesn't give the error, but doesn't do what it is suppose to do, which is to post the data input into the text boxes to the database and then display a link back to the topic.
     
    Carsen, Aug 2, 2008 IP
  5. Hallent

    Hallent Peon

    Messages:
    65
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    if (isset($_POST['id'])) {
    $id=$_POST['id'];

    // Find highest answer number.
    $sql="SELECT MAX(a_id) AS `maxa_id` FROM `$tbl_name` WHERE `question_id`='$id'";
    $result=mysql_query($sql);
    $rows=mysql_fetch_array($result);

    // add + 1 to highest answer number and keep it in variable name "$Max_id". if there no answer yet set it = 1
    if ($rows) {
    $max_id = $rows['Maxa_id']+1;
    }
    else {
    $max_id = 1;
    }
    } else {
    $max_id=1;
    }
     
    Hallent, Aug 2, 2008 IP
  6. Carsen

    Carsen Well-Known Member

    Messages:
    427
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    140
    #6
    Still gives the same error....
     
    Carsen, Aug 2, 2008 IP
  7. Hallent

    Hallent Peon

    Messages:
    65
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    lol would help if you named Max_id and max_id the same thing variables are case sensitive
     
    Hallent, Aug 2, 2008 IP
  8. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #8
    i have asked you to post sample db results that atch your query

    If your db has no results matching query then it will be empty everytime

    Make sure you have the data in table
    Please post some data here

    Regards

    Alex
     
    kmap, Aug 2, 2008 IP
  9. Hallent

    Hallent Peon

    Messages:
    65
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #9
    repost your code since you have added changes so i can see what you did...
     
    Hallent, Aug 2, 2008 IP
  10. Hallent

    Hallent Peon

    Messages:
    65
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #10
    lol or that kmap :D
     
    Hallent, Aug 2, 2008 IP
  11. Carsen

    Carsen Well-Known Member

    Messages:
    427
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    140
    #11
    Well the code is still basically the same -

    <?php
    include("connect.php");
    
    // Get value of id that sent from hidden field
    $id=$_POST['id'];
    
    // Find highest answer number.
    $sql="SELECT MAX(a_id) AS `maxa_id` FROM `$tbl_name` WHERE `question_id`='$id'";
    $result=mysql_query($sql);
    $rows=@mysql_fetch_array($result);
    
    // add + 1 to highest answer number and keep it in variable name "$Max_id". if there no answer yet set it = 1
    if ($rows) {
    $max_id = $rows['maxa_id']+1;
    }
    else {
    $max_id = 1;
    }
    
    // get values that sent from form
    $a_name=$_POST['a_name'];
    $a_email=$_POST['a_email'];
    $a_answer=$_POST['a_answer'];
    
    $datetime=date("d/m/y H:i:s"); // create date and time
    
    // Insert answer
    $sql2="INSERT INTO $tbl_name(question_id, a_id, a_name, a_email, a_answer, a_datetime)VALUES('$id', '$Max_id', '$a_name', '$a_email', '$a_answer', '$datetime')";
    $result2=mysql_query($sql2);
    
    if($result2){
    echo "Successful<BR>";
    echo "<a href='view_topic.php?id=".$id."'>View your answer</a>";
    
    // If added new answer, add value +1 in reply column
    $tbl_name2="forum_question";
    $sql3="UPDATE $tbl_name2 SET reply='$max_id' WHERE id='$id'";
    $result3=mysql_query($sql3);
    
    }
    else {
    echo $query;
    $results = MySQL_Query($query) or die(MySQL_Error());
    }
    
    mysql_close();
    ?>
    Code (markup):
    and also, There is no data in the database for this part of the script, But I want it to work so that I dont have to put nulled data or something in there. If you guys want, Please PM for a link to the script on my server.
     
    Carsen, Aug 2, 2008 IP
  12. Hallent

    Hallent Peon

    Messages:
    65
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #12
    ok so lets change to this:
    if ($rows=mysql_fetch_array($result)) {
    $max_id = $rows['Maxa_id']+1;
    }
    else {
    $max_id = 1;
    }
     
    Hallent, Aug 2, 2008 IP
  13. Hallent

    Hallent Peon

    Messages:
    65
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #13
    also instead of mysql_fetch_array try mysql_fetch_assoc
     
    Hallent, Aug 2, 2008 IP
  14. Carsen

    Carsen Well-Known Member

    Messages:
    427
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    140
    #14
    Still a no go...This thing is really giving me a headache :( lol...
     
    Carsen, Aug 2, 2008 IP
  15. Hallent

    Hallent Peon

    Messages:
    65
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #15
    you tried:
    if ($rows=mysql_fetch_assoc($result)) {
    $max_id = $rows['Maxa_id']+1;
    }
    else {
    $max_id = 1;
    }

    and that didnt work?
    there is always error_reporting(0); :p
     
    Hallent, Aug 2, 2008 IP
  16. Hallent

    Hallent Peon

    Messages:
    65
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #16
    that code should have replaced:

    $rows=@mysql_fetch_array($result);

    // add + 1 to highest answer number and keep it in variable name "$Max_id". if there no answer yet set it = 1
    if ($rows) {
    $max_id = $rows['maxa_id']+1;
    }
    else {
    $max_id = 1;
    }
     
    Hallent, Aug 2, 2008 IP
  17. Hallent

    Hallent Peon

    Messages:
    65
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #17
    after you make change paste it and show me your changes to verify
     
    Hallent, Aug 2, 2008 IP
  18. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #18
    lol if you donot have any data how you expecting it to work

    It is allways going to show empty results

    Regards

    Alex
     
    kmap, Aug 3, 2008 IP
  19. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #19
    $sql="SELECT MAX(a_id) AS maxa_id FROM $tbl_name WHERE question_id = $id ";
     
    php-lover, Aug 3, 2008 IP
  20. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #20
    Try this, I am sure you required this:
    
    #First prevent SQL Injection
    #if $_POST['id'] is integer use [B]intval[/B]($_POST['id']), and if it is string use [B]htmlspecialchars[/B]($_POST['id'])
    #-------------------------------------------------------------------------------------------------
    $id=$_POST['id']; #Talking about this for SQL Injection
    
    // Find highest answer number.
    $sql="SELECT MAX(a_id) AS `maxa_id` FROM `$tbl_name` WHERE `question_id`='$id'";
    $result=[B]mysql_query[/B]($sql);
    
    if([B]$result === FALSE[/B]) {
    	echo "1:".mysql_error(); #query has error
    	@mysql_close();
    	die();
    }
    
    // add + 1 to highest answer number and keep it in variable name "$Max_id". if there no answer yet set it = 1
    if([B]mysql_num_rows[/B]($result) ==0) {
    	$max_id = 1;
    } else {
    	$rows = [B]mysql_fetch_assoc[/B]($result);
    	$max_id = $rows['maxa_id'] + 1;
    }
    
    Code (markup):
     
    Vooler, Aug 3, 2008 IP