SqlQuery(statement,params) - sql

I wonder how to do:
params= new object[]{ new SqlParameter("ID",val),new SqlParameter("Label","'%Name%'")};
statement = "Select ID, Label from Country Where ID = ? or or Label Like ?";
I tried and it gave me :
Incorrect syntax near '?'.

You have a typo:
"Select ID, Label from Country Where ID = ? or or Label Like ?";
should be
"Select ID, Label from Country Where ID = ? or Label Like ?";

Related

R shiny and Database

i really need your help.
I have an example of a DataBase which table is "t_client (id_cli, name_cli)"
I want to select the id of client(id_cli) from "selectInput" and the result gives me the name of client (name_cli) by reactive function.
This is my code :
req1 = dbGetQuery(DB, "select id_cli, name_cli from t_client;")
selectInput(inputId = "id_cli", label = "Clients", choices = req1$id_cli)
databaseInput <- reactive({
req1$id_cli = input$id_cli
req2 = dbGetQuery(DB, "select *from t_client where id_cli = 'req1$id_cli';")
req2
})
renderTable({
databaseInput()
})
It doesnt work; i dont have the link between the item from selectInput and the real table "t_client"

How to compare attributes

I have the following code and the output will be in array. How can I find arrays that match between $pesara and $ahli. the condition between it is CURRENT_ID_NO is equal to ic_no
$sql = "SELECT CURRENT_ID_NO, NAME, MOBILE_NO FROM pesara";
$dbCommand = Yii::app()->db->createCommand($sql);
$pesara = $dbCommand->queryAll();
$sql2 = "SELECT name, ic_no, ic_type, tel_no, pesara, created_dt, created_by, updated_dt, updated_by FROM ost_ahli";
$dbCommand2 = Yii::app()->db->createCommand($sql2);
$ahli = $dbCommand2->queryAll();
$result = array_intersect($pesara->CURRENT_ID_NO, $ahli->ic_no);
print_r($result);
Sorry for my bad English but I hope someone can help me on this.
You can use array_uintersect() for custom comparison
and also have sample code for that here

LastInserID from other table insert to table [duplicate]

I have a query, and I want to get the last ID inserted. The field ID is the primary key and auto incrementing.
I know that I have to use this statement:
LAST_INSERT_ID()
That statement works with a query like this:
$query = "INSERT INTO `cell-place` (ID) VALUES (LAST_INSERT_ID())";
But if I want to get the ID using this statement:
$ID = LAST_INSERT_ID();
I get this error:
Fatal error: Call to undefined function LAST_INSERT_ID()
What am I doing wrong?
That's because that's an SQL function, not PHP. You can use PDO::lastInsertId().
Like:
$stmt = $db->prepare("...");
$stmt->execute();
$id = $db->lastInsertId();
If you want to do it with SQL instead of the PDO API, you would do it like a normal select query:
$stmt = $db->query("SELECT LAST_INSERT_ID()");
$lastId = $stmt->fetchColumn();
lastInsertId() only work after the INSERT query.
Correct:
$stmt = $this->conn->prepare("INSERT INTO users(userName,userEmail,userPass)
VALUES(?,?,?);");
$sonuc = $stmt->execute([$username,$email,$pass]);
$LAST_ID = $this->conn->lastInsertId();
Incorrect:
$stmt = $this->conn->prepare("SELECT * FROM users");
$sonuc = $stmt->execute();
$LAST_ID = $this->conn->lastInsertId(); //always return string(1)=0
You can get the id of the last transaction by running lastInsertId() method on the connection object($conn).
Like this $lid = $conn->lastInsertId();
Please check out the docs https://www.php.net/manual/en/language.oop5.basic.php

Count function in sql query issue

Please, i have this query here:
$query_pag_num = "SELECT COUNT(*) AS count FROM forma";
$result_pag_num = odbc_exec($connection, $query_pag_num) or die(odbc_error());
$row = odbc_fetch_array($result_pag_num);
$count = $row['id'];
The issue is i get this error here:
Undefined index: id where it's: `$count = $row['id'];`
Help please! It won't work without this piece of code here.
I'm using odbc_connect..
Thanks in advance..
your select function will only return 1 column: "count". you are trying to get to column "id"
here is your fixed code
$query_pag_num = "SELECT COUNT(*) AS count FROM forma";
$result_pag_num = odbc_exec($connection, $query_pag_num) or die(odbc_error());
$row = odbc_fetch_array($result_pag_num);
$count = $row['count'];
you do not have any field in your SELECT called 'id'. Try: $count = $row['count'];

NHibernate Select Max value with filter

I am trying to get the max value from a table, doing something like this:
SELECT max(re.Sequence) FROM MyTable re WHERE re.ItemId = :itemId
So i can get for each itemId the maximum value for the column Sequence.
Tried with createQuery but didnĀ“t worked:
string hql = #"SELECT new Int32(max(re.Sequence) FROM MyTable re WHERE re.Item.Id = :itemId";
List<Int32> lista = session
.CreateQuery(hql)
.SetParameter("itemId", idItem)
.List<Int32>()
.ToList();
Any help will be appreciated.
Best Regards.
Using the criteria syntax:
var criteria = session.CreateCriteria<MyTable>();
criteria.Add(Restrictions.Eq("ItemId", itemId));
criteria.SetProjection(Projections.Max("Sequence"));
var max = criteria.UniqueResult<int>();
Using the query over syntax:
var max = session.QueryOver<MyTable>().Where(x => x.ItemId.Equals(itemId)).Select(
Projections.Max<MyTable>(x => x.Sequence)).SingleOrDefault<int>();