I have this query in laravel which runs perfectly fine when it finds the data in table but give error when it doesn't find the matched data, how can make query to give no error when it doesn't find data in table ?
DB::select(DB::raw('SELECT * FROM time
WHERE p_day = dayname(CURDATE())
AND CURTIME() BETWEEN p_start AND p_end'))[0];
thats because then there is no item in the array returned by DB::select. You would have to check its lenght before accessing it using index operator
try sth. like that:
$results = DB::select(DB::raw('SELECT * FROM time
WHERE p_day = dayname(CURDATE())
AND CURTIME() BETWEEN p_start AND p_end'));
if (!empty($results)) // or: if (count($results) > 0)
{
// Stuff was returned
$desiredResult = $results[0];
// Your hork here
}
else
{
// what you need to to if nothing is returned (no matches for your query)...
}
Related
I'm running a very simple query in Oracle NoSQL (NodeJs driver):
let count_statement = `SELECT count(*) FROM ${resource}`;
let res = await this.client.query(count_statement, {});
This returns 0 rows (and thus no count). If I run the query without the count, I get back rows I can iterate over. Is there no way to get the total results for a query.
I don't want to count the results WITHIN the row. I need the number of rows that match this query (which is all of them in this query)
In order to get the full result set, the code should call client.query in a loop intil the continuationKey in the result object is null, as in this example from the nosql node sdk:
/*
* Execute a query and print the results. Optional limit parameter is used to
* limit the results of each query API invocation to that many rows.
*/
async function runQuery(client, stmt, limit) {
const opt = { limit };
let res;
console.log('Query results:');
do {
// Issue the query
res = await client.query(stmt, opt);
// Each call to NoSQLClient.query returns a portion of the
// result set. Iterate over the result set, using the
// QueryResult.continuationKey in the query's option parameter,
// until the result set is exhausted and continuation key is
// null.
for(let row of res.rows) {
console.log(' %O', row);
}
opt.continuationKey = res.continuationKey;
} while(res.continuationKey != null);
}
You can also use the new queryIterable syntax introduced in version 5.3.0
const count_statement = `SELECT count(*) FROM ${resource}`
const rows = []
for await(const res of client.queryIterable(count_statement )) {
rows.push.apply(rows, res.rows);
}
return rows
Suppose I have a user table with columns id, name, age
With a normal get query User::get(), I get all results with those column's value.
But instead of just getting the id, name, age columns, is it possible if I add another column, perhaps a column with an alias of future_age with a value of age + 1.
The above result could be achieved in SQL like so:
SELECT res.*, (SELECT sum(res.age+1) from users where id = res.id) as future_age from users as res
I've thought about looping through the result and creating new key. but I think this would make the query execution time slow when the data is lengthy.
Is it possible to create the future_age key/column (I don't know the term hehe) directly on the query?
Currently, this is my query:
$user = User::get();
$new_res = []
if($user->count() > 0) {
foreach ($user as $u) {
$u['future_age'] = $u->age + 1
$new_res[] = $u;
}
}
The query works tho, but I don't think this is good if I have a large set of data.
Codeigniter I want to increase +1 when loading page. but the query does 0
'views + 1' and I did 10 no problem.
It didn't make sense to throw the query for views.
function get_views()
{
$this->db->set('views', 'views+1');
$this->db->where('Id', 1);
$this->db->update('pages'); // gives UPDATE mytable SET field = field+1 WHERE id = 2
}
You need to pass a third parameter as false like below:
$this->db->set('views', 'views+1', FALSE);
The Third Parameter tells CodeIgniter not to protect the generated query with backticks. In your case the final query will be
UPDATE pages SET views = views + 1 WHERE Id = '1';
I have an sqliteCpp wrapper for my sqlite3 and was wondering if there is a query to search for integer in 'Symcod' columns as shown below:
sym = 100;
SQLite::Statement query(db, "SELECT Symcod FROM RawData WHERE EXISTS Symcod = sym");
but this gives me an Syntax error. Is there a way I can search the table for a integer using a variable name?
The sqlitecpp documentation suggests
// Compile a SQL query, containing one parameter (index 1)
SQLite::Statement query(db, "SELECT * FROM test WHERE size > ?");
// Bind the integer value 6 to the first parameter of the SQL query
query.bind(1, 6);
// Loop to execute the query step by step, to get rows of result
while (query.executeStep())
{
//.... do stuff with e.g. query.getColumn()
}
So you would construct your query using a ? for the variable, then do query.bind(1,sym)
I am developing a search tool for my project,
My desired output is to get the common value from the different tables. eg) SKR0BP100
How to get this value ??
As i am running the program in for-loop and fetching the values from while-loop, now how to use array_intersect() function? Because for array intersect function, minimum 2 arrays are needed, but i get only one array at a time, as it runs on for-loop. So what should i do ?? Please Help me!
$result = array_intersect($arr1, $arr2);
But i have only one $array (ie, $sid[$i] at a time, as it runs in for-loop.
My program
for($i=0;$i<$cc;$i++)
{
$m1="select * from $u$sc where $b[$i]='$a[$i]' ";
$m2=mysql_query($m1);
echo"$m1<br><br>";
while($we=mysql_fetch_array($m2))
{
$sid[$i]=$we['SI'];
echo"$sid[$i]<br><br>";
}
}
Desired Output = SKR0BP100
// How to get this??
Present output
select * from Studentsc where Zone='East'
SKR0BP100
SKR0BP12
select * from Studentsc where Area='Rural'
SKR0BP129
SKR0BP13
SKR0BP100
select * from Studentsc where Class='12'
SKR0BP100
SKR0BP101
So if you want to create query then try this
$where = array();
for($i=0;$i<$cc;$i++)
{
$where[] = $b[$i]."='".$a[$i]."'";
}
$m1="select * from $u$sc where ".implode(" and ",$where); //If you are sure that atleast one value vomes