Multiple SQL queries in PHP combined gives error - php

I have a two MySQL queries I'd like to combine, it works when I enter them directly to phpmyadmin.
I get those queries like this:
$sqlCombine = $sqlStart.";".$sqlStartBefore;
$conn->query($sqlCombine);
echo $sqlCombine;
echo gives the following:
UPDATE rn_slots_availability SET slot_avail_noclean = slot_avail_noclean -1 WHERE hotel_id = '5' AND room_type_id = '6' AND slot_date = '2014-09-05';UPDATE rn_slots_availability SET slot_avail_clean = slot_avail_clean -1 WHERE hotel_id = '5' AND room_type_id = '6' AND slot_date = '2014-09-06'
copy/paste to phpmyadmin works like a charm, executing directly does not, gives the following error:
Error: 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 'UPDATE rn_slots_availability SET slot_avail_clean = slot_avail_clean -1 WHERE ho' at line 1
That is the second query, but I don't see why?

Your current configuration doesn't support multiquery for some reason. If you don't care about the way you are executing them, just do the queries one at a time like this:
$conn->query($sqlStart);
$conn->query($sqlStartBefore);
Error should be gone.

Related

Mysqli query with zerofill

Hello stackoverflow community, I need help with this mysqli query. My auto increase column in database is ZERO-FILL element for example 00001 so when I try to query like this:
$stmt = $mysqli->query("UPDATE ".$db_table_prefix."korteles
SET
vardas = '".$this->clean_k_name."',
pavard = ".$this->clean_k_surname.",
WHERE korteles_nr = '00001'");
And I get error:
You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use
near 'WHERE korteles_nr = 00001' at line 18
I was searching Internet for solving this problem. But can't find so please help!
Query should be like this:-
$stmt = $mysqli->query("UPDATE ".$db_table_prefix."korteles
SET
vardas = '$this->clean_k_name',
pavard = '$this->clean_k_surname'
WHERE
korteles_nr = '00001'");
You have to remove the comma after $this->clean_k_surname variable

Error when preparing query - MySQLIi class "SHOW TABLES LIKE" error

I am using this database class for my project: GitHub.
When trying to execute a SHOW query to determine whether a table exists or not I receive this error:
Fatal error: Problem preparing query (SHOW TABLES LIKE users) 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 'users' at line 1 in mysqli.php on line 679
The query looks like this:
$result = $DATABASE->rawQuery("SHOW TABLES LIKE " . $TABLE);
$TABLE is obviously filled with a string, I double checked that.
Any idea what could be wrong?
You probably missed the quotes:
$result = $DATABASE->rawQuery("SHOW TABLES LIKE '" . $TABLE . "'");
The like statement it's value is wrong.
You should use:
BAD
$result = $DATABASE->rawQuery("SHOW TABLES LIKE 'value here' ");
Good
$result = $DATABASE->rawQuery("SHOW TABLES LIKE ? ");
$DATABASE->addParam($table);
I think you allso want to add % in front and after your $table :)

Using a PHP variable within a MySQL query string WHERE clause

What exactly is the correct syntax for adding PHP variables to a MySQL string?
This is my query:
"SELECT cd.SectionID, cd.CompanyName, cd.ShowOnSite, cd.LiveDate, cd.EndDate, cds.SiteID, s.SiteName
FROM CompanyDirectory cd
LEFT JOIN CompanyDirectorySections cds ON cd.SectionID = cds.SectionID
LEFT JOIN Sites s ON cds.SiteID = s.SiteID
WHERE s.SiteID = " . $id . " AND cd.ShowOnSite = 'y'
ORDER BY cd.EndDate DESC"
But it throws the following:
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 'AND cd.ShowOnSite = 'y' ORDER BY cd.EndDate DESC' at line 5
I have also tried WHERE s.SiteID = $id and WHERE s.SiteID = '" . $id . "' but to no avail. The former gives a blank screen, and the latter gives the aforementioned error. The variable is an integer.
I have tried the query in phpMyAdmin and it works perfectly, substituting the variable for an actual ID.
Note: if it's important, $id has been received from a form via $id = $_POST['id']; before the query, and then stripped and escaped.
Thanks.
If MySQL is saying there is an error near "AND cd.ShowOnSite = 'y'", this normally means there's an issue with whatever comes before it - in this case, the $id.
Can you print out the query in your PHP file? This may show you that $id is in fact blank, which would make the query look like "WHERE s.SiteID = AND cd.ShowOnSite = 'y'".
If it's blank, there's obviously something wrong with the $id value which you will need to sort out before your MySQL code.

MySQL Error 1064 Using LIMIT Clause

I'm having a strange issue running a query via PDO prepared statements that I just can't spot the issue. When running the query manually it works just fine. Here is the code (simplified for conciseness):
// Query Parameters
$params = array( 1, 5 );
// Get Products
$query = "SELECT * FROM mydb.Product
WHERE ProductId >= ?
AND IsApproved = 1
AND IsPublic = 1
LIMIT ?";
// Get Database Instance
$dbh = App\App::getDatabase()->getInstance();
// Prepare Query
if( $stmt = $dbh->prepare($query) )
{
if( $stmt->execute($params) )
{
// Return Records
}
}
Removing the LIMIT ? portion from the query returns all results as expected. Instead, when attempting to use the LIMIT and it passes 5 as the value, I get this error:
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 ''5'' at line 5
A dump of the PDOStatement object after preparation shows:
object(PDOStatement)[59]
public 'queryString' => string 'SELECT * FROM mydb.Product
WHERE ProductId >= ?
AND IsApproved = 1
AND IsPublic = 1
LIMIT ?'
I've tried putting a semicolon at the end of the query but that gives same error. Am I having cerebral flatulence and missing something obvious?
TL;DR; Why does my prepared statement fail when using the LIMIT clause with a 1064 error?
I think this could be a duplication of
PDO Mysql Syntax error 1064
The solution is to bind limit parameter forcing it to be an int instead of a string with a simple (int) cast.
just put (int) before your limit value

Use array in where clause not working

Hi I have a query in php file which is used to filter the data in file from mysql database
$_SESSION['sc_session'][$this->Ini->sc_page]['grid_deposit']['where_orig'] = " where Reg_no = \"69\"";
In this line if Reg_no = \"69\"" , if i change the 69 to any value data is being modified but if i use an array instead of 69 then its not working like this
$_SESSION['sc_session'][$this->Ini->sc_page]['grid_deposit']['where_orig'] = " where Reg_no = " . $fc . "";
But if i use
$fc = 69;
echo $fc;
Then its working but not on that line please tell me how to code this The error on which i get is
Error
Error while accessing the database:
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 '' at line 1
select count(*) from deposit where Reg_no =
from your Reg_no =\"69\""
and your Reg_no =". $fc."";
are you not missing the "" of the $fc
$_SESSION['sc_session'][$this->Ini->sc_page]['grid_deposit']['where_orig'] = " where Reg_no = \"" . $fc . "\"";
to match your 69 example.
In your original question you stated this error text
Error
Error while accessing the database:
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 '' at line 1
select count(*) from deposit where Reg_no =
If the $fc would be an array you would see this in the query as such. If i remember correctly it would look like that ...
Error
Error while accessing the database:
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 '' at line 1
select count(*) from deposit where Reg_no = Array
As it does not i assume that the variable $fc is empty. Did you check the variable or better create the query and log it somewhere to check the query as it gets sent to the sql server.
As mentioned, if it would be an array PHP would convert it when wrongly used to the text "Array" which you should find in the query.
i guess you try to do something like
"where Reg_no IN (".implode(",", $fc).")";

Categories