Can i store an selected value in variable - php

Is it possible to store an select value into an variable? in codeigniter.
Something like this:
$this->db->select_max('Datum');
$this->db->from('Uren');
$this->db->join('Project','Project.idProject = Uren.idProject');
$test123 = $this->db->get();
So that i get the value of the select_max into the variable $test123

The proprer way, then $max will contain the max of your request, or FALSE if request is not valid :
$query = $this->db->select_max('Uren.Datum', 'max_Datum')->get('your_table');
$max = $query ? $query->row()->max_Datum : FALSE;

I think you need to do something like this.
$this->db->select_max('Uren.Datum');
$test = $this->db->get('table_name');
if ($test) {
$this->db->select('fields');
$this->db->group_by('Project.idProject');
$this->db->get('table_name');
}
these will be two separate quires.

try
$this->db->select_max('field_name');
$check_query = $this->db->get('table_name');
if ($check_query->num_rows() > 0) {
$max = $check_query->result();
if(!empty($max[0]->field_name)) {
}
}
for more :- https://www.codeigniter.com/userguide2/database/active_record.html

This is the answer that worked to my question. Hope it will help someone else too
$this->db->select_max('Datum');
$this->db->from('Uren');
$this->db->join('Project','Project.idProject = Uren.idProject');
if ($idKlant > 0){
$this->db->where('idKlant', $idKlant);}
$datumstart = $this->db->get();
foreach ($datumstart->result() as $row)
{
$datastart = $row->Datum;
}

Related

How to check 2 values in 1 table using PHP, jQuery?

Is there a way to check 2 exists values in 1 table ?
It was work just fine when i use only 1 value 'beezName' but when i added another value 'divId' it didn't work, they only check whether the 'beezName' or 'divId' exist in table. I'm trying to get :
if beezName and divId exist then return true else false
How can i make this work? Thanks in advance
Here's my controller : Beez
function checkBeezExists()
{
$beezId = $this->input->post("beezId");
$beezName = $this->input->post("beezName");
$divId = $this->input->post("divId");
if(empty($beezId)){
$result = $this->beez_model->checkBeezExists($beezName, $divId);
} else {
$result = $this->beez_model->checkBeezExists($beezName, $divId, $beezId);
}
if(empty($result)){ echo("true"); }
else { echo("false"); }
}
And this is my model : beez_model
function checkBeezExists($beezName, $divId, $beezId= 0)
{
$this->db->select("beezName, divId");
$this->db->from("tbl_beez");
$this->db->where("beezName", $beezName);
$this->db->where("divId", $divId);
$this->db->where("isDeleted", 0);
if($beezId != 0){
$this->db->where("beezId !=", $beezId);
}
$query = $this->db->get();
return $query->result();
}
You want to check beezName and divId exist but in your model you have wrong condition for beezId
try below code:
function checkBeezExists($beezName, $divId, $beezId= 0)
{
$this->db->select("beezName, divId");
$this->db->from("tbl_beez");
$this->db->where("beezName", $beezName);
$this->db->where("divId", $divId);
$this->db->where("isDeleted", 0);
if($beezId != 0){
$this->db->where("beezId", $beezId); //<--------remove !=
}
$query = $this->db->get();
return $query->result();
}

Query not assigned into the variable / Not going into the condition

I was doing this code for my project and it seems that I can't get the values of the query into $currentRow. All that saved into the variable $currentrow is 22 which is the number rows in the database. I want to have access to all the query results. Please help. Here's the code.
public function getBSIConfig(){
$conn = oci_connect("472proj","system","//localhost/XE");
$sql = oci_parse($conn,"SELECT conf_id, conf_key, conf_value FROM bsi_configure");
oci_execute($sql);
echo "0";
while($currentRow = oci_fetch_all($sql,$res)){
echo "1.5";
echo $currentRow;
if($currentRow["conf_key"]){
echo "1";
if($currentRow["conf_value"]){
$this->config[trim($currentRow["conf_key"])] = trim($currentRow["conf_value"]);
echo "2";
}else{
$this->config[trim($currentRow["conf_key"])] = false;
echo "3";
}
}
}
}
And the output is only:
0
1.5
22
The results from this function are stored in the 2nd argument, rather than returned directly. See if this works for you:
$results = array();
$numResults = oci_fetch_all($sql, $results);
foreach ($results as $result) {
if ($result["conf_key"]) {
// etc ...
}
}
Read this http://php.net/manual/en/function.oci-fetch-all.php , you might get an idea what's wrong.

Call MySql Select Query in 2 different php function

Please help me with my problem.. can i call once Mysql Select Query from different function from two different function too... Sorry i don't know how to explain.. but below my sample what i want to archive..
I want use single query for 2 function.. so maybe i have to write code like below?
function sqlSelect ($db, $id, $id2) {
$sql = mysqli_query($db, "SELECT * FROM xxx WHERE id='$id' AND id2='$id2'");
$row = mysqli_num_rows($sql);
$field = mysqli_fetch_object($sql);
return $row;
}
but how to use $field inside 2 diff function?
function aaa ($a,$b,$c) {
if($row >= 1){
//Do something with $a $b and $c
//$reget field with $field['column'];
$field['column']; //???
}
return $result;
}
function bbb ($a,$b,$c) {
if($row >= 1){
//Do something with $a $b and $c
//get field with $field['column'];
$field['column']; //???
}
}
what i do right now is
function aaa ($db,$id,$id2,$a,$b,$c) {
$sql = mysqli_query($db, "SELECT * FROM xxx WHERE id='$id' AND id2='$id2'");
$row = mysqli_num_rows($sql);
$field = mysqli_fetch_object($sql);
if($row >= 1){
//Do something with $a $b and $c
//get field with $field['column'];
}
}
function bbb ($db,$id,$id2,$a,$b,$c) {
$sql = mysqli_query($db, "SELECT * FROM xxx WHERE id='$id' AND id2='$id2'");
$row = mysqli_num_rows($sql);
$field = mysqli_fetch_object($sql);
if($row >= 1){
//Do something with $a $b and $c
//get field with $field['column'];
}
}
But if i use what i write and code right now i think it's have to call same query twice. I cannot use query outside function so i have to write query on each function but i want to just write query once and can use in two different function..
Sorry for stupid explanation..
Thank you for help
One implementation can be as follow by passing an array as argument,
<?php
$arg['db']="database";
$arg['tabe']="table";
$arg['search']['id1']="id1";
$arg['search']['id2']="id2";
$arg['do_something']['a']="a";
$arg['do_something']['b']="b";
$arg['do_something']['c']="c";
function searchAndDoSomethingAndReturnResult($arg)
{
$return = NULL;
$query="SELECT * FROM ".$arg['table'];
$flag=false;
foreach($arg['search'] as $key=>$value)
{
if($flag)
$query.=" AND ";
else
$flag=true;
$query.= $key." = '".$value."' ";
}
$row = mysqli_num_rows($query);
$field = mysqli_fetch_object($query);
if($row >= 1)
{
foreach($arg['do_something'] as $job=>$value)
{
// $return[] = "some result"
// do something
}
}
return $return;
}
?>
let me know if this solve your problem
You can either pass in the $field variable as a function argument
<?
$field = ...;
function sql1($field,...,...){
// Use $field here
}
function sql2($field,...,...){
// Use $field here
}
?>
Alternatively, you can use the global keyword to access variables from outside of the function
<?
$field = ...;
function sql1(){
global $field;
// Use $field here
}
function sql2(){
global $field;
// Use $field here
}
?>

using a $_GET id to filter a mysql_fetch_array in PHP

So I have a query that I am returning all of the items into a mysql_fetch_array. Now, I know I could write another query and just select the items I need into a seperate query but, is there a way to just filter from the larger query what I want dependent on $_GET?
So, in english the user comes from a hyperlink that has ?id=1 and I peform a while that gets the all the values but, only display the $_GET['id'] items in a list
<?php //give ma all values but only echo out list of the $_GET['id'] in the url
while ($row = mysql_fetch_array($result) {
$id = $rowvideo["id"];
$title = $rowvideo["title"];
$length = $rowvideo["length"];
}
echo("<li><a href='#'>". $title." " .$length. "</a></li>");
?>
Hope this makes sense. Thank you all.
If you do not want a second query to get just what you need, a simple-if-statement in your loop should work:
<?php
$getId = isset($_GET['id']) ? $_GET['id'] : false;
//give ma all values but only echo out list of the $_GET['id'] in the url
while ($row = mysql_fetch_array($result)) {
$id = $row["id"];
$title = $row["title"];
$length = $row["length"];
if ($id == $getId) {
echo("<li><a href='#'>". $title." " .$length. "</a></li>");
}
}
?>
Note that I declared $getId outside of the loop to prevent having to use isset() during every iteration. If you don't verify if it's set and attempt to use it it will throw an undefined index warning - assuming you have error_reporting turned on (with that level enabled).
Alternatively, you could use PHP's array_filter() on the data after you've parsed it all:
$results = array();
while ($row = mysql_fetch_array($result)) $results[] = $row;
if (isset($_GET['id'])) {
$filtered = array_filter($results, function($element) use ($_GET['id']) { return ($element['id'] == $_GET['id']); });
$results = $filtered;
}
foreach ($results as $result) {
echo("<li><a href='#'>". $result['title']." " .$result['length']. "</a></li>");
}
My personal opinion would be to be more efficient and write the second query though, assuming of course you don't actually need all of the results when an id is specified. It would be as simple as:
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
$query = 'SELECT id, title, length FROM table WHERE id=' . (int)$_GET['id'];
} else {
$query = 'SELECT id, title, length FROM table';
}
// your existing code as-is
A little more clarity here:
This will allow the filter by id in the url by specifying id=xxx, IF xxx is an integer that is positive. So id of 'bob' or -1 will not filter the results still giving all results
$filter=false;
if(isset($_GET['id']))
{
$filter_id=intval($_GET['id']);
if($id>0) $filter=true;
}
while($row = mysql_fetch_array($result))
{
if( (!$filter) || ( ($filter) && ($filter_id==$row['id']) ) )
{
$id = $row["id"];
$title = $row["title"];
$length = $row["length"];
// do other stuff here
}
}
I also changed $rowvideo to $row as this is the array you used to fetch the results.
<?php //give ma all values but only echo out list of the $_GET['id'] in the url
while ($row = mysql_fetch_array($result)) {
$id = $rowvideo["id"];
$title = $rowvideo["title"];
$length = $rowvideo["length"];
if ($id == $_GET['id']) { // or even ===
echo("<li><a href='#'>". $title." " .$length. "</a></li>");
}
}
?>

unique random id

I am generating unique id for my small application but I am facing some variable scope problem. my code-
function create_id()
{
global $myusername;
$part1 = substr($myusername, 0, -4);
$part2 = rand (99,99999);
$part3 = date("s");
return $part1.$part2.$part3;
}
$id;
$count=0;
while($count == 1)
{
$id;
$id=create_id();
$sqlcheck = "Select * FROM ruser WHERE userId='$id';";
$count =mysql_query($sqlcheck,$link)or die(mysql_error());
}
echo $id;
I dont know which variable I have to declare as global
That doesn't look like a variable scope problem, it looks like a simple variable assign problem:
$count=0;
while($count == 1)
{
This block will clearly never execute.
Further, please use a boolean with a good name when doing boolean checks. It reads so much cleaner. i.e.:
function isUniqueUserID($userIDToCheck)
{
$sqlcheck = "Select * FROM user WHERE userId='$userIDToCheck';";
$resource = mysql_query($sqlcheck)or die(mysql_error());
$count = mysql_fetch_assoc($resource);
if( count($count) > 0)
{return false;}
return true;
}
$userIDVerifiedUnique = false;
while(! $userIDVerifiedUnique )
{
$userIDToCheck = create_id();
$userIDVerifiedUnique = isUniqueUserID($userIDToCheck );
}
Note that mysql_query will use the last used connection if you don't specify a link:
http://us2.php.net/mysql_query
No need to make it global.
in adition to Zak's answer i'd pass the username into the function instead of using globals
function create_id($username)
{
$part1 = substr($username, 0, -4);
$part2 = rand (99,99999);
$part3 = date("s");
return $part1.$part2.$part3;
}
also
//$id; no need for this
$count=1; // this bit
while($count == 1) // not sure what's going on
{
//$id; again same thing no need for this
$id=create_id($myusername);
edit: now that i think of it: how do you expect to find "Select * FROM ruser WHERE userId='$id';"? A Select query is used to find something specific, your username is so random, i think the likely hood of actually successfully getting a record is 1 in a bajillion.
edit2 whoops, i see the whole point is to get a unique username... O_O
In addition to the others:
$count =mysql_query($sqlcheck,$link)or die(mysql_error());
mysql_query doesn't return a record count but, rather, a resource.
mysql_query

Categories