Laravel sql insert is not working properly via DB::unprepared - mysql

I get SQL via file_get_contents and give DB::unprepared function
$path = public_path('sql/Store.sql');
$sql = file_get_contents($path);
DB::unprepared($sql);
the tables are created but the triggers are not created.
But when I put this SQL code to phpMyadmin both tables and triggers are created successfully. I use Server version: 10.3.28-MariaDB - MariaDB Server.
Do you have any idea how to solve this issue?

You should trim the contents that come from the file to remove some characters like \n at the end of sqls. I've used trim($file_path, "\x00..\x1F") to remove this range of characters from your sql.
$path = public_path('sql/Store.sql');
$sql = trim(file_get_contents($path), "\x00..\x1F");
DB::unprepared($sql);

Related

Error when rendering .tpl file from SQL query

I'm trying to get a result from a MySQL database and display selected values into a .tpl template file.
This is what I tried so far:
{php}
$clienthosting = $this->get_template_vars(service);//Here is where the exception is thrown
$dbid = $clienthosting['id'];
$query = mysql_query("SELECT dedicatedip FROM tblhosting WHERE id = $dbid");
$result = mysql_fetch_array($query);
$dedicatedip = $result["dedicatedip"];
$this->assign("dedicatedip", $dedicatedip);
{/php}
But it generated the following error:
Something went wrong and we couldn't process your request.
What am I doing wrong?
Thanks.
WHMCS recommends not use {php} inside tpl files, you can use hooks instead to add variables and use them in the TPL file.
But you can enable it in the settings: Setup > General Settings > Security > Allow Smarty PHP Tags.
Also, if you're running PHP 7 the mysql extension is removed, you can't use functions like mysql_query and mysql_fetch_array.
You should use Capsule and Eloquent as recommended at Interacting with the Database page

How do raw query in kohana freamwork

I want make package with list of query.
It is update query like as:
UPDATE table
SET column = XXX
WHERE column = XXX AND column2 = XXX;
UPDATE table
SET column = XXX
WHERE column = XXX AND column2 = XXX;
UPDATE table
SET column = XXX
WHERE column = XXX AND column2 = XXX;
I have these 1000 queries in one package. Now I want make do queries.
I try do it, but unsuccessfully, because every time I get an error like that:
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...
I copy this query and past to phpmyadmin and I do it. These queries work!
I have a variable with prepared queries and I trying to do it in different way.
Database::instance()->query(NULL,$sql);
DB::query(5,$sql)->execute();
DB::query(Database::UPDATE,DB::expr($sql))->execute();
DB::query(Database::UPDATE,$sql)->execute();
but it does not work ;/
Anyone know how do it?
Just call method query from instance of database:
/** #var Database $db */
$db = Database::instance();
$db->query(Database::UPDATE, 'UPDATE table SET column = XXX WHERE column = XXX AND column2 = XXX;');
But if you want execute multiple SQL statements in one query it's impossible out of box. By default Kohana use old mysql API and mysql_query do not support multiple queries in a single call.
It you want use multiple SQL statements i know 3 way:
Some times ago for Kohana i saw module for mysqli support, you can try to find him and modify: add to it new method which will be use http://php.net/manual/en/mysqli.multi-query.php mysqli::multi_query can execute multiple SQL statements in one query.
Or you can switch to using PDO. For this you must make changes in your database config file, according Kohana's documentation, add map for columns in models.
Or create new PDO connection in set up it only for this query (if you don't use transaction it will be more easy, than variant 2).
Also for variant 2 and 3 you must set up some PDO settings: https://stackoverflow.com/a/6461110/419665
P.S. For call database instance by config name:
Database::instance('config_name');

php or mysql pound underscore variable

Ran across some old PHP code that runs as a mysql query that I need to debug because its failing.
$sql = "SELECT * FROM #__menu ORDER BY name";
Basically I have never seen a pound symbol in PHP or MySQL used like this. I could understand if it was a comment or a super global variable but I don't think it is that.

Warning: mysql_num_rows() But only on webhost, not localhost

On localhost this works great. But when I upload files to server I get this error:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /storage/content/04/13fd39104/xxxxx.com/public_html/users/list.php on line 54
My code is:
//load users from database
$users = mysql_query("SELECT id,username FROM ".$sql_table_users." WHERE id!='".$_SESSION['user_id']."'");
if(mysql_num_rows($users) > 0){
while($user = mysql_fetch_assoc($users)){
//ALT tag contains user ID and user name
print '• '.$user['username'].'<br />';
}
}
Are you sure to have the same db strutture on server? Probably some column or table name are different from localhost. Check also if your db connection credentials are correct
Perhaps your database on server is not the same as your localhost.
add echo mysql_error(); after mysql_query to see details.
I would echo the query string before performing the query to see if it really is the string you think it should be. Most likely one of the variables is not defined so the query is failing. If that doesn't help you could also output any mysql errors after the query.
And of course, the obligatory suggestion to get off of the deprecated mysql_* functions! If you don't want to do PDO, check out mysqli and how very similar it is to the mysql functions if you use the procedural functions.

Ruby SQL Insert using Mysql2 gem

I'm trying to insert into a remote mysql database. I am able to connect correctly and can query 'select' no problem from it. However, I cannot perform inserts into the same table that I can select from. I suspect it has something to do with my binds, but this is nearly identical to what I was using to get sqlite3 working which I think uses the same Arel to insert.
#result = #db.query("insert into lead_to_processes (case_number, style_of_case) values (?,?)", [
self.case_number.to_blob.force_encoding("UTF-8"),
self.style_of_case.to_blob.force_encoding("UTF-8")
]
)
Ultimate goal is to be able query a remote database from inside of a model and insert data into it. I've tried using Octopus and that didn't quite work because the tables will be different from the databases.
I have full permissions with this user on the database.
So following guidance from comments i changed the syntax and am getting a different error
Mysql2::Error: You have an error in your SQL syntax;
However i'm doing the query like this now
#db = Mysql2::Client.new(connectionstring)
#case_number = #db.escape(self.case_number)
#style_of_case = #db.escape(self.style_of_case)
#db.query("insert into lead_to_processes (case_number, style_of_case) VALUES
(#{#case_number}, #{#style_of_case})
Any ideas or guidance? I've also tried this with '' encapsulating the variables that i'm inserting
I guess there were some weird characters in my code so I had to force UTF-8 encoding and then removed the characters using gsub below, everything is flowing now.
Thanks for the advice
#db.escape(self.style_of_case.force_encoding("UTF-8"))
#db.escape(self.case_number.gsub(/[\xC2]/,'').gsub(/[\xA0]/,'').force_encoding("UTF-8"))
Is it possible that you are missing an end quote?
this
#db.query("insert into lead_to_processes (case_number, style_of_case) VALUES
(#{#case_number}, #{#style_of_case})
should be
#db.query("insert into lead_to_processes (case_number, style_of_case) VALUES
(#{#case_number}, #{#style_of_case}") <== notice the quote at the end.