Hey everyone, I'm utilizing a few PHP functions I made and for some reason, I keep getting errors in the queries. In particular this error:

expects parameter 1 to be resource, boolean given on line 66

I'm struggling and I can't seem to find an answer. With that said I'm hoping to find some help here 😃.

SQL Lib

<?PHP
/*
	SQL LIB
*/

/*
FUNCTION Guide:

S_ERROR({INT})					<- Gives an error for the {INT} value
S_FILTER({QUERY})				<- Filters {QUERY} Preventing SQL Injections
S_CONNECT({H},{B},{U},{P})		<- Connects to the SQL database
S_OBJECT({QUERY}, {TABLE})		<- Runs the {QUERY} and returns the value of {TABLE}
S_QUERY({QUERY})				<- Executes and filters QUERY
S_ARRAY({QUERY})				<- Returns an array from a query
S_INSERT({B},{H},{A1},{A2})		<- Formats insert queries with {A1} & {A2} as arrays
*/


FUNCTION s_error($_int)
{
	$error = array(	"[SQL] Connection Error",
					"[SQL] Database Select Error",
					"[SQL] Error in SQL string (<i>we filter</i>)");

return $error[$_int];
}

FUNCTION s_filter($_q)
{
	return	$_q = stripslashes(mysql_real_escape_string($_q));
}

FUNCTION s_connect($host, $base, $user, $pass)
{
	$host = mysql_connect($host, $user, $pass);
		if(!$host)
		{
			exit(s_error(0));
		}
		$base = mysql_select_db($base, $host);
			if(!$base)
			{
				exit(s_error(1));
			}
				else
			{
				return TRUE;
			}
}

FUNCTION s_object($sql, $out)
{
	$q = s_array($sql);
	return $q[$out];
}


FUNCTION s_query($_q)
{
	return mysql_query(s_filter($_q)) or die(s_error(3));
}

FUNCTION s_array($_q)
{
	$q = s_query($_q);
	return mysql_fetch_array($q);
}

FUNCTION s_insert($base, $table, $a1, $a2)
{
$x = 0;
	$string = "INSERT INTO `".$base."`.`".$table."` (";
		foreach($a1 as $in)
		{
			if($x != count($a1) -1)
			{
			$tables .= "`".$in."`, "; 
			$x++;
			}
				else
			{
				$tables .= "`".$in."`) VALUES (";
				$x=0;
			}
		}

	foreach($a2 as $in)
	{
		if($x != count($a2) -1)
		{
		$tables .= "'".$in."', "; 
		$x++;
		}
			else
		{
			$tables .= "'".$in."');";
		}
	}

$final = $string . $tables;
return str_replace("`NULL`", "NULL", $final);
}
?>


Testing Script:

include("connect.php");
include("sql.lib.php");
print_r(s_array("SELECT * FROM `users`"));
s_object("SELECT `blog` from `models` WHERE id =", "$_GET['id']")

Thanks for any help you guys can offer 😃!

    You should be checking to see if [man]mysql_query/man returns false and, if so, debug the failing query (e.g. via using the error message returned by the SQL server via [man]mysql_error/man).

    Also, do you realize that this:

    return	$_q = stripslashes(mysql_real_escape_string($_q));

    might as well say this:

    return $_q;

    since [man]stripslashes/man will simply undo all of the sanitizing that [man]mysql_real_escape_string/man did? Also note that the function is only appropriate for sanitizing strings - not numeric input (or any other kind for that matter).

      The query returns 1 (true), just fine. As mentioned the error seems to be with line 66 saying that it's expecting a resource but a boolean is given. This is the area I need help with. Thanks for the input on the Filtering though 😃.

        [man]mysql_query/man will never return boolean TRUE for a SELECT query. Since the error message clearly states that a boolean was passed, it must be boolean FALSE (hence you have an error in your query). Echo out the full SQL query that failed as well as the error message returned from the MySQL server.

          Also, do you "filter" the entire SQL query string? If so, that is definitely going to cause errors; you should find some very basic PHP+SQL tutorials that discuss data sanitation.

            Solved it, the problem was coming from the line 55. Thanks mate!

              Write a Reply...