Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

Discussion in 'PHP' started by chrishawkins, Nov 30, 2009.

  1. #1
    I am getting this error; Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in

    running this code;

    <form name="form1" method="post" action="">
    <label>
    <input type="submit" name="update" id="update" value="Update Records">
    </label>
    </form>
    <form name="form2" method="get" action="test.php?show_record=1">
    <label>
    <input type="submit" name="show_results" id="show_results" value="Show Results">
    </label>
    </form>
    <?php
    $con = mysql_connect("localhost","username","password");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("my_db", $con);
    
    if(isset($_GET['show_results']) && $_GET['show_results'] == 1){
    $res = mysql_query("SELECT *, probid_users.name AS name,
    probid_winners.winner_id AS winnerid
    FROM probid_winners
    left join probid_users ON probid_users.user_id = probid_winners.buyer_id
    WHERE probid_winners.payment_status='confirmed' AND printed='' AND buyer_id BETWEEN 2 AND 1000000000 ORDER BY bid_amount DESC = " . mysql_real_escape_string($_GET['show_results']));
    }
    
    echo "<table border='5'>
    <tr>
    <th>Name</th>
    <th>Auction ID</th>
    <th>Bid Amount</th>
    <th>Deposit Status</th>
    </tr>";
    
    while($row = mysql_fetch_array($res))
    {
    echo "<tr>";
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['auction_id'] . "</td>";
    echo "<td>" . $row['bid_amount'] . "</td>";
    echo "<td>" . $row['payment_status'] . "</td>";
    echo "<td><form1=\"update_row\" method=\"post\" action=\"test.php?id=".$row['winnerid']."\">
    <input type=\"hidden\" name=\"updating_row\" value=\"".$row['winnerid']."\" />
    <input type=\"submit\" name=\"submit_update\" value=\"Remove\" />
    </form>";
    echo "</tr>";
    
    if(isset($_POST['updating_row'])){
    $res = mysql_query("UPDATE probid_winners SET printed='printed' WHERE probid_winners.winner_id = " . mysql_real_escape_string($_POST['updating_row']));
    }
    }
    echo "</table>";
    
    mysql_close($con);
    ?>
    PHP:

    I was hoping that someone might be able to help me out with this issue.
     
    chrishawkins, Nov 30, 2009 IP
  2. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #2
    Query is wrong.

    
    $res = mysql_query("SELECT *, probid_users.name AS name,
    probid_winners.winner_id AS winnerid
    FROM probid_winners
    left join probid_users ON probid_users.user_id = probid_winners.buyer_id
    WHERE probid_winners.payment_status='confirmed' AND printed='' AND buyer_id BETWEEN 2 AND 1000000000 
    [B]ORDER BY bid_amount DESC = " . mysql_real_escape_string($_GET['show_results']));[/B]
    
    Code (markup):
    Why giving equals after ORDER BY?

    Try following query...

    
    $res = mysql_query("SELECT *, probid_users.name AS name,
    probid_winners.winner_id AS winnerid
    FROM probid_winners
    left join probid_users ON probid_users.user_id = probid_winners.buyer_id
    WHERE probid_winners.payment_status='confirmed' AND printed='' AND buyer_id BETWEEN 2 AND 1000000000 ORDER BY bid_amount DESC");
    
    Code (markup):
     
    mastermunj, Nov 30, 2009 IP
  3. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #3
    Possible that desc is char therefore quotes are required
    ORDER BY bid_amount DESC = '" . mysql_real_escape_string($_GET['show_results'])."'");
    Code (markup):
     
    javaongsan, Nov 30, 2009 IP
  4. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #4
    DESC is an indication to order the result in descending order of bid_amount.

    Order by should be the last clause in any query.
    If at all he is trying to limit the result then a LIMIT clause should be added at end.

     
    mastermunj, Nov 30, 2009 IP
  5. chrishawkins

    chrishawkins Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I've done everything possible... I did what you told me to and I even followed other advice to add or trigger_error (mysql_error();

    Now, I get this error;

    Parse error: syntax error, unexpected ';' in C:\VertrigoServ\www\test\test.php on line 26

    This is the code now;

    <center>
    <form name="form1" method="post" action="">
    <label>
    <input type="submit" name="update" id="update" value="Update Records">
    </label>
    </form>
    <form name="form2" method="get" action="test.php?show_results=1">
    <label>
    <input type="submit" name="show_results" id="show_results" value="Show Results">
    </label>
    </form>
    <?php
    $con = mysql_connect("localhost","username","password");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("my_db", $con);
    
    if(isset($_GET['show_results']) && $_GET['show_results'] == 1){
    $res = mysql_query("SELECT *, probid_users.name AS name,
    probid_winners.winner_id AS winnerid
    FROM probid_winners
    left join probid_users ON probid_users.user_id = probid_winners.buyer_id
    WHERE probid_winners.payment_status='confirmed' AND printed='' AND buyer_id BETWEEN 2 AND 1000000000 ORDER BY bid_amount DESC"); mysql_real_escape_string($_GET['show_results'] or trigger_error (mysql_error());
    }
    
    echo "<table border='5'>
    <tr>
    <th>Name</th>
    <th>Auction ID</th>
    <th>Bid Amount</th>
    <th>Deposit Status</th>
    </tr>";
    
    while($row = mysql_fetch_array($res))
    {
    echo "<tr>";
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['auction_id'] . "</td>";
    echo "<td>" . $row['bid_amount'] . "</td>";
    echo "<td>" . $row['payment_status'] . "</td>";
    echo "<td><form1=\"update_row\" method=\"post\" action=\"test.php?id=".$row['winnerid']."\">
    <input type=\"hidden\" name=\"updating_row\" value=\"".$row['winnerid']."\" />
    <input type=\"submit\" name=\"submit_update\" value=\"Remove\" />
    </form>";
    echo "</tr>";
    
    if(isset($_POST['updating_row'])){
    $res = mysql_query("UPDATE probid_winners SET printed='printed' WHERE probid_winners.winner_id = " . mysql_real_escape_string($_POST['updating_row'] or trigger_error (mysql_error());
    }
    }
    echo "</table>";
    
    mysql_close($con);
    ?> 
    </center>
    PHP:
     
    Last edited: Nov 30, 2009
    chrishawkins, Nov 30, 2009 IP
  6. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #6
    Why do you have to add following line in code?
    What is the significance of $_GET['show_results']??

    
    mysql_real_escape_string($_GET['show_results'] or trigger_error (mysql_error());
    
    Code (markup):
     
    mastermunj, Nov 30, 2009 IP
  7. chrishawkins

    chrishawkins Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I am using the get method to show the results of the database table, probid_winners...

    What I am doing here is this;

    I am creating a script (or trying to) that will pull information from the table probid_winners. The information will be shown when I click the Show Results button. The information that is shows will only be show if it meets the conditions of payment_status='confirmed' AND printed=''.

    Once the information is shown on the page, I will print them out and then update the printed column in probid_winners to the value of printed by clicking the update records button.

    Now, when I click the show results button again, I will not get the same records as last time, due to the fact that updating the printed column will not meet the conditions of payment_status='confirmed' AND printed=''.

    Let me know if you understand what I am trying to do here and if you have any suggestions for me.

    Thanks in advance,
     
    chrishawkins, Nov 30, 2009 IP
  8. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #8
    Put the query I have given and it should work fine, if at all it gives error, let me know..

    
    res = mysql_query("SELECT *, probid_users.name AS name,
    probid_winners.winner_id AS winnerid
    FROM probid_winners
    left join probid_users ON probid_users.user_id = probid_winners.buyer_id
    WHERE probid_winners.payment_status='confirmed' AND printed='' AND buyer_id BETWEEN 2 AND 1000000000 ORDER BY bid_amount DESC");
    
    Code (markup):
     
    mastermunj, Nov 30, 2009 IP
  9. chrishawkins

    chrishawkins Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Yes, this is the error;

    Parse error: syntax error, unexpected ';' in C:\VertrigoServ\www\test\test.php on line 51

    Just to make sure, this is now what my code looks like;

    <form name="form1" method="post" action="">
    <label>
    <input type="submit" name="update" id="update" value="Update Records">
    </label>
    </form>
    <form name="form2" method="get" action="test.php?show_results=1">
    <label>
    <input type="submit" name="show_results" id="show_results" value="Show Results">
    </label>
    </form>
    <?php
    $con = mysql_connect("localhost","username","password");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("my_db", $con);
    
    if(isset($_GET['show_results']) && $_GET['show_results'] == 1){
    $res = mysql_query("SELECT *, probid_users.name AS name,
    probid_winners.winner_id AS winnerid
    FROM probid_winners
    left join probid_users ON probid_users.user_id = probid_winners.buyer_id
    WHERE probid_winners.payment_status='confirmed' AND printed='' AND buyer_id BETWEEN 2 AND 1000000000 ORDER BY bid_amount DESC");
    }
    
    echo "<table border='5'>
    <tr>
    <th>Name</th>
    <th>Auction ID</th>
    <th>Bid Amount</th>
    <th>Deposit Status</th>
    </tr>";
    
    while($row = mysql_fetch_array($res))
    {
    echo "<tr>";
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['auction_id'] . "</td>";
    echo "<td>" . $row['bid_amount'] . "</td>";
    echo "<td>" . $row['payment_status'] . "</td>";
    echo "<td><form1=\"update_row\" method=\"post\" action=\"test.php?id=".$row['winnerid']."\">
    <input type=\"hidden\" name=\"updating_row\" value=\"".$row['winnerid']."\" />
    <input type=\"submit\" name=\"submit_update\" value=\"Remove\" />
    </form>";
    echo "</tr>";
    
    if(isset($_POST['updating_row'])){
    $res = mysql_query("UPDATE probid_winners SET printed='printed' WHERE probid_winners.winner_id = " . mysql_real_escape_string($_POST['updating_row'] or trigger_error (mysql_error());
    }
    }
    echo "</table>";
    
    mysql_close($con);
    ?> 
    PHP:
     
    chrishawkins, Nov 30, 2009 IP
  10. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #10
    Change code on line #50 as below..

    
    $res = mysql_query("UPDATE probid_winners SET printed='printed' WHERE probid_winners.winner_id = " . mysql_real_escape_string($_POST['updating_row']))  or trigger_error (mysql_error());
    
    Code (markup):
     
    mastermunj, Nov 30, 2009 IP
  11. chrishawkins

    chrishawkins Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    No more Parse error's, only;

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\VertrigoServ\www\test\test.php on line 37
     
    chrishawkins, Nov 30, 2009 IP
  12. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #12
    try following code..

    basically, you have defined $res inside if and you are using it outside if.. that can cause trouble..

    
    <form name="form1" method="post" action="">
    <label>
    <input type="submit" name="update" id="update" value="Update Records">
    </label>
    </form>
    <form name="form2" method="get" action="test.php?show_results=1">
    <label>
    <input type="submit" name="show_results" id="show_results" value="Show Results">
    </label>
    </form>
    <?php
    $con = mysql_connect("localhost","username","password");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("my_db", $con);
    
    if(isset($_GET['show_results']) && $_GET['show_results'] == 1){
    $res = mysql_query("SELECT *, probid_users.name AS name,
    probid_winners.winner_id AS winnerid
    FROM probid_winners
    left join probid_users ON probid_users.user_id = probid_winners.buyer_id
    WHERE probid_winners.payment_status='confirmed' AND printed='' AND buyer_id BETWEEN 2 AND 1000000000 ORDER BY bid_amount DESC");
    
    
    echo "<table border='5'>
    <tr>
    <th>Name</th>
    <th>Auction ID</th>
    <th>Bid Amount</th>
    <th>Deposit Status</th>
    </tr>";
    
    while($row = mysql_fetch_array($res))
    {
    echo "<tr>";
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['auction_id'] . "</td>";
    echo "<td>" . $row['bid_amount'] . "</td>";
    echo "<td>" . $row['payment_status'] . "</td>";
    echo "<td><form1=\"update_row\" method=\"post\" action=\"test.php?id=".$row['winnerid']."\">
    <input type=\"hidden\" name=\"updating_row\" value=\"".$row['winnerid']."\" />
    <input type=\"submit\" name=\"submit_update\" value=\"Remove\" />
    </form>";
    echo "</tr>";
    
    if(isset($_POST['updating_row'])){
    $res = mysql_query("UPDATE probid_winners SET printed='printed' WHERE probid_winners.winner_id = " . mysql_real_escape_string($_POST['updating_row'] or trigger_error (mysql_error());
    }
    }
    echo "</table>";
    }
    mysql_close($con);
    ?>
    
    Code (markup):
     
    mastermunj, Nov 30, 2009 IP
  13. chrishawkins

    chrishawkins Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Let me show you something real quick. The code below is the code I began with, and it works perfectly. It does just as I need it to do. However, I did not start getting these errors until I tried to add 2 buttons to the code. One to show the results instead of the results just showing when I navigate to the script, and the second button to update the printed column to the value of printed, so the script would not update the column if I were to accidentally refresh the page.

    The buttons are what is getting me stuck with all of these errors. Do you have any other suggestions to add buttons to perform these functions?

    Like I said, the below code works perfectly;

      <?php
    $con = mysql_connect("localhost","username","password");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("my_db", $con);
    
    $result = mysql_query("SELECT *, probid_users.name AS name,
    probid_winners.winner_id AS winnerid 
    FROM probid_winners 
    left join probid_users ON probid_users.user_id = probid_winners.buyer_id
    WHERE probid_winners.payment_status='confirmed' AND printed='' AND buyer_id BETWEEN 2 AND 1000000000 ORDER BY bid_amount DESC");
    
    echo "<table border='5'>
    <tr>
    <th>Name</th>
    <th>Auction ID</th>
    <th>Bid Amount</th>
    <th>Deposit Status</th>
    </tr>";
    
    while($row = mysql_fetch_array($result))
      {
      echo "<tr>";
      echo "<td>" . $row['name'] . "</td>";  
      echo "<td>" . $row['auction_id'] . "</td>";
      echo "<td>" . $row['bid_amount'] . "</td>";
      echo "<td>" . $row['payment_status'] . "</td>";
      echo "</tr>";
    $res = mysql_query("UPDATE probid_winners SET printed='printed' WHERE probid_winners.winner_id = " . $row["winnerid"]);
    
      }
    echo "</table>";
    
    
    
    mysql_close($con);
    ?> 
    PHP:
     
    chrishawkins, Nov 30, 2009 IP
  14. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #14
    your new code is fine except that it needs to be organized nicely.

    just go step by step and add debugging at each step to analyze the problem..
     
    mastermunj, Nov 30, 2009 IP
  15. chrishawkins

    chrishawkins Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Thanks again for all of your help...
     
    chrishawkins, Nov 30, 2009 IP
  16. chrishawkins

    chrishawkins Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    What would it take to see if you could get me the correct coding for a show results button that will show the results from the first query and am update button that will update the printed columns in the second query, cause I am at my whits end and don't know if I can figure this out...
     
    chrishawkins, Nov 30, 2009 IP
  17. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #17
    Check with following code...

    
    <?php
    $con = mysql_connect("localhost","username","password");
    if (!$con)
    {
    	die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("my_db", $con);
    
    if(isset($_GET['show_results']) && $_GET['show_results'] == 1)
    {
    	$res = mysql_query("SELECT *, probid_users.name AS name, probid_winners.winner_id AS winnerid
    	FROM probid_winners left join probid_users ON probid_users.user_id = probid_winners.buyer_id
    	WHERE probid_winners.payment_status='confirmed' AND printed='' AND buyer_id BETWEEN 2 AND 1000000000 ORDER BY bid_amount DESC");
    	
    	
    	echo "<table border='5'>
    	<tr>
    	<th>Name</th>
    	<th>Auction ID</th>
    	<th>Bid Amount</th>
    	<th>Deposit Status</th>
    	</tr>";
    	
    	while($row = mysql_fetch_array($res))
    	{
    		echo "<tr>";
    		echo "<td>" . $row['name'] . "</td>";
    		echo "<td>" . $row['auction_id'] . "</td>";
    		echo "<td>" . $row['bid_amount'] . "</td>";
    		echo "<td>" . $row['payment_status'] . "</td>";
    		echo "<td><form1=\"update_row\" method=\"post\" action=\"test.php?id=".$row['winnerid']."\">
    		<input type=\"hidden\" name=\"updating_row\" value=\"".$row['winnerid']."\" />
    		<input type=\"submit\" name=\"submit_update\" value=\"Remove\" />
    		</form>";
    		echo "</tr>";
    	}
    	echo "</table>";
    }
    
    if(isset($_POST['updating_row']))
    {
    	$res = mysql_query("UPDATE probid_winners SET printed='printed' WHERE probid_winners.winner_id = " . mysql_real_escape_string($_POST['updating_row'] or trigger_error (mysql_error());
    }
    
    mysql_close($con);
    ?>
    
    <form name="form1" method="post" action="">
    <label>
    <input type="submit" name="update" id="update" value="Update Records">
    </label>
    </form>
    <form name="form2" method="get" action="test.php?show_results=1">
    <label>
    <input type="submit" name="show_results" id="show_results" value="Show Results">
    </label>
    </form>
    
    
    Code (markup):
     
    mastermunj, Nov 30, 2009 IP
  18. chrishawkins

    chrishawkins Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #18
    That code was pieced together by someone else. If possible, I'm sure there would be less chance for error if you would like to use the below code to add buttons to. Only if you wouldn't mind;

    <?php
    $con = mysql_connect("localhost","username","password");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("my_db", $con);
    
    $result = mysql_query("SELECT *, probid_users.name AS name,
    probid_winners.winner_id AS winnerid
    FROM probid_winners
    left join probid_users ON probid_users.user_id = probid_winners.buyer_id
    WHERE probid_winners.payment_status='confirmed' AND printed='' AND buyer_id BETWEEN 2 AND 1000000000 ORDER BY bid_amount DESC");
    
    echo "<table border='5'>
    <tr>
    <th>Name</th>
    <th>Auction ID</th>
    <th>Bid Amount</th>
    <th>Deposit Status</th>
    </tr>";
    
    while($row = mysql_fetch_array($result))
      {
      echo "<tr>";
      echo "<td>" . $row['name'] . "</td>";  
      echo "<td>" . $row['auction_id'] . "</td>";
      echo "<td>" . $row['bid_amount'] . "</td>";
      echo "<td>" . $row['payment_status'] . "</td>";
      echo "</tr>";
    $res = mysql_query("UPDATE probid_winners SET printed='printed' WHERE probid_winners.winner_id = " . $row["winnerid"]);
    
      }
    echo "</table>";
    
    
    
    mysql_close($con);
    ?>
    PHP:
     
    chrishawkins, Nov 30, 2009 IP
  19. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #19
    did you try the code i posted last?

    I mean, if you can do little learning and try doing, it will be good for your reference in future..
     
    mastermunj, Dec 1, 2009 IP