Supplied argument is not a valid MySQL result resource

Discussion in 'PHP' started by goneinsane, Jul 7, 2010.

  1. #1
    I'm getting this warning when saving the data in a form. It send an email to administrator which it should but it also gives the user this error.

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /inc/backend.php on line 40

    LINE 40 says: $row=mysql_fetch_array($sql);

    Any idea what I'm doing wrong here?
     
    goneinsane, Jul 7, 2010 IP
  2. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #2
    Hard to say, could you please post the whole php source here?
     
    Rainulf, Jul 7, 2010 IP
  3. goneinsane

    goneinsane Well-Known Member

    Messages:
    303
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    130
    #3
    <?php
    
    define('_VALID_','1');
    
    
    if ( !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' ) {
    
    require 'database.class.php';
    
    $db = new database();
    $id = (int) $_POST['id'];
    
    switch ($_REQUEST['action']) {
    
    	case 'get_admins':
    		$rs = $db->query("SELECT `ID` as id, CONCAT(`fname`, ' ', `lname`) as name, `type` FROM ".DB_TABLE."users WHERE (`type` = 1 OR `type` = 2 OR `type` = 3) AND `fname` LIKE '%%".$_GET["q"]."%%' ORDER BY `fname` DESC LIMIT 10");
    		
    		while($obj = mysql_fetch_object($rs))
    		    $arr[] = $obj;
    
    		echo json_encode($arr);
    
    	break;	
    	
    	case 'update':
    		$data = Array (
    			'status' => $_POST['sta'],
    			'DEPARTMENT_ID' => $_POST['dep'],
    			'priority' => $_POST['pri'],
    			'modified' => 'NOW()'
    		);
    		$update = $db->query_update("tickets",$data,"`ID`='{$id}'");
    		$delete = $db->query("DELETE FROM ".DB_TABLE."assigned WHERE `TICKET_ID`='{$id}';");
    		
       		$string = substr($_POST['asi'],0,-1);
       		$assigned = explode(",",$string);
       		foreach ($assigned as $as){
       			$db->query("INSERT INTO ".DB_TABLE."assigned (`USER_ID`,`TICKET_ID`) VALUES ('$as','$id')");
    				 $sql=mysql_query('SELECT email FROM `support_users` WHERE ID='.$as);
    				 $row=mysql_fetch_array($sql);
    				 
    				 $frmmail='email@mydomain.com';
    				 $tomailid=$row['email'];
    				 $bodymessage='A new ticket has been assigned to you. Please log in to reply to it.';
    				 $subjectuser='A ticket has been assigned to you';
    				 $headers  = 'MIME-Version: 1.0' . "\r\n";
    				 $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    				 $headers .= 'From: '.$frmmail.' <'.$frmmail.'>'. "\r\n";
    				 mail($tomailid,$subjectuser,$bodymessage,$headers);
    			}
    			
    			
    		
    		if ($update)
    			echo 'Ticket Successfully Modified!';
    		else
    			echo 'Ticket Unsuccessfully Modified!';
    
    	break;
    
    	case 'delete':
    		$delete = $db->query("DELETE FROM ".DB_TABLE."tickets WHERE `ID`='{$id}';");
    		$delete2 = $db->query("DELETE FROM ".DB_TABLE."tickets WHERE `parent`='{$id}';");
    		$delete3 = $db->query("DELETE FROM ".DB_TABLE."assigned WHERE `TICKET_ID`='{$id}';");
    		
    		if ($delete)
    			echo 'Ticket Successfully Deleted!';
    		else
    			echo 'Ticket Unsuccessfully Deleted!';
    
    	break;
    	
    	case 'delete_user':
    		$rows = $db->fetch_all_array("SELECT `ID` FROM FROM ".DB_TABLE."tickets WHERE `by`='{$id}' ");
    		if ($rows) {
    			foreach ($rows as $row)
    				$delete_ticket = $db->query("DELETE FROM ".DB_TABLE."tickets WHERE `parent`='{$row['ID']}';");
    		}
    		$delete = $db->query("DELETE FROM ".DB_TABLE."users WHERE `ID`='{$id}';");
    		$delete2 = $db->query("DELETE FROM ".DB_TABLE."tickets WHERE `parent`='{$id}';");
    		$delete3 = $db->query("DELETE FROM ".DB_TABLE."tickets WHERE `by`='{$id}';");
    		
    		if ($delete)
    			echo 'User Successfully Deleted!';
    		else
    			echo 'User Unsuccessfully Deleted!';
    
    	break;
    
    	case 'delete_department':
    		$delete = $db->query("DELETE FROM ".DB_TABLE."departments WHERE `ID`='{$id}';");
    		$delete2 = $db->query("DELETE FROM ".DB_TABLE."user_department WHERE `DEPARTMENT_ID`='{$id}';");
    		if ($delete && $delete2)
    			echo 'Department Successfully Deleted!';
    		else
    			echo 'Department Unsuccessfully Deleted!';
    
    	break;
    
    	case 'update_user':
    		$delete = $db->query("DELETE FROM ".DB_TABLE."departments WHERE `ID`='{$id}';");
    		$delete2 = $db->query("DELETE FROM ".DB_TABLE."user_department WHERE `DEPARTMENT_ID`='{$id}';");
    		if ($delete && $delete2)
    			echo 'Department Successfully Deleted!';
    		else
    			echo 'Department Unsuccessfully Deleted!';
    
    	break;
    
    	default:
    		echo 'did not seem to work';
    	break;
    }
    
    } else {
    	echo 'Error';
    	exit;
    }
    
    ?>
    Code (markup):
     
    goneinsane, Jul 8, 2010 IP
  4. atpaz

    atpaz Member

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #4
    hi...
    you can print what kind of mySQL error by adding mysql_error() function on line 39 like this:
    this will print specific mysql error so you can trace which SQL query that trigger the error.
     
    atpaz, Jul 9, 2010 IP
  5. goneinsane

    goneinsane Well-Known Member

    Messages:
    303
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    130
    #5
    It says:
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
     
    goneinsane, Jul 9, 2010 IP
  6. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #6
    I would echo out the SELECT statement to see exactly what is trying to be executed. So instead of this:

    
    $sql=mysql_query('SELECT email FROM `support_users` WHERE ID='.$as);
    
    PHP:
    use this:

    
    echo 'SELECT email FROM `support_users` WHERE ID='.$as;
    // $sql=mysql_query('SELECT email FROM `support_users` WHERE ID='.$as);
    
    PHP:
    My guess it it has to do with the single quotes around 'support_users', the value of $as isn't what you expect (possibly a string) or no results are being returned from the query.

    If you still have trouble, post what gets echoed for your SELECT statement.
     
    plog, Jul 9, 2010 IP
  7. atpaz

    atpaz Member

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #7
    ok, that because the sql query is
    SELECT email FROM `support_users` WHERE ID=
    PHP:
    (you have an empty value for the $as variable).

    on line 35 and 36, your script expect coma separated input data from the asi form field. :)
     
    atpaz, Jul 9, 2010 IP