I want this code to accept more than one WHERE parameter at the bottom, but when the added text comes into play, it returns nothing. It returns what it should without the filter.
What I'm trying to do is filter the results I get from the inner query from user inputted data that gets posted into the PHP code.
Any advice would be helpful at this point.
$OgrenciNo= " AND OgrenciNo LIKE %1005%";
$sth = $conn->prepare('SELECT t.* FROM (SELECT ogrenciler.OgrenciNo, ogrenciler.adsoyad as adsoyad1,
dersler.Ders, dersler.DersKodu, tarih.Tarih, ogretmenler.isim,
login.adsoyad, onayturu.Onay, mazeretturu.tur,
mazeretaciklama.Aciklama, mazeretaciklama.LogTarihi
FROM ogrenciler, tarih, aratablo, ogretmenler,
mazeretaciklama, login, dersler, mazeretturu, onayturu
WHERE aratablo.AciklamaID = mazeretaciklama.AciklamaID
AND aratablo.TarihID = tarih.TarihID
AND aratablo.DersID = dersler.DersID
AND aratablo.OnaylayanID = login.OnaylayanID
AND aratablo.OnayID = onayturu.OnayID
AND aratablo.Mazturu = mazeretturu.Mazturu
AND dersler.OgrGorID = ogretmenler.OgrGorID
AND mazeretaciklama.OgrenciNo = ogrenciler.OgrenciNo) t
WHERE LogTarihi > "1900-01-01"
'.$OgrenciNo
);
$sth->execute();
Normal results are as follows:
array (size=2)
0 =>
array (size=11)
'OgrenciNo' => string '1005.02021' (length=10)
'adsoyad1' => string 'Örnek Öğrenci' (length=13)
'Ders' => string 'Ders Adı' (length=8)
'DersKodu' => string 'KOD000' (length=6)
'Tarih' => string '2016-02-01' (length=10)
'isim' => string 'Öğretim Elemanı İsmi' (length=20)
'adsoyad' => string 'ROOT' (length=4)
'Onay' => string 'Onaylandı' (length=9)
'tur' => string 'Sınav' (length=5)
'Aciklama' => string 'hastalık hastası' (length=16)
'LogTarihi' => string '2016-02-09 09:14:52' (length=19)
1 =>
array (size=11)
'OgrenciNo' => string '1035.02021' (length=10)
'adsoyad1' => string 'ahmet mehmet' (length=12)
'Ders' => string 'E-Ticaret Sistem Tasarımı' (length=25)
'DersKodu' => string 'BIL446' (length=6)
'Tarih' => string '2016-02-07' (length=10)
'isim' => string 'Yrd. Doç. Dr. Mustafa Cem Kasapbaşı' (length=35)
'adsoyad' => string 'Araş. Gör. Erdem Yavuz' (length=22)
'Onay' => string 'Onaylanmadı' (length=11)
'tur' => string 'Ders' (length=4)
'Aciklama' => string 'cenaze vardı' (length=12)
'LogTarihi' => string '2016-02-11 13:33:53' (length=19)
I'm trying to filter out the second result but the LIKE argument doesn't seem to work.
By popular demand here's the whole query after I print it out in the page (P.S. the only thing different from the top one is that it contains "AND OgrenciNo LIKE %1005%" at the end):
SELECT t.* FROM (SELECT ogrenciler.OgrenciNo, ogrenciler.adsoyad as adsoyad1, dersler.Ders, dersler.DersKodu, tarih.Tarih, ogretmenler.isim, login.adsoyad, onayturu.Onay, mazeretturu.tur, mazeretaciklama.Aciklama, mazeretaciklama.LogTarihi FROM ogrenciler, tarih, aratablo, ogretmenler, mazeretaciklama, login, dersler, mazeretturu, onayturu WHERE aratablo.AciklamaID = mazeretaciklama.AciklamaID AND aratablo.TarihID = tarih.TarihID AND aratablo.DersID = dersler.DersID AND aratablo.OnaylayanID = login.OnaylayanID AND aratablo.OnayID = onayturu.OnayID AND aratablo.Mazturu = mazeretturu.Mazturu AND dersler.OgrGorID = ogretmenler.OgrGorID AND mazeretaciklama.OgrenciNo = ogrenciler.OgrenciNo) t WHERE LogTarihi > "1900-01-01" AND OgrenciNo LIKE %1005%
Your like condition should be like this. Just add ' in it. :
$OgrenciNo= " AND OgrenciNo LIKE '%1005%'";
Related
Having trouble wrapping my head around this conceptually. Still new to this. Basically I have this return from my database :
array (size=456)
0 =>
object(stdClass)[358]
public 'id' => string '2432' (length=4)
public 'symbol' => string '.AMLP' (length=14)
public 'last' => string '0.01' (length=4)
public 'volume' => string '3690' (length=4)
public 'the_date' => string '2019-09-13' (length=10)
public 'the_screener' => string '1' (length=1)
public 'notes' => string 'notes here' (length=149)
1 =>
object(stdClass)[726]
public 'id' => string '2417' (length=4)
public 'symbol' => string '.ARCC' (length=14)
public 'last' => string '2.25' (length=4)
public 'volume' => string '1633' (length=4)
public 'the_date' => string '2019-09-13' (length=10)
public 'the_screener' => string '1' (length=1)
public 'notes' => string 'notes' (length=60)
2 =>
object(stdClass)[726]
public 'id' => string '2447' (length=4)
public 'symbol' => string '.ARCC' (length=14)
public 'last' => string '2.25' (length=4)
public 'volume' => string '1633' (length=4)
public 'the_date' => string '2019-09-12' (length=10)
public 'the_screener' => string '1' (length=1)
public 'notes' => string 'notes here 3' (length=60)
3 =>
What I'm trying to do with PHP is create an object/array that I can work with that displays these items like
AMLP 1 found on dates 2019-09-13
ARCC 2 found on dates 2019-09-13, 2019-09-12
In the end I would display these in a table, but conceptually this is what I'm trying to do.
I've tried creating an array in my foreach I use to display this information in a table, but I was thinking about it and it's probably better to just use the same query data and break it down separately.
So I'd like to create an array like :
Array
(
[1] => Array
(
[id] => 1
[symbol] => ARCC
[dates] => Array
(
[3] => Array
(
2019-09-13
2019-09-12
)
)
)
)
Consider your array is $arrDates. If you want to access properties like object properties
$arrFinal = [];
foreach ($arrDates as $intKey => $obj){
$strSymbol = getSubSymbol($obj->symbol);
if(!isset($arrFinal[$strSymbol])) {
$arrFinal[$strSymbol] = [ 'id' => $obj->id, 'symbol' => $obj->symbol];
}
$arrFinal[$strSymbol]['dates'][] = $obj->the_date;
}
// Now loop throuh arrFinal and do print the statements you want.
foreach($arrFinal as $strSubSymbol => $arrData){
echo $strSubSymbol . ' '. count($arrData['dates']) . ' found on dates ' . implode(',', $arrData['dates']). PHP_EOL;
}
Function to get the desired subpart of symbol
function getSubSymbol($symbol_original){
$symbol = preg_split('/(?=\d)/', $symbol_original, 2); //get everything up until first number or the date in the string in this case.
$symbol_here = substr($symbol[0], 1);
return $symbol_here;
}
For a brief, I have a USER which has multiple INI Files uploaded to his account.
The Path and Filename are stored in the database.
So The INI file has SECTIONS then the parameter and the value.
This is my INI File sample:
[INIDetails]
SipUserName=
Password=
Domain=182.010.50.59
Proxy=5080
Port=
SipAuthName=
DisplayName=
ServerMode=Automatic
UCServer=192.168.50.5250
UCUserName=
UCPassword=
[DialPlan]
DP_Exception=
DP_Rule1=
DP_Rule2=
[Advanced]
OperationMode=1
MutePkey=16
Codec=
PTime=20
AudioMode=3
SoftwareAEC=
EchoTailLength=5
PlaybackBuffer=10
CaptureBuffer=10
JBPrefetchDelay=60
JBMaxDelay=160
SipToS=
RTPToS=00
LogLevel=
So now for values like Domain and Proxy, I store them in the database, and I let the user edit those values.
Now the problem:
If there is an update in database value, I loop through and I fetch all the files that user has and I want to update them. So I do this:
function update_ini($site_session)
{
$base_configs= $this->upload_ini_m->get_files();
//Now Here We Have Files That Has to be updated as per DATABASE PARAMS
$count = count($base_configs);
//WE KNOW THE NUMBER OF FILES TO UPDATED
for ($i=0; $i < $count; $i++)
{
$filename = $base_configs[$i]->base_ini_filename;
$path = $base_configs[$i]->file_path;
//Here We Get Final File To Process//
$file_to_update =$path.$filename;
// $file_contents = file_get_contents( $file_to_update);
//Parse the INI
$file_contents = $this->data['parameters'] = parse_ini_file($file_to_update,true);
//This is Our Array which has the values so replace the values here.
foreach ($file_contents as $key => $sections)
{
foreach ($sections as $parameter => $value)
{
//Update files here
}
}
}
}
Now I want to put the values that user inserted into database written against the parameter it has, If I update the Domain name then I just want to write the value I get from POSTand write it against the parameter.
How Can I Achieve This?
I Guess I can use STR_REPLACE but how I am not sure. I want to replace the values I get from post from the values in the File.
When I var_dump() $file_contents I Get this:
array (size=3)
'INIDetails' =>
array (size=11)
'SipUserName' => string '' (length=0)
'Password' => string '' (length=0)
'Domain' => string '182.010.50.59' (length=13)
'Proxy' => string '5080' (length=4)
'Port' => string '' (length=0)
'SipAuthName' => string '' (length=0)
'DisplayName' => string '' (length=0)
'ServerMode' => string 'Automatic' (length=9)
'UCServer' => string '192.168.50.5250' (length=15)
'UCUserName' => string '' (length=0)
'UCPassword' => string '' (length=0)
'DialPlan' =>
array (size=3)
'DP_Exception' => string '' (length=0)
'DP_Rule1' => string '' (length=0)
'DP_Rule2' => string '' (length=0)
'Advanced' =>
array (size=14)
'OperationMode' => string '1' (length=1)
'MutePkey' => string '16' (length=2)
'Codec' => string '' (length=0)
'PTime' => string '20' (length=2)
'AudioMode' => string '3' (length=1)
'SoftwareAEC' => string '' (length=0)
'EchoTailLength' => string '5' (length=1)
'PlaybackBuffer' => string '10' (length=2)
'CaptureBuffer' => string '10' (length=2)
'JBPrefetchDelay' => string '60' (length=2)
'JBMaxDelay' => string '160' (length=3)
'SipToS' => string '' (length=0)
'RTPToS' => string '00' (length=2)
'LogLevel' => string '' (length=0
I have array of columns and their values selected by user. I need to perform search based on these columns and values.
For instance, a user choose:
morning(column) => Thurs,Sun(values)
afternoon(column) => Sat(values)
Now, how do I search for records that match the columns and the values? I'm using PDO.
the arrays:
//columns
var_dump($cols);
array (size=3)
0 => string 'morning' (length=7)
1 => string 'morning' (length=7)
2 => string 'afternoon' (length=9)
var_dump($days);
//values
array (size=3)
0 => string 'Thrs' (length=4)
1 => string 'Sun' (length=3)
2 => string 'Sat' (length=3)
Sample Columns and values stored in the database,for a record.So , I want to use the above days and columns as search criteria.
In the following record, this user has
'morning' => string 'Thrs'
'afternoon' => string 'Fri,Sat'
'evening' => ''
My search criteria for morning is, Thurs and Sun. But this user only available on Thurs in the morning. SO 1 criteria is met.
Next for afternoon, this user available on Fri, Sat. Bu my search needs only Sat. Since either one is met so this criteria is met.
But let say my search criteria is evening only instead of morning and afternoon. Then this user will not selected as he/she not available on evenings.
Hope you understand how it goes..If not feel free to ask. Thank you.
array (size=1)
0 =>
array (size=25)
'postUUID' => string '5623853773026' (length=13)
'subid' => string '36' (length=2)
'subname' => string 'Muay Thai' (length=9)
'catID' => string '4' (length=1)
'catname' => string 'Martial Arts' (length=12)
'pricing' => string '200' (length=3)
'post_status' => string '0' (length=1)
'UUID' => string '562385374bddf' (length=13)
'Name' => string 'Kalaivani Nair' (length=14)
'Phone' => string '012345666' (length=9)
'Email' => string 'kalaivaninair#ymail.com' (length=23)
'location' => string 'kajang' (length=6)
'lat' => string '2.993518' (length=8)
'lon' => string '101.7874058' (length=11)
'DateReg' => string '2015-10-18 19:40:39' (length=19)
'Reputation' => string '0' (length=1)
'ReviewPlus' => string '' (length=0)
'ReviewNeg' => string '' (length=0)
'Sex' => string 'f' (length=1)
'centre' => string '0' (length=1)
'morning' => string 'Thrs' (length=4)
'afternoon' => string 'Fri,Sat' (length=7)
'evening' => string '' (length=0)
'catid' => string '4' (length=1)
'distance' => string '0' (length=1)
EDITED based on steve's answer
"avail":["Wed-2","Wed-3"]//JSON string
$data = json_decode($return, true);//decoded
$avail = $data['avail'];//stored into database
if($avail != ""){
foreach($avail as $k=>$v)
{
echo $v;
$array = explode('-', $v);
$day =$array[0]; // Languages
$column = $array[1]; // English
echo"<br/>";
if($column == 1)
{
$col = "morning";
}
if($column == 2)
{
$col = "afternoon";
}
if($column == 3)
{
$col = "evening";
}
echo $col ."=>". $day;
echo $sql=" SELECT * , (3956 * 2 * ASIN(SQRT( POWER(SIN(('$lat' - lat) * pi()/180 / 2), 2) +COS('$lat' * pi()/180) * COS(lat * pi()/180) * POWER(SIN(('$lon' - lon) * pi()/180 / 2), 2) ))) as distance
from posts,subjects WHERE ('posts.".$col."' LIKE '%".$day."%') AND posts.catID = '$catid' AND posts.subname LIKE '%$subject%' AND posts.subid = subjects.subid AND posts.catID = subjects.catid AND posts.pricing <= '$rate' having distance <= '$distance' order by distance ";
echo"<br/>";
// array_push($cols,$col);
// array_push($days,$day);
}
}
$stmt =connection::$pdo->prepare($sql);
$stmt->execute();
$place=array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$place[] = $row;
}
Use the LIKE function in MySQL to search for strings within your columns.
In your first example ,so search for either a Thursday or Sunday morning:
SELECT * FROM `tableName` WHERE (`morning` LIKE '%Thrs%' OR `morning` LIKE '%Sun%');
To search for either a Friday or Saturday afternoon:
SELECT * FROM `tableName` WHERE (`afternoon` LIKE '%Fri%' OR `afternoon` LIKE '%Sat%');
This would be sufficient for your scenario where the list of potential values is short. However in conventional relational databases it is often good practice to move columns with multiple values to a separate table rather than using comma seperated values within a single column. This scales better through the use of indexing and offers more flexibility in terms or the queries you can run.
i'm working on a small text based web game for a few friends to play (for nostalgia) but i'm having trouble with the very core of the game, the attacking/defending.
Each player has x amount of each troop type (more can be purchased and will be lost on atk/def)
so far I have a table with troop types and stats and a table with troop qty linked with user id and troop id.
I'm using the user id to get the troop id and qty using;
// Get attacker troop quantities.
$i = 0;
$attacker_deets = $conn->prepare("SELECT * FROM troop_qty WHERE user_id= :attacker_id");
$attacker_deets->bindParam(':attacker_id', $attacker_id);
$attacker_deets->execute();
$attacker_deets_results = $attacker_deets->fetchAll(PDO::FETCH_ASSOC);
foreach($attacker_deets_results as $atk_key=>$atk_result) {
echo "<pre>"; var_dump($atk_result); echo "</pre>";
}
which outputs;
array (size=4)
'troop_qty_id' => string '1' (length=1)
'user_id' => string '2' (length=1)
'troop_id' => string '1' (length=1)
'qty' => string '100' (length=3)
array (size=4)
'troop_qty_id' => string '2' (length=1)
'user_id' => string '2' (length=1)
'troop_id' => string '2' (length=1)
'qty' => string '100' (length=3)
I'm then using the troop id to get the troop details;
// get attacker troop details.
$attacker_troop_deets = $conn->prepare("SELECT * FROM troops WHERE troop_id= :atk_troop_id");
$attacker_troop_deets->bindParam(':atk_troop_id', $attacker_deets_results[$i]['troop_id']);
$attacker_troop_deets->execute();
$returned_results = $attacker_troop_deets->fetchAll(PDO::FETCH_ASSOC);
foreach($returned_results as $key=>$result) {
echo "<pre>"; var_dump($result); echo "</pre>";
}
++$i;
which in total gives me;
array (size=4)
'troop_qty_id' => string '1' (length=1)
'user_id' => string '2' (length=1)
'troop_id' => string '1' (length=1)
'qty' => string '100' (length=3)
array (size=4)
'troop_id' => string '1' (length=1)
'troop_name' => string 'Fist Puncher' (length=12)
'troop_atk' => string '1' (length=1)
'troop_def' => string '1' (length=1)
array (size=4)
'troop_qty_id' => string '2' (length=1)
'user_id' => string '2' (length=1)
'troop_id' => string '2' (length=1)
'qty' => string '100' (length=3)
array (size=4)
'troop_id' => string '2' (length=1)
'troop_name' => string 'Stick Waver' (length=11)
'troop_atk' => string '2' (length=1)
'troop_def' => string '1' (length=1)
Now where i'm stuck is I need to be able to compare the troop_atk of First Puncher with the defence of Stick Waver which is gotten from a different set of duplicate query's getting the defenders details, i also need to be able to multiply and divide the atk and def variables.
so how would i go about achieving this? i would assume i would need to give each field in the array their own variable, but how? i have tried using ${"troop_name" . $i} = $attacker_deets_results[$i]['troop_name']; but it only ever outputs the last entered name.
any help would be amazing. thanks.
Edit: To clear things up a little, my goal is to get the troop qty and multipy it by both atk and def then use these numbers to do some other math with the same fields from the defending player which I'll use to decrease the qty field.
Maybe you could modify your code a little and do the following.
Fetch the data about the user's troops in one query:
$results = $conn->query("SELECT troop_qty.user_id as uid, troop.troop_id, troop.troop_name, troop_qty.qty, troop.troop_atk, troop.troop_def FROM troop_qty join troop on troop.troop_id=troop_qty.troop_qty_id WHERE troop_qty.user_id=:attacker_id
Create a new array which contains information about all user's troops:
$userData = array(
'id' => $attacker_id,
'troops' => array());
while (($res = $results->fetch_assoc()) != null) {
$troopData = array(
'name' => $res['troop_name'],
'qty' => $res['qty'],
'atk' => $res['troop_atk'],
'def' => $res['troop_def']);
$userData['troops'][$res['troop_id']] = $troopData;
}
As result you would have something like this (vardump($userData)):
array (size=2)
'id' => int 2
'troops' =>
array (size=2)
1 =>
array (size=4)
'name' => string 'Fist Puncher' (length=12)
'qty' => string '100' (length=3)
'atk' => string '1' (length=1)
'def' => string '1' (length=1)
2 =>
array (size=4)
'name' => string 'Stick Waver' (length=11)
'qty' => string '100' (length=3)
'atk' => string '2' (length=1)
'def' => string '1' (length=1)
I don't know how exactly you want to manipulate the data later on in the game but I think this would ease the work.
Btw. I've thrown away fetchAll() function because I personaly think it is not a good practice to fetch all data into memory and then modify it or do whatever with it. It's a waste of the memory and when you would deal with a big amount of data, at some point that could cause you some troubles.
I have a var dump of my sql query which return the following
I wanna to count in the array below that how many rows of myID = 5 are there. How would I do that. I am using php. Thanks in advance
array
0 =>
object(stdClass)[17]
public 'myID' => string '5' (length=1)
public 'data' => string '123' (length=3)
1 =>
object(stdClass)[18]
public 'myID' => string '5' (length=1)
public 'data' => string '123' (length=3)
2 =>
object(stdClass)[19]
public 'relativeTypeID' => string '2' (length=1)
public 'data' => string '256' (length=3)
3 =>
object(stdClass)[20]
public 'myID' => string '4' (length=1)
public 'data' => string '786' (length=3)
object(stdClass)[21]
public 'myID' => string '4' (length=1)
public 'data' => string '786' (length=3)
Do you always have the same value of data for the same myID? In other words, is data functionally dependant on myID?
If so, you can get the database to do this for you:
SELECT myID, data, COUNT(*) AS cnt
FROM (your query here)
GROUP BY myID, data
This would give you results like the following:
myID data cnt
'5' '123' 3
'2' '256' 1
'4' '786' 2
Or, you can use a foreach statement, like:
$count = 0;
foreach($arr as $item)
{
// Given that your item is an stdClass Object you access its property with "->"
// if an array $item["myID"] instead
if ( $item->myID == '4' )
{
$count ++;
}
}