This question already has an answer here:
mysqli_real_escape_string() expects exactly 2 parameters, 1 given Fatal Error [duplicate]
(1 answer)
Closed 6 years ago.
Here is my code:
include "db_conx.php";
$sql = mysqli_query("INSERT INTO table(column) VALUES('" . mysqli_real_escape_string($var) . "')");
if ($sql) {echo "connection successful";
} else {
echo "failure";
}
It returns these errors:
Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given
Warning: mysqli_query() expects at least 2 parameters, 1 given
I tried using PDO but that didn't work either...
First Parameter is mysql connection link identifier, and second is string For more details, you can visit this link : http://in2.php.net/manual/en/mysqli.real-escape-string.php.
include "db_conx.php";
$sql = mysqli_query(pass_your_connection_identifier_here ,"INSERT INTO table(column) VALUES('" . mysqli_real_escape_string(pass_your_connection_identifier_here, $var) . "')");
if ($sql) {echo "connection successful";
} else {
echo "failure";
}
You are missing connection identifier in both mysqli_real_escape_string() and mysqli_query()
change your code to mysqli_query($connection,$sql) and mysqli_real_escape_string($connection,$string)
You are missing connection variable at both the lines that's why you are facing issues :
Try to replace your code with this :
//$con // it is your connection variable
include "db_conx.php";
$sql = mysqli_query($con,"INSERT INTO table(column) VALUES('" . mysqli_real_escape_string($con,$var) . "')");
if ($sql) {echo "connection successful";
} else {
echo "failure";
}
Related
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
I'm new to MySql so forgive me for any stupid errors but Basically i'm making a blog and i have a table which has 4 columns: post_id, post_text, post_user and post_date. I am unable to get my code to insert data into MySql, I'd be greatfull if you could have a look and let me know where I'm going wrong:
<?php
$host = "localhost";
$user="user";
$password="*********";
$db="myData";
$user_id = $_SESSION['user_id'];
ini_set('display_errors', 1); error_reporting(-1);
//can't find the user_id
$con=mysql_connect($host,$user,$password,"myData","posts");
if( $con === FALSE ) {
die('mysql connection error: '.mysql_error());
}
$post_text = mysqli_real_escape_string($con,$_POST["blog_entrance"]);
$post_date = mysqli_real_escape_string($con,date("y.m.d"));
$query = "INSERT INTO posts (post_text, post_user) VALUES ('$post_text','$user_id','$post_date')";
mysql_query($query,$con) or die('Can\'t post data atm: ' . mysql_error());
mysql_close($host,$user,$password,"myData","posts");
header('Location: ../index.php');
?>
the error message is this
"Warning: mysql_connect() expects parameter 5 to be long, string given in /media/sdc1/Documents/Coding/Blog/www/core/submit.php on line 15 Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in /media/sdc1/Documents/Coding/Blog/www/core/submit.php on line 21 Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in /media/sdc1/Documents/Coding/Blog/www/core/submit.php on line 22 Notice: Undefined variable: user_id in /media/sdc1/Documents/Coding/Blog/www/core/submit.php on line 25 Warning: mysql_query() expects parameter 2 to be resource, null given in /media/sdc1/Documents/Coding/Blog/www/core/submit.php on line 26 Can't post data atm:"
I can see that all these errors seem to stem from the mysql_connect function, please could you tell me the error of my ways?
Also i'm unsure as to whether to use mysql or mysqli????
Read about mysql_connect() function: here
You must use:
$con = mysql_connect($host,$user,$password);
Anyway, mysql_* functions is old, monstrous and deprecated. Use PDO or mysqli
I thought i'd post my final code so future users can have a look at it:
$con= new mysqli($host,$user,$password,$db);
if ($con->connect_errno) {
die("Failed to connect to MySQL: (" . $con->connect_errno . ") " . $con->connect_error);
}
$post_text = mysqli_real_escape_string($con,$_POST["blog_entrance"]);
$post_date = mysqli_real_escape_string($con,date("y.m.d"));
if (!$con->query("INSERT INTO posts(post_text, post_user, post_date) VALUES ('$post_text',$user_id','$post_date')")) {
die("Multi-INSERT failed: (" . $con->errno . ") " . $con->error);
}
This question already has answers here:
How do I get the last inserted ID of a MySQL table in PHP?
(16 answers)
Closed 9 years ago.
I would like to return the ID "setsId" after I insert values in the the Sets table.
This is the error I receive: Call to undefined function last_insert_id().
Here is my code:
if($_POST[reps]=="")
{
echo "Record can not be added. All fields are mandatory";
echo '<br>';
}
else
{
$con = mysqli_connect(); //removed for privacy
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO Sets (exerciseId, bandId, reps, notes)
VALUES ('$_POST[exerciseId]',
'$_POST[bandId]',
'$_POST[reps]',
'$_POST[notes]')";
$sId=last_insert_id();
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo '<div id ="error">';
echo 'SUCCESS!';
echo '<br>';
echo "1 record added";
echo '<br>';
echo '</div>';
mysqli_close($con);
}
include ('ptfooter.html');
?>
Could someone please let me know how I am using the function last_insert_id() incorrectly so that it would cause an error?
last_insert_id is the name of a MySQL function. The corresponding mysqli function is called mysqli_insert_id. Use that instead.
But you can't call that function if you haven't executed your insert query yet. Make sure that your insert is executed (and succeeded) before you call that function.
This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 3 years ago.
I have written a mysql_query and it is not working.
I want to know the problem via mysqli_error() however it gives the following error mysqli_error() expects parameter 1 to be mysqli
my mysql code is as follows:
$connect = mysqli_connect("localhost","root", "", "tomuman");
$query = mysqli_query($connect, "SELECT id, to FROM messages WHERE read='0'");
and mysqli_error as follows:
echo mysqli_error($query);
What could cause this problem?
You should not use $query as a parameter for mysqli_error()
use
echo mysqli_error($connect);
Just after trying to connect you can also check for specific connection errors with:
if (mysqli_connect_errno()) {
echo mysqli_connect_error();
exit();
}
Add following code in your query to get the mysql error message.
or die (mysqli_error($connect))
as like below
$query = mysqli_query($connect, "SELECT id, to FROM messages WHERE read='0'") or die (mysqli_error($connect));
if ($query) {
echo "success";
}
else {
echo("Error description: " . mysqli_error($connect));
}
This question already has answers here:
How to change mysql to mysqli?
(12 answers)
Closed 1 year ago.
I need some help in converting my script from using mysql to mysqli I have been trying for ever to do this and keep getting errors I have read articles on how to do this and still can get it to work.
Here is the original working script below.
<?php
require_once ('./mysqli_connect.php'); // Connect to the db.
if (isset($_POST['submitted'])) {
if ($dbc = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD)) { // Make the connnection.
if (!mysql_select_db (DB_NAME)) { // If it can't select the database.
// Handle the error.
trigger_error("Could not select the database!\n<br />MySQL Error: " . mysql_error());
exit();
} // End of mysql_select_db IF.
} else { // If it couldn't connect to MySQL.
// Print a message to the user, include the footer, and kill the script.
trigger_error("Could not connect to MySQL!\n<br />MySQL Error: " . mysql_error());
exit();
} // End of $dbc IF.
// grab the tag
$tag = mysql_real_escape_string($_POST['tag']);
// see if the tag already exists and potentially what the current count is
$query = "SELECT id, count, page FROM tags WHERE tag='$tag'";
$result = mysql_query($query);
//--if there is a row, that means the tag exists
if(mysql_num_rows($result))
{
//--pull out the tag ID and the current count and increment by one.
$tag_info = mysql_fetch_array($result);
$tag_info_id = $tag_info["id"];
$tag_info_count = $tag_info["count"] + 1;
//--update the table with the new count
$sql_update_cnt = "UPDATE tags SET count='$tag_info_count'
WHERE id='$tag_info_id'";
mysql_query($sql_update_cnt);
echo "$tag now with $tag_info_count instances";
}
else
{
// tag is not there, so insert a new instance
$query = "INSERT INTO tags (tag, count) VALUES ('$tag', 1)";
if (!mysql_query($query, $dbc))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
}
mysql_close($dbc);
}
?>
Here is the errors I keep getting.
Warning: mysqli_select_db() expects exactly 2 parameters, 1 given on line 13
Warning: mysqli_error() expects exactly 1 parameter, 0 given on line 16
If you're going to go through the hassle, I would really recommend you consider PDO instead.
It's all a matter of changing mysql to mysqli.
For example, you could call mysqli_query just like you do mysql_query here.
When you call these functions, you need to pass in the database reference as the first parameter.
See the mysqli function reference here
It's been a while since I've done a mysql->mysqli conversion (like 2 or 3 years), but IIRC there are some functions for which you also have to reverse the parameter order. Isn't that lovely?
This question already has answers here:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result [duplicate]
(6 answers)
Closed 9 years ago.
I get this error when I use this code for people to input data. The submit form won't be listed because it's not useful in this circumstance:
function some_more_custom_content() {
$output="<BR>";
ob_start();
if ($_REQUEST['code'] != "") {
$code = $_REQUEST['code'];
$query="INSERT INTO `fc` (`code`,`datetime`) values ('" . mysql_real_escape_string($code) . "', now())";
$result=mysql_query($query);
while ($fetch_array = mysql_fetch_array($result)) {
$seconds = time() - strtotime($fetch_array["datetime"]);
if ((time() - $entry['datetime']) < 60*60) {
echo ("The code " . htmlentities($code) ." was updated less than an hour ago.");
} else {
echo ("Inserted " . htmlentities($code) ." into the top.");
}
}
}
Any idea why?
INSERT statements don't return a resource, they return TRUE on success or FALSE on failure. Thus there is no resource for mysql_fetch_array() to operate on.
(This is one of the main reasons why people complain about PHP -- its semantics are inconsistent at best.)
Adding to Meredith Answer.
You shoul use
if($result!==FALSE) {
// the insert was ok
} else {
//the inser failed
}
change:
$result=mysql_query($query);
with:
$result=mysql_query($query) or die(mysql_error());
you will see the error.