Connecting Tech Pros Worldwide Help | Site Map

mysql_real_escape_string errror: expects string, resource given.

  #1  
Old June 19th, 2009, 05:17 AM
Newbie
 
Join Date: Jun 2009
Posts: 26
Hi, can anyone please help me why I got this error every I uploaded files.

Error:
Quote:
Warning: mysql_real_escape_string() expects parameter 1 to be string, resource given in c:\Inetpub\wwwroot\uploadingfiles\add_file.php on line 89

Warning: mysql_real_escape_string() expects parameter 1 to be string, resource given in c:\Inetpub\wwwroot\uploadingfiles\add_file.php on line 90

Warning: mysql_real_escape_string() expects parameter 1 to be string, resource given in c:\Inetpub\wwwroot\uploadingfiles\add_file.php on line 92
Here is the code on the said warning message:
Expand|Select|Wrap|Line Numbers
  1. # Gather all required data
  2.         $name = mysql_real_escape_string($dbLink, $_FILES['uploaded_file']['name']);
  3.         $mime = mysql_real_escape_string($dbLink, $_FILES['uploaded_file']['type']);
  4.         $size = $_FILES['uploaded_file']['size'];
  5.         $data = mysql_real_escape_string($dbLink, file_get_contents($_FILES['uploaded_file']['tmp_name']));
And because of that error the name and mime of the files I uploaded didn't appear, thats why I cannot properly download the files. Maybe because the mime/filetype is not present.

Your help will be highly appreciated.

Thanks guys..

Last edited by Atli; June 19th, 2009 at 09:20 AM. Reason: Moved from the article into it's own thread.
  #2  
Old June 19th, 2009, 09:18 AM
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,701
Provided Answers: 4

re: mysql_real_escape_string errror: expects string, resource given.


Quote:
Originally Posted by roseple View Post
Hi, can anyone please help me why I got this error every I uploaded files.
Hi.

Did you swap back to the old mysql extension, or did you just forget the "i" in the function name?

The old mysql_real_escape_string function expects the first parameter to be a string and the second one to be the mysql connection resource.
Your code has it backwards.

The improved mysqli_real_escaped_string function (note the "i" in the function name) expects the first parameter to be a mysqli object, and the second one to be a string.
Your code has the parameters right, but leaves out the "i" in the function name.
  #3  
Old June 19th, 2009, 11:10 AM
Newbie
 
Join Date: Jun 2009
Posts: 26

re: mysql_real_escape_string errror: expects string, resource given.


Hi, I'm sorry I have another question..
Can anyone know why this warning message occur:
Quote:
Warning: mysql_real_escape_string() expects parameter 1 to be string, resource given in c:\Inetpub\wwwroot\uploadingfiles\add_file.php on line 89

Warning: mysql_real_escape_string() expects parameter 1 to be string, resource given in c:\Inetpub\wwwroot\uploadingfiles\add_file.php on line 90

Warning: mysql_real_escape_string() expects parameter 1 to be string, resource given in c:\Inetpub\wwwroot\uploadingfiles\add_file.php on line 92
Here's the code on the said line.
Expand|Select|Wrap|Line Numbers
  1. $name = mysql_real_escape_string($dbLink, $_FILES['uploaded_file']['name']);
  2.         $mime = mysql_real_escape_string($dbLink, $_FILES['uploaded_file']['type']);
  3.         $size = $_FILES['uploaded_file']['size'];
  4.         $data = mysql_real_escape_string($dbLink, file_get_contents($_FILES['uploaded_file']['tmp_name']));
And because of that warning messae the name, mime and filedata is not present that's why I cannot properly download the files I uploaded.

Thank you very much in advance.
  #4  
Old June 19th, 2009, 11:57 AM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,487
Provided Answers: 9

re: mysql_real_escape_string errror: expects string, resource given.


Quote:
Originally Posted by roseple View Post
Can anyone know why this warning message occur:
because you defined the parameters in the wrong order. as stated in the manual, the string comes first and the resource comes second.
  #5  
Old June 19th, 2009, 12:02 PM
Newbie
 
Join Date: Jun 2009
Posts: 26

re: mysql_real_escape_string errror: expects string, resource given.


Can you tell me how can I do that..

Thanks
  #6  
Old June 19th, 2009, 12:04 PM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,487
Provided Answers: 9

re: mysql_real_escape_string errror: expects string, resource given.


Quote:
Originally Posted by roseple View Post
Can you tell me how can I do that..
do what? you mean to correct the error?
  #7  
Old June 20th, 2009, 02:53 PM
Newbie
 
Join Date: Jun 2009
Posts: 26

re: mysql_real_escape_string errror: expects string, resource given.


Yup, if you don't mind..

Thank you very much..
  #8  
Old June 20th, 2009, 06:49 PM
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,701
Provided Answers: 4

re: mysql_real_escape_string errror: expects string, resource given.


You have the parameters of the function in reverse order.
One would think the solution would be obvious...

The database link is supposed to go after the data.
(Or not at all. That would work to.)

Keep in mind that the example code in my article is meant for a different function, as I explained in my first post in this thread.

Take a look at the examples in the links both myself and Dormilich posted. See how the parameters are used there.
Reply