How to use "SELECT * INTO ..." using laravel - php

I'm new to laravel and I cant seem to run this query in laravel.
SELECT * INTO tmp.dbo.dataset_15112022_124949 FROM tmp.dbo.dataset;
I have tried the following without any success
$archive_db = "SELECT * INTO tmp.dbo.dataset_15112022_124949 FROM tmp.dbo.dataset" ;
config(['database.connections.sqlsrv.database' => 'tmp']);
DB::connection('sqlsrv')->raw($archive_db);
Any help would be highly apreciated.

With the following code you will get a PDO instance:
DB::connection('sqlsrv')->getPDO();
So you can execute a raw query with the following code:
$sql = "SELECT * INTO tmp.dbo.dataset_15112022_124949 FROM tmp.dbo.dataset;";
DB::connection('sqlsrv')->getPDO()->query($sql);
Another approach is DB::statement($sql);
$sql = "SELECT * INTO tmp.dbo.dataset_15112022_124949 FROM tmp.dbo.dataset;";
DB::connection('sqlsrv')->statement($sql);

Related

How to use PHP's " .date('Y-m-d'). " in a MySQL query in Where clause?

I have cloned a movie theatre project where the movie_carousel.php uses a query to fetch movies from the MySQL database.
However it doesn't fetch any data when I run it, only when I remove the entire Where clause from the query - then it fetches the movies.
I believe it has something to do with the .date('Y-m-d'). in the script.
What things I could try to fix it?
Here's the code of movie_carousel.php:
<?php
include 'admin/db_connect.php';
$movies = $conn->query("SELECT * FROM movies where '".date('Y-m-d')."' BETWEEN date(date_showing) and date(end_date)" );
?>
It is better to use SQL rather than PHP when possible.
So you can replace :
$movies = $conn->query("SELECT * FROM movies where '".date('Y-m-d')."' BETWEEN date(date_showing) and date(end_date)" );
By:
$movies = $conn->query("SELECT * FROM movies WHERE DATE(NOW()) BETWEEN DATE(date_showing) and DATE(end_date)" );
Also check if you really have datas in your table for now.
Fiddle: https://www.db-fiddle.com/f/heYtYGoChmz28XeT7mzGsp/0#&togetherjs=bguyuSjnva

PHP and Mysql query does not work as expected

I am trying to make a web application and I would like to search in a mysql database with a php script.
But my code does not work as expected.
Here is my code:
$search=$_POST['search'];
$sql_cautare="SELECT * FROM carti WHERE titlu = "$search"";
I have search form..and I want to select those values that are entered in the search form.
Can anyone help me?
Try this one
$sql_cautare = $conn->prepare("SELECT * FROM carti WHERE titlu = ?");
$sql_cautare->bind_param('s', $search);
$sql_cautare->execute();
The following should work:
$sql_cautare = "SELECT * FROM carti WHERE titlu = '$search'";
But you should definitly do some input sanitization before using it in SQL!

PHP LIKE query with oracle database

$ten_desc=$_REQUEST['frm_num'];
$sql = "select * from TENDER_REG where TENDER_DESC LIKE:ten_desc%'";
$stmt=oci_parse($conn,$sql);
oci_bind_by_name($stmt,':ten_desc',$ten_desc);
displaying error please help
The SQL query string will likely confuse PHP and ORACLE. The following will make more sense to both. 1) there is one clear placeholder in the query string. 2) the value passed is complete.
Untested:
replace:
$sql = "select * from TENDER_REG where TENDER_DESC LIKE:ten_desc%'";
with:
$sql = "select * from TENDER_REG where TENDER_DESC LIKE :ten_desc";
and:
oci_bind_by_name($stmt,':ten_desc',$ten_desc);
with:
oci_bind_by_name($stmt,':ten_desc', $ten_desc .'%');

How to write select query in moodle

This is my sql query:
select * from user_tb;
How can I convert it to a Moodle query format.
Please help me.
$users = $DB->get_records_sql('SELECT * FROM {user}');
or
$users = $DB->get_records('user');
See the database documentation here http://docs.moodle.org/dev/Data_manipulation_API#Getting_an_hashed_array_of_records

[PHP][MySQL]How To SELECT data with two constrain?

I'm new to PHP+MySQL programming
I want to SELCET data using tow constrain at once,
the cons1 is
Tid=user02 and Fid=user01
and cons2 is
Tid=user01 and Fid=user02
the data I want to output is something like:
$result1 = mysql_query("SELECT * FROM myTable WHERE Tid='user02' AND Fid='user01');
+
$result2 = mysql_query("SELECT * FROM myTable WHERE Tid='user01' AND Fid='user02');
= what I want
can it be done in single line?
or can make a result which stored $result1 and $result2
thanks for taking time reading my question
You can OR the two conditions resulting in a single query:
SELECT * FROM chat
WHERE (Tid='user02' AND Fid='user01')
OR (Tid='user01' AND Fid='user02')
$result1 = mysql_query("SELECT * FROM chat WHERE Tid='user02' AND Fid='user01' OR Tid='user01' AND Fid='user02'");
Some reading : boolean algebra and the mysql doc
Try this one:
$result1 = mysql_query("SELECT * FROM chat WHERE Tid='user02' AND Fid='user01'
UNION SELECT * FROM chat WHERE Tid='user01' AND Fid='user02'");

Categories