Ok i inherited a pretty bad DB Situation the Views and Tables are in use and can't or can only with a lot of work be changed.
I am trying to get a Php/Html based Tool to work with the DB.
Here is my Info:
try {
$conn = new PDO("sqlsrv:server=$serverName;Database = $database", $uid, $pwd);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->setAttribute(PDO::SQLSRV_ATTR_ENCODING, PDO::SQLSRV_ENCODING_UTF8);
} catch (PDOException $e) {
die("Error connecting to SQL Server " . $e);
}
This is my PDO connection and
select * from munition.dbo.V_Admin_Zündhütchen order by Kaliber desc
this my query.
V_Admin_Zündhütchen is a View.
When i fetch the return and dump it like this:
$row = $sql->fetch(PDO::FETCH_ASSOC)
var_dump($row);
I get this:
array (size=5)
'ID' => string '2' (length=1)
'Kaliber' => string '7,62 mm' (length=7)
'Hersteller' => string 'Vihtavuori' (length=10)
'Z�ndtyp' => string 'Boxer' (length=5)
'Bezeichnung' => string '1511' (length=4)
the � is supposed to be a ü
The byte sequence of what is shown as Z�ndtyp is 5A FC 6E 64 74 79 70
The Data itself is fine only the Columnames are affected:
array (size=5)
'ID' => string '24' (length=2)
'Kaliber' => string '12/70' (length=5)
'Hersteller' => string 'Rottweil' (length=8)
'Z�ndtyp' => string 'Boxer' (length=5)
'Bezeichnung' => string 'Schrotzünder' (length=13)
So why does it not resolve properly?
Related
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%'";
I want to get a multidimentional php array from MySQL database where I want to cast a field to float.
I'm using PHP 5.3 and MySQL 5
and this is my code:
*** My PDO class:
class DB {
private static $instance = NULL;
public static function getInstance() {
if (!self::$instance) {
try {
self::$instance = new PDO("mysql:host=" . SERVER . ";dbname=" . DtB, USR, PASS);
self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
self::$instance->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0);
self::$instance->query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'");
} catch (PDOException $e) {
echo 'Échec lors de la connexion : ' . $e->getMessage();
}
}
return self::$instance;
}
private function __clone() {
/* interdiction de cloner l'instance */
}
}
And I use it like this:
$datas1 = $db->query('
SELECT
r.nom AS nomReg,
COUNT(e.regions) AS nbr
FROM regions r
LEFT OUTER JOIN event e ON r.nom=e.regions
GROUP BY r.nom
')->fetchAll(PDO::FETCH_ASSOC);
and :
$dataTypes=$db->query("
SELECT tt.nomtype_tache AS name,
CAST(((COUNT(t.nomtype_tache) / (
SELECT count(t.idtype_tache) AS y
FROM type_tache tt
LEFT OUTER JOIN tache t
ON tt.idtype_tache = t.idtype_tache
)
) * 100) AS DECIMAL(5,1)) AS y
FROM type_tache tt
LEFT OUTER JOIN tache t ON tt.idtype_tache = t.idtype_tache
GROUP BY name
")->fetchAll(PDO::FETCH_ASSOC);
and this the var_dump of these variables:
$datas1:
array (size=4)
0 =>
array (size=2)
'nomReg' => string 'ARIANA' (length=6)
'nbr' => int 1
1 =>
array (size=2)
'nomReg' => string 'BEJA' (length=4)
'nbr' => int 0
2 =>
array (size=2)
'nomReg' => string 'BEN AROUS' (length=9)
'nbr' => int 1
3 =>
array (size=2)
'nomReg' => string 'BIZERTE' (length=7)
'nbr' => int 0
$dataTypes:
array (size=3)
0 =>
array (size=2)
'name' => string 'type1' (length=5)
'y' => string '0.0' (length=3)
1 =>
array (size=2)
'name' => string 'type2' (length=5)
'y' => string '0.0' (length=3)
2 =>
array (size=2)
'name' => string 'type3' (length=5)
'y' => string '13.3' (length=4)
I don't no what is the problem that don't make the 'y' field to be float???
I'm trying to write an array or object to a file in plain-text (Not Binary), however whenever I loop over the array and look at the file I'm receiving Binary instead.
$this->user and $this->temp_files['in_file_name'] are passed into the function prior.
$user_array = (array)$this->user;
$data = "";
foreach ($user_array as $key => $value) {
$data .= "$key $value\n";
}
file_put_contents($this->temp_files['in_file_name'], $data);
If i just pass a string into $data it gets written to the file as plain-text, but when I loop over the object or array I receive binary. Is there a way to get the plain-text version of this array in the file?
$this->user is a database row as an object from Ion_Auth called by:
$this->user =$this->ion_auth->user($this->user_id)->row();
Here is a portion of the output of the file
4461 7465 2054 7565 7364 6179 204f 6374 6f62 6572 2033 302c 2032 3031 3269 6420 320a 6970 5f61 6464 7265 7373 207f 0000 010a 7573 6572 6e61 6d65 2062 7261 6e64 6f6e 2062 6f73 7765 6c6c 0a70 6173 7377 6f72 6420 6539 3936 3630 6665 3039 3535 6663 3862 3431 6639 3838 3066 3639 6131 6664 6635
The var_dump of the user array is: (I've changed some values for data security reasons)
array (size=22)
'id' => string '2' (length=1)
'ip_address' => string '��' (length=4)
'username' => string 'brandon' (length=15)
'password' => string 'changed' (length=40)
'salt' => null
'email' => string 'brandon#changed.net' (length=27)
'activation_code' => null
'forgotten_password_code' => null
'forgotten_password_time' => null
'remember_code' => string 'changed' (length=40)
'created_on' => string '1337702147' (length=10)
'last_login' => string '1351607850' (length=10)
'active' => string '1' (length=1)
'first_name' => string 'Brandon' (length=7)
'last_name' => string 'changed' (length=7)
'company' => string 'changed' (length=17)
'phone' => string '804-814-changed' (length=12)
'store' => string 'mechanicsville' (length=14)
'delivery' => string '1' (length=1)
'receiveEmails' => string '1' (length=1)
'customerType' => string 'business' (length=8)
'user_id' => string '2' (length=1)
If I do a file_get_contents on the file it can be echoed back in correctly, but I wish the file were stored as plain-text.
Decoding the hex bytes that you've posted reveals the correct data. So I'll take a guess: PHP is writing the file perfectly, but the raw null bytes in the IP address (127.0.0.1) are confusing the OS or editor you are using to open the file, into thinking that this is not a text file, so it's displaying it in a hex editor view for you. Try opening the file in a different editor, or saving the 'ip_address' field as text.
I'am at loss here. Not sure why I'm getting "stdClass". Shouldn't I be getting name from 1st column?
$sql = "SELECT cn, iso2, iso3, fid, sort FROM ct";
$stmt = $dbh->query($sql);
$result = $stmt->fetch( PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE );
var_dump($result);
//expected - first: cn.col holds country names
object(Brazil)[3]
public 'iso2' => string 'BR' (length=2)
public 'iso3' => string 'BRA' (length=3)
public 'fid' => string '1' (length=1)
public 'sort' => string '0' (length=1)
//received
object(stdClass)[3]
public 'iso2' => string 'BR' (length=2)
public 'iso3' => string 'BRA' (length=3)
public 'fid' => string '1' (length=1)
public 'sort' => string '0' (length=1)var_dump
The issue is most likely that the class is not defined (or in scope). I did some testing using local data.
$conn = new PDO("mysql:host=localhost;dbname=test", 'user', 'pass');
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
$stmt = $conn->prepare("SELECT name,email FROM `test_table`");
$stmt->execute();
$result = $stmt->fetch( PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE );
print_r($result) // returns stdClass Object ( [email] => 'test#test.com' )
By adding this above the PDO code
Class levi{
}
I was able to get it to return:
levi Object ( [email] => 'test#test.com')
The error message
[23-Mar-2010 08:36:16] PHP Fatal
error: Cannot redeclare humanize()
(previously declared in
/Users/tmclssns/Sites/nadar/nadar/trunk/webapp/application/filer/models/Filer/Aggregate.php:133)
in
/Users/tmclssns/Sites/nadar/nadar/trunk/webapp/application/filer/models/Filer/Aggregate.php
on line 133
I have a "Filer" model which contains several methods to generate graphs. Each method in there related to generating graphs has the suffix "Graph" in the method name. As we have some performance issues, I try to render the graphs in advance (using cron) instead of rendering them on each request. The code below is what I came up with:
public function generategraphsAction()
{
$this->_helper->viewRenderer->setNoRender();
$config = Zend_Registry::get('config');
$id = $this->_getParam('filerid');
$filer = new Filer($id);
$filer_methods = get_class_methods($filer);
foreach ($filer_methods as $filer_method) {
if (preg_match('/^(.*)Graph$/i', $filer_method, $matches)) {
$path = $config->imaging_caching_dir . "/$id/{$matches[1]}.png";
$filer->$matches[0]($path);
}
}
// var_dump(get_class_methods($filer)); die;
}
The result from the var_dump(), when uncommented, is:
array
0 => string '__construct' (length=11)
1 => string 'find_by_name' (length=12)
2 => string 'getPartner' (length=10)
3 => string 'getSlots' (length=8)
4 => string 'getGroups' (length=9)
5 => string 'grouplist' (length=9)
6 => string 'getAggregates' (length=13)
7 => string 'getVolumes' (length=10)
8 => string 'getAggregateVolumes' (length=19)
9 => string 'getShelves' (length=10)
10 => string 'getAutoSupportHistory' (length=21)
11 => string 'getAutoSupportMail' (length=18)
12 => string 'getOrphans' (length=10)
13 => string 'getAll' (length=6)
14 => string 'getDiskRevOverview' (length=18)
15 => string 'getDiskTypeOverview' (length=19)
16 => string 'getDiskTypeSizeFunctionOverview' (length=31)
17 => string 'getLicenses' (length=11)
18 => string 'removeGroup' (length=11)
19 => string 'addGroup' (length=8)
20 => string 'hasGroup' (length=8)
21 => string 'aggdefaultGraph' (length=15)
22 => string 'aggbarGraph' (length=11)
23 => string 'voldefaultGraph' (length=15)
24 => string 'volbarGraph' (length=11)
25 => string 'replicationGraph' (length=16)
26 => string 'getReplicationData' (length=18)
27 => string 'humanize' (length=8)
28 => string 'getFiler' (length=8)
29 => string 'getOptions' (length=10)
30 => string 'getCifsInfo' (length=11)
31 => string 'getCifsStats' (length=12)
32 => string '__get' (length=5)
33 => string 'tr' (length=2)
34 => string 'trs' (length=3)
35 => string 'fieldList' (length=9)
The generategraphsAction() method finds the 'Graph' methods correctly:
array
0 => string 'aggdefaultGraph' (length=15)
1 => string 'aggdefault' (length=10)
array
0 => string 'aggbarGraph' (length=11)
1 => string 'aggbar' (length=6)
array
0 => string 'voldefaultGraph' (length=15)
1 => string 'voldefault' (length=10)
array
0 => string 'volbarGraph' (length=11)
1 => string 'volbar' (length=6)
array
0 => string 'replicationGraph' (length=16)
1 => string 'replication' (length=11)
However when the first graph is generated, it generates the above listed PHP fatal error. Anyone can come up with a solution to this? I tried to pass by reference or switch a few things around (like re declare the Filer model, $current_filer = new Filer($id); and unset() it again after the request, but resulted in the same error) without much success.
The referenced method "humanize" isn't used for anything I'm doing at the moment, but belongs to the Model because it's used in several other places. Of course, removing the method is not really an option right now, and the model contains several other methods as well so I assume if I just move the humanize method around, it will generate an error on the next one.
For reference, the humanize() method:
public function humanize ($kbytes, $unit = null) {
// KiloByte, Megabyte, GigaByte, TeraByte, PetaByte, ExaByte, ZettaByte, YottaByte
$units = array('KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
if (null !== $units) {
$i = array_search(substr($unit, -2), $units);
if (! $i) {
$i = floor((strlen($kbytes) - 1) / 3);
}
} else {
$i = floor((strlen($kbytes) - 1) / 3);
}
$newSize = round($kbytes / pow(1024, $i), 2);
return $newSize . $units[$i];
}
Thanks in advance for the help offered.
I expect your parenthesise are wrong and your function humanize declaration is inside a while loop hence the redeclaration.
Unless of course you are including the file that defines this function twice somewhere?
public static function sums ($aggregates) {
function humanize(&$item, $key) {
$item = Filer::humanize($item);
}
$sums = array('size_total' => 0, 'size_usable' => 0, 'size_snapshot_reserve' => 0,
'size_snapshot_used' => 0, 'size_snapshot_free' => 0,
'size_active_fs_used' => 0, 'size_active_fs_free' => 0,
'size_active_fs_reserved' => 0);
foreach ($aggregates as $aggregate) {
if ($aggregate->state !== 'online') continue;
$sums['size_total'] += $aggregate->size_total;
$sums['size_usable'] += $aggregate->size_usable;
$sums['size_snapshot_reserve'] += $aggregate->size_snapshot_reserve;
$sums['size_snapshot_used'] += $aggregate->size_snapshot_used;
$sums['size_snapshot_free'] += $aggregate->size_snapshot_free;
$sums['size_active_fs_used'] += $aggregate->size_active_fs_used;
$sums['size_active_fs_free'] += $aggregate->size_active_fs_free;
$sums['size_active_fs_reserved'] += $aggregate->size_active_fs_reserved;
}
$humanSums = $sums;
array_walk($humanSums, 'humanize');
return array($sums, $humanSums);
}
The function inside function is the culprit.
Problem fixed.
Adding if(!function_exists('humanize')) block around the method declaration solved it.