I'm Updating with mysql over ODBC Filemaker Table.
When a field contains o'reilly or example'two I get this error message:
Warning: odbc_exec(): SQL error: [FileMaker][FileMaker] FQL0001/(1:80):
There is an error in the syntax of the query., SQL state 42000 in SQLExecDirect in C:\fm_1.php on line 49
and using addslashes() does not work.
thank you!
this is my code:
<?php
$conn = odbc_connect("DSN=Server;Database=TEST;UID=odbc;PWD=1234", "odbc", "1234");
if ($conn)
echo "\nConnection established.";
else
die("\nConnection could not be established.");
$result = odbc_exec($conn, "SELECT ID_MH, MH_Name FROM myTable WHERE MH_Name LIKE '%EXAMPLE'");
while ($row = odbc_fetch_array($result)) {
$ID_MH = $row["ID_MH"];
$MH_Name = $row["MH_Name"];
// do something
$MH_Name = addslashes($MH_Name);
$update = "UPDATE myTable SET MH_Name='$MH_Name' WHERE ID_MH=" . $ID_MH;
$data_update = odbc_exec($conn, $update);
}
odbc_close($conn);
?>
Try to escape instead of using addslashes:
"UPDATE myTable SET MH_Name=\"$MH_Name\" WHERE ID_MH=" . $ID_MH;
here is the solution:
$query = 'UPDATE myTable SET MH_Name=? WHERE ID_MH=?';
$stmt = odbc_prepare ($conn, $query);
$success = odbc_execute($stmt, array($MH_Name, $ID_MH));
source:https://www.skeletonkey.com/FileMaker_11_ODBC_Drivers/
thanks!
Related
<?php
//Step1
$db = mysqli_connect('localhost','root','','form')
or die('Error connecting to MySQL server.');
//Step2
$query = "SELECT * FROM info";
mysqli_query($db, $query) or die('Error querying database.');
$result = mysqli_query($db, $query);
$row = mysqli_fetch_array($result);
while ($row = mysqli_fetch_array($result)) {
$nam=$row['name'];
$fnam=$row['father'];
$date=$row['date'];
$aadh=$row['aadhaar'];
}
1.In this code it fetches all the values that are stored in the database . But I am in need of the code for fetching the top most row from the database.
2.The query with top attribute is also not working. It gives the following error:
#1064 - 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 '1 * FROM info LIMIT 0, 30' at line 1
This is not a PHP issue, it's rather a matter of your MySQL query.
This will return everything because of the * usage.
$query = "SELECT * FROM info";
You can try this instead:
$query = "SELECT *
FROM info
WHERE unique_id = (SELECT MAX(unique_id) FROM info)";
I have made a php file from which parameters are passed through GET method..
The Problem is when I am passing paramenters it is saying:
Parameters using Following URL:
http://www.akshay.site90.net/sendlats.php?username=rakesh&lat=30.13348419&longitude=77.28685067
MySQL query failedYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' longitude=77.28685067 WHERE `username`=rakesh' at line 1
The code of MY Php file is given below please have a look:
<?php
$username = $_GET['username'];
$latitude = $_GET['latitude'];
$longitude = $_GET['longitude'];
$con = mysql_connect("mysql3.000webhost.com","a2418693_GCM","[passwordhere");
if(!$con){
die('MySQL connection failed'.mysql_error());
}
$db = mysql_select_db("a2418693_GCM",$con);
if(!$db){
die('Database selection failed'.mysql_error());
}
$sql = "UPDATE driver SET lat=$latitude, longitude=$longitude WHERE `username`=$username";
if(!mysql_query($sql, $con)){
die('MySQL query failed'.mysql_error());
}
mysql_close($con);
IMPORTANT!
Try to avoid SQL-Injection situation.
Before using these values:
$username = $_GET['username'];
$latitude = $_GET['latitude'];
$longitude = $_GET['longitude'];
...
filter, escape, prepare them in order to have safe query to your Database.
The best way is to use PDO
use this:
$sql = "UPDATE driver SET lat='$latitude', longitude='$longitude' WHERE `username`='$username'";
instead of this:
$sql = "UPDATE driver SET lat=$latitude, longitude=$longitude WHERE `username`=$username";
your variables must be quoted.
Try as below, you have missed quotes for variable $username:
$sql = "UPDATE driver SET lat=$latitude, longitude=$longitude WHERE `username`='".$username."'";
You are missing quotes for both field names and variable names :
$sql = "UPDATE driver
SET `lat` = '".$latitude."',
`longitude` = '".$longitude."'
WHERE `username` = '".$username."'";
PS: Don't forget the "." concat operator for PHP!
When connecting to a database with PHP I have been using an ODBC connection with the following query:
"SELECT * FROM TAB.LE WHERE TAB.LE.Id = 1";
Where the tablename is TAB.LE.
The code used to make the query isn't required here - but it works fine returning the correct result(s). When I use an OCI connection the same query fails:
$conn = oci_connect("username", "password", "database");
if($conn){
$query = "SELECT * FROM TAB.LE WHERE TAB.LE.Id = 1";
$stid = oci_parse($conn, $query);
$res = oci_execute($query);
if($res){
echo "success";
}
else{
echo "failed";
}
}
I constantly see failed on the screen. I am stumped as to why. The odd thing is: the tablename TAB.LE works for the ODBC connection; however, when viewed in MS Access it shows as TAB_LE. I have attempted to use this different notation in the OCI connection but to no avail.
oci_execute() Executes a statement previously returned from oci_parse().
$stid = oci_parse($conn, $query);
So change
$res = oci_execute($query);
To
$res = oci_execute($stid);
This question already has answers here:
php/mysql with multiple queries
(3 answers)
Closed 3 years ago.
I've a doubt with mysqli_query..
this is a part of my code:
$con = db_connect();
$sql= "SET foreign_key_checks = 0; DELETE FROM users WHERE username = 'Hola';";
$result = mysqli_query($con, $sql);
return $result;
I can't do the query...
If I try to do a query like this:
$sql= "INSERT INTO categorias(id_categoria,name) VALUES ('15','ssss');";
It works.
What's the problem?? I can't use SET with mysqli_query?
Thanks
You can not execute multiple queries at once using mysqli_query but you might want to use mysqli_multi_query as you can find out in the official documentation:
http://www.php.net/manual/en/mysqli.multi-query.php
Lets start with creating a working php script.
<?php
// replace for you own.
$host ="";
$user = "";
$password = "";
$database = "";
$con= mysqli_connect($host, $user, $password, $database);
if (!$con)
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else{
// Begin SQL query
$sql = "SELECT * FROM users";
$result = mysqli_query($con,$sql) OR Die('SQL Query not possible!');
var_dump($result);
return $result;
var_dump($result);
// End SQL query
mysqli_close($con);
};
?>
INSERT query:
$sql= "INSERT INTO categorias(name) VALUES ('ssss')";
mysqli_query ($con,$sql) OR Die('SQL Query not possible!');
UPDATE and DELETE query:
$sql= "DELETE FROM users WHERE username = 'Hola';";
$sql.= "UPDATE users SET foreign_key_checks = 0 WHERE username = 'Hola'"; /* I made a guess here*/
mysqli_multi_query ($con,$sql) OR Die('SQL Query not possible!');
Check the SET query. I think something is missing. I have changed it to what I think was your aim.
The connection should be established like this:
$Hostname = "Your host name mostly it is ("localhost")";
$User = "Your Database user name default is (root)"//check this in configuration files
$Password = "Your database password default is ("")"//if you change it put the same other again check in config file
$DBName = "this your dataabse name"//that you use while making database
$con = new mysqli($Hostname, $User , $PasswordP , $DBName);
$sql= "INSERT INTO categorias(id_categoria,name) VALUES ('15','ssss');";
In this query:
put categorias in magic quotes(`) and column names also
For your next query do this:
$sql= "SET foreign_key_checks = 0; DELETE FROM users WHERE username = 'Hola';";
Change to:
$sql= "SET foreign_key_checks = 0; DELETE FROM `users` WHERE `username` = 'Hola'";
I'm trying to get data from an database that I have created in phpMyAdmin. My problem is that however I change my query I'm getting the same type of error message using the mysql_error() function:
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 ''foods'' at line 1
PHP code index file:
<?php
require 'connect.inc.php';
$query = "SELECT 'food_type', 'calories' FROM 'foods'";
if($query_run = mysql_query($query)){
while ($query_row = mysql_fetch_assoc($query_run)){
$food = $query_row('food_type');
$calories = $query_row('calories');
echo $food.' has '.$calories.' calories ';
}
}else{
echo mysql_error();
}
?>
PHP code database connection file:
<?php
$connectionError = 'Can\'t connect.';
$mySqlHost = 'localhost';
$mySqlUser = 'root';
$mySqlPassword = 'Bhu8Nji9';
$mySqlDataBase = 'my_first_database';
if(!#mysql_connect($mySqlHost, $mySqlUser, $mySqlPassword) || !#mysql_select_db($mySqlDataBase)){
die($connectionError);
}else{
//echo 'Connected';
}
?>
Rewrite your query [Use Backticks instead of Single quotes]
$query = "SELECT 'food_type', 'calories' FROM 'foods'";
to
$query = "SELECT `food_type`, `calories` FROM foods";
use this..
$result = mysql_query("SELECT food_type,calories FROM foods");
while($row = mysql_fetch_array($result))
{...}
Do not use single quotes around table name and field names.
$query = "SELECT food_type, calories FROM foods";
Also avoid mysql_* functions.
Why shouldn't I use mysql_* functions in PHP?
Read following to know when or why to use backticks
Using backticks around field names
Can you try this, added backticks in table foods
$query = "SELECT food_type, calories FROM `foods`";
It is a problem regarding of unknown column name
you should use backtick as:
$query = "SELECT `food_type`, `calories` FROM foods";
There is a syntax error in your query. It should be as below,
$query = "SELECT food_type, calories FROM `foods`";