How to write select query in moodle - php

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

Related

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

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);

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

Convert sql query to codeiginiter

SELECT *
FROM `tb_slots`
WHERE `service_name`='Paragliding'
and repetition > (
SELECT count(*)
FROM `user_booking`
WHERE date='2019-11-17'
and `time-session`=`tb_slots`.`id`
and `pay_status`='Success'
);
There is no such thing as converting a SQL query to CodeIgniter.
If you wish to have the query working in CodeIgniter's query builder, it's should be easy following the documentation here:
https://codeigniter.com/user_guide/database/query_builder.html
I hope this would help you
$this->db->select('*');
$this->db->where('service_name','Paragliding');
$this->db->where('`repetition` > (SELECT count(*) FROM user_booking WHERE date="2019-11-17" and time-session=tb_slots.id and pay_status="Success"'));
$this->db->get('tb_slots');
thanks

like operator in mysql using php

I need to fetch the data from database using like operator.If I use static value i got the answer but using dynamically I can't fetch it.Any body know the answer please help.
$query = mysql_query("SELECT * FROM table WHERE the_number LIKE '2016-01-09%'");
function calendercheck($data)
{
$query = $this->db->query("SELECT * FROM events WHERE start_date LIKE '$data%'");
return($query->result());
}
You can use WHERE docdate BETWEEN '$from' AND '$to' for filter data in mysql.

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 .'%');

Categories