Jump to content

mysqli_real_escape_string() throwing error


n1concepts
Go to solution Solved by Barand,

Recommended Posts

Hi,

 

Can someone take a look at the two snippets of code and tell me what am I doing wrong to cause the following error?

 

Error: Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, string given....

 

Here's the code (flies - there are two - one the db_connection string and the other the script attempting to perform the insert)

 

<?php
// db connection string that resides in 'includes' folder

function execute($query) {
$db_con = mysqli_connect("localhost","db_user","passxxx");
mysqli_select_db($db_con, "mytable_name");
$res=mysqli_query($db_con,$query) or die(mysqli_error($db_con) . '<br />' . $query) ;
mysqli_close($db_con);
return $res;
}

?>

I know this is the (old) procedure way but I don't have the time to revamp the code to OOP - will update to PDO in coming weeks; for now, i need this set of code to work (if possible).

 

 

 

Now, here's the portion of the code where I want to escape string - giving two as example - which throws the (above) error.

 

<?php

require("includes/db_con.php");

$v1 = $_GET['v1'];\
$v2 = $_GET['v2'];
$v3 = $_GET['v3'];
// I even tried adding mysqli_real_escape_string($db_con,$v1) - as an example here but still error
$v1 = mysqli_real_escape_string(mysqli $db_con,$v1);
-----------

// Below is the function to execute MySQL Insert which pulls from the include file (db_con.php) - this is where i have the 'mysqli_real_ecape_string() defined for $v1, $v2, $v3 currently but still throwing error:

execute("insert into logs (logid,value1,value2,value3) values ('',,'$'".mysqli_real_escape_string($v1).'".mysqli_real_escape_string($v2)."','".mysqli_real_escape_string($v3)."')");

?>

 

Note: I know the syntax is mysqlI_real_escape_string(mysqli $link, string) - when i add 'mysqli $link - code breaks....
Issue: I switched current code from mysql to mysqli and need to launch; once up and running, i plan to go back and revise code to PDO which, I know the prefer choice - just under gun to get up and running so switched to mysqli just in interim.

Any advise to get provided snippet code running where insert possible, much appreciated - thx!

Edited by n1concepts
Link to comment
Share on other sites

execute("insert into logs (logid,value1,value2,value3) values ('',,'".mysqli_real_escape_string($v1).'","'.mysqli_real_escape_string($v2)."','".mysqli_real_escape_string($v3)."')");

 

I missed a comma between $v1 and $v2 - however the issues still remains: Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given

 

This is starting to bug me as it shouldn't be this difficult - appreciate another set of eyes to identify issue (thx!)

Link to comment
Share on other sites

  • Solution

Don't connect whenever you run a query - connecting takes about 100 times longer than most queries.

 

Variable scope - $db_con only exists within the function and not available outside.

 

Connect at the beginning of your script than pass the connection to functions that need it via the function parameters

Link to comment
Share on other sites

  • 3 weeks later...

Hi All,

 

I have query problem for the below point, So can some help us on this, as we are trying this one for the past 2 weeks.

 

exclude sunday's and time should start from 8.00Am to 6.00PM in mysql query

 

And The query we have is

 

SELECT B.ProblemID, A.problemID,A.EnclID as A_ENCLID,
B.EnclID as B_EnclID, B.EnclSubmittedTime,
(SPLIT_STR(B.enclname,"_", 1)),
B.EnclComment,B.EnclSubmittedTime,
COALESCE((TIMEDIFF(A.EnclSubmittedTime, B.EnclSubmittedTime))) AS Time_DIFF
FROM TABLE B
LEFT JOIN TABLE A ON B.EnclID < A.EnclID
AND A.problemID = B.ProblemID
and A.enclname not like 'Enclosure%'
where B.enclname not like 'Enclosure%'
and A.EnclSubmittedTime not in (
Give_Me_Dates_Days(1, B.EnclSubmittedTime, A.EnclSubmittedTime)
)
Group by B.ProblemID, B.EnclID
order by B.ProblemID ASC

 

If any quries, please reply to this, we will response you.

 

Thanks In advance.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.