PHP - Retreive Float From Pdo And Postgresql
The following returns strings instead of floats. Am I able to retrieve floats directly from PDO and/or PostgreSQL or must I manually type cast them afterwards using PHP? Thanks $sql='WITH RECURSIVE t AS (bla bla bla) SELECT id, CAST(SUM(slope*value+intercept) AS FLOAT) "value", SUM(slope*prev_value+intercept)::FLOAT "prevValue" FROM t WHERE type=\'physical\' GROUP BY id'; $stmt = $this->pdo->prepare($sql); $stmt->execute($ids); $arr = $stmt->fetchAll(\PDO::FETCH_UNIQUE); //returns [123=>['value'=>'123.456', 'prevValue'=>'122.234'], ...]
Similar Tutorialswhen a user forgets his or her password they can retreive it by sending an email to their account and the password is given to them. unfortunatly the password they recieve is in md5: for example the password 'the' is given in an email like so: Password:6e9b31333e61aad015fa16a3a5fe8e0d Username:the Please change your password as soon as you logon could someone please advise me what i am doing wrong so the password would be converted back to plain text. here is the code for the email: $query="Select pw from user where uname='$name'" or die(mysql_error()); $result= mysql_query($query); if(mysql_num_rows($result)>0){ for ($i=0; $i<mysql_num_rows($result); $i++) { $row = mysql_fetch_assoc($result); $pass= md5($row['pw']); $to="$em\r\n"; $from="From: Admin@asdesigns.co.uk\r\n"; $msg="Password:$pass\r\n"; $msg .="Username:$name\r\n"; $msg .="Please change your password as soon as you logon\r\n"; $subject="From Admin Your Login Password\r\n"; thanks in advance i know im doing something really silly by missing something out but i cant see where it is Hello, I have this code to retreive forgotten pass: Code: [Select] //here is my db connection// $result = mysql_query("SELECT email FROM db WHERE email='$email'"); if(!$result){ } else if($email!= mysql_result($result, 0)){ include 'recover.php'; echo "bla bla"; exit(); } $sql= mysql_query("SELECT pass FROM db WHERE email='$email'"); $result=mysql_query($sql); // if found e-mail address, row must be 1 row // keep value $count=mysql_num_rows($result); if($count==1){ $rows=mysql_fetch_array($result); // keep password $pass=$rows['pass']; exit(); } //here I have the Send Email code including: $pass I get no errors, but inside the email I only get the $email and not the $pass. Any help? Thank you So here is my array:
array(1) { ["84EA0353-58EB-E311-A8B1-001D09E7525D"]=> array(1) { [0]=> object(stdClass)#7 (1) { ["data"]=> array(2) { ["amt"]=> string(7) "100.000" ["unit"]=> string(3) "BUC" } } }}I'm trying to set the variable $x to = amt. I've tried $x= $ArrayMap->amt; this but it comes up null. I've also tried: $x= $ArrayMap['amt'];But both come up NULL. Any ideas on what I'm doing wrong? Thanks, sanchez Hi, You probably know where I am right now: after reading and coding for about an hour, you are happy with the solution you've come up with and that is working 'so-and-so', and then you do some more reading and coding only to find that suddenly everything stops working. At this moment, my db-session-class doesn't do *zilch* anymore. Could someone please look through the code and point me at something probably to stupid to mention? Code: [Select] <?php class Session { /* Required this Postgresql table: * CREATE TABLE session ( sessionid CHAR(32) NOT NULL, expiration INT NOT NULL, value TEXT NOT NULL, CONSTRAINT session_pk PRIMARY KEY(sessionid) ); */ public $sess_id; public $sess_data; public $sess_name; public $sess_life; public $sess_exp; private $_conn; /* open() * Opens a persistent server connection and selects the database. */ function open($sess_path, $sess_name) { if (! session_set_save_handler( array(&$this,'open'), array(&$this,'close'), array(&$this,'read'), array(&$this,'write'), array(&$this,'destroy'), array(&$this,'garbage_collect') )) { die('session_set_save_handler() failed'); } $this->sess_life = 18000; $this->_conn = @ pg_connect("host=localhost dbname=<snip> user=<snip> password=<snip>"); } // end function open() /* close() * Doesn't actually do anything since the server connection is * persistent. Keep in mind that although this function * doesn't do anything in this particular implementation, it * must nonetheless be defined. */ function close() { // Allegedly needed to write everything to db before closing // the object. session_write_close(); // On Debian and Ubuntu, garbage collection is not immediately // handled, so we call it here ourselves, just to make sure. $this->garbage_collect($this->sess_life); //pg_close($this->_conn); return 1; } // end function close() /* read() * Reads the session data from the database */ function read($sess_id) { $query = "SELECT value FROM session WHERE sessionid ='$sess_id' AND expiration > " . time(); $result = pg_query($this->_conn, $query); if (pg_num_rows($result)) { $row = pg_fetch_assoc($result); $value = $row['value']; return $value; } else { return ""; } } // end function select() /* write() * This function writes the session data to the database. * If that sessionid already exists, then the existing data will be updated. */ function write($sess_id, $sess_data) { $expiration = time() + $this->sess_life; $query = "INSERT INTO session VALUES('$sess_id', $expiration, '$sess_data')"; $result = pg_query($this->_conn, $query); if (! $result) { $query = "UPDATE session SET expiration = $expiration, value = '$sess_data' WHERE sessionid = '$sess_id' AND expiration >". time(); $result = pg_query($this->_conn, $query); } } // end function write() /* destroy() * Deletes all session information having input sessionid (only one row) */ function destroy($sess_id) { $query = "DELETE FROM session WHERE sessionid = '$sess_id'"; $result = pg_query($this->_conn, $query); } // end function destroy() /* garbage_collect() * Deletes all sessions that have expired. */ function garbage_collect($lifetime) { $lifetime = $this->sess_life; $old = time() - $lifetime; $query = "DELETE FROM session WHERE expiration < $old"; $result = pg_query($this->_conn, $query); return pg_affected_rows($result); } // end function garbage_collect() } ?> (I have been tinkering with that piece of code for more than 2 hours now, and I don't get any errors. Where at first, I got a row in my db (only the sessionid and expiration-columns were filled, no 'value' whatsoever...), now I don't get anything and NO errors... Anyway: This is how I initialize: Code: [Select] require_once 'classes/cls_session.php'; $S = new Session; session_start(); session_regenerate_id(true); thanks for any insights! Hey guys, I'm new here so please be gentle with me! I have this php command referencing my postgresql db at present: 'Select'=>$this->MyTable->GetAllSQLSelectFieldNames(array('perc'=>'((firstnumber)::float)/((secondnumber)::float) as perc')), It is possible for firstnumber to be greater than secondnumber, but I'd like to limit the array output to 1 - ie to represent that firstnumber is greater than or equal to secondnumber without ever returning a number greater than 1. Can anyone point me in the right direction for this please? Hi, Trying PostgreSQL for the first time but not making much progress. Get peer failure when not including a host and Ident error when including a host. Never heard of Ident authentication until today and don't know for sure if I even have such a server running. Using Centos7, PHP7.4 using remi's repo, and PostgreSQL 12 from their repo. Any thoughts? Thanks
try { //use Unix domain sockets $dbh = new PDO("pgsql:dbname=postgres", 'postgres', 'secret'); } catch(Exception $e){ echo($e->getMessage().PHP_EOL); } try { $dbh = new PDO("pgsql:host=localhost;dbname=postgres", 'postgres', 'secret'); } catch(Exception $e){ echo($e->getMessage().PHP_EOL); } try { $dbh = new PDO("pgsql:host=127.0.0.1;dbname=postgres", 'postgres', 'secret'); } catch(Exception $e){ echo($e->getMessage().PHP_EOL); }
SQLSTATE[08006] [7] FATAL: Peer authentication failed for user "postgres" SQLSTATE[08006] [7] FATAL: Ident authentication failed for user "postgres" SQLSTATE[08006] [7] FATAL: Ident authentication failed for user "postgres"
Hey again, was making a registration form with php and postgresql and im stuck at making a 'log out' button, i need to make something simple like - press on a link, it redirects u to the index page and if u try to go 'back' it wouldn't let u.. I found something on google like this: <a href="test.php?logout=1">Log out</a> and on test.php: <?php if(isset($_GET['logout'])){ session_unset; session_destroy; } ?> Succesfully logged out, go back to index page: <a href="index.php">Home</a> but i didnt use any sessions at all.. is there something like 'session_destroy' with postgre? i mean smth like 'pg_desroy'? i have a register and login page where the users can perform register and login task and their detail gets saved in the database. my requirement is that when the user enters his/her account, they should be redirected to their profile page, where they can view all their profile details. Till now i have been able to make the user login and dislay a part of their profile page, for this purpose the code that i am using is
<code>
<?php Hello guys, I have purchased a new book for PHP called Professional PHP 6. But the bad news is that the whole book was written for PostgreSQL NOT for MYSQL which I'm familiar with! I have this code, I have tried to do so many things to get it working! but nothing seems to have it working! Long story short, I have failed to convert the code to work with MySQL! Here is my code, in case some one will offer a help, or a reference to go to if some similar case comes up on my way. <?php class Widget { private $id; private $name; private $description; private $hDB; private $needsUpdating = false; public function __construct($widgetID) { //The widgetID parameter is the primary key of a //record in the database containing the information //for this object //Create a connection handle and store it in a private member variable //This code assumes the DB is called "parts" $this->hDB = pg_connect('dbname=parts user=postgres'); if(! is_resource($this->hDB)) { throw new Exception("Unable to connect to the database."); } $sql = "SELECT name, description FROM widget WHERE widgetid = $widgetID"; $rs = pg_query($this->hDB, $sql); if(! is_resource($rs)) { throw new Exception("An error occurred selecting from the database."); } if(! pg_num_rows($rs)) { throw new Exception("The specified widget does not exist!"); } $data = pg_fetch_array($rs); $this->id = $widgetID; } public function getName() { return $this->name; } public function getDescription() { return $this->description; } public function setName($name) { $this->name = $name; $this->needsUpdating = true; } public function setDescription($description) { $this->description = $description; $this->needsUpdating = true; } public function __destruct() { if($this->needsUpdating) { $sql = "UPDATE widget SET "; $sql .= "name = " . pg_escape_string($this->name) . ", "; $sql .= "description = " . pg_escape_string($this->description) . ""; $sql .= "WHERE widgetID = " . $this->id; $rs = pg_query($this->hDB, $sql); } pg_close($this->hDB); } } ?> FYI: I have tried mysql_pconnect ! results => failure! I have tried replacing the prefix "pg" to "mysql" or "mysqli"! results => failure! I have tried switching parameters, say in pg_query($resource, $query) TO mysql_query($query, $resource) results => failure too! Thank you in advance! A while back, I was showed how to authenticate to PostgreSQL using peer authentication over a socket for applications where PHP, FPM, and PostgreSQL are all on the same machine. All works. I could use native PHP as shown and it returns results without errors so I know that PHP, FPM, and PostgreSQL is setup correctly to establish a connection using peer authentication without a PostgreSQL username or password. $pdo = new PDO("pgsql:dbname=testing"); $rs = $pdo->query('SELECT * FROM company')->fetchAll(); I could also use Doctrine but not Symfony and get results without errors so I know that Doctrine is capable of establishing a connection using peer authentication without a PostgreSQL username or password. $pdo = EntityManager::create(['driver' => 'pdo_pgsql','dbname' => 'testing'], Setup::createAnnotationMetadataConfiguration([__DIR__."/../src"], true, null, null, false))->getConnection(); $rs = $pdo->query('SELECT * FROM company')->fetchAll(); Now I am trying to do the same but when using Symfony. I expected I could just edit config/packages/doctrine.yaml as follows, however, it results in An exception occurred in driver: SQLSTATE[08006] [7] fe_sendauth: no password supplied doctrine: dbal: driver: pdo_pgsql dbname: testing server_version: 13 Any thoughts how to do this? Thank you Can someone clear up my confusion about FLOAT. I have data in a TINYINT field that displays whole numbers WITHOUT any decimal places. This does not format very nicely when there are ALSO decimalled values to be echoed: 1 1.5 2 2.5, etc. It can be a little confusing. The only way I've thought of to resolve the issue (if you know another, please share) is to add a decimal place to get: 1.0 1.5 2.0 2.5 etc. But I am unclear on WHERE to put the commands to get this result. Does it go into the actual DB? Or into my PHP script? Please help with a detailed example. Thanks This topic has been moved to CSS Help. http://www.phpfreaks.com/forums/index.php?topic=312178.0 Hello, The data that actually gets stored in my database differs from what I'm trying to put there. Here's a short code snippet: $lat = 42.272939; $lng = -83.622941; // insert cordinates into dB $query = "INSERT INTO markers (lat, lng, user_id) VALUES ('$lat','$lng','$user_id')"; $results = mysql_query($query) or die(mysql_error()); When I look in the database, the data is stored as: 42.272938 and -83.622940. Any ideas as to why this happens? These values are being stored as float(10,6). Not sure if this is useful, but here's some other info: datbase: MySQL engine: InnoDB Format: Compact Collation: utf8_general_ci Thanks first person to solve this gets an automatic, irrevocable entry into my VIP Holiday Card Master List. ive been tussling with it for hours over two days and cannot figure out what the problem is.
site: http://104.152.168.18/~redtagsp/
you'll see five image buttons in the center of the homepage. red bg is for diagnostic only. the third of the smaller images in the top row is sitting slightly lower than the others. the containing element (transparent black) is floated as are all 5 images within it. and the text-div above the images. seems really simple but ive had these frigging elements bobbing all around and this is the best i can get them. my guess is there is some CSS outside of my own (this is a Wordpress site) that is screwing things up for me but i cant find it.
here is the HTML for the containing element:
<div id='home_buttons'> <div id='free_shipping'><strong>FREE</strong> Shipping on all orders over $40</div> <a href=''><img src='/~redtagsp/wp-content/themes/bmw_storefront_child/pics/buttons/button_blank_pucks.png' class='home_button_small'></a> <a href=''><img src='/~redtagsp/wp-content/themes/bmw_storefront_child/pics/buttons/button_custom_pucks.png' class='home_button_small'></a> <a href=''><img src='/~redtagsp/wp-content/themes/bmw_storefront_child/pics/buttons/button_custom_pucks.png' class='home_button_small'></a> <div style='float:left; clear:both; width:100%; height:5px;'></div> <a href='' class='link_button_large'><img src='/~redtagsp/wp-content/themes/bmw_storefront_child/pics/buttons/tape.png' class='home_button_large'></a> <a href='' class='link_button_large'><img src='/~redtagsp/wp-content/themes/bmw_storefront_child/pics/buttons/balls.png' class='home_button_large'></a> </div>here is the CSS directly associated with the above: #home_buttons { float:left; width:95.4%; padding:1%; margin:100px 1% 20px 1%; background:rgba(0, 1, 17, 0.75); border-radius:10px; } #free_shipping { float:left; width:100%; text-align:center; border-radius:5px; background:#555; color:#fff; margin-bottom:10px; } #home_buttons a { float:left !important; max-width:32.8%; margin:0 0.5% 0 0; padding:0px !important; background:red; clear:none !important; } .link_button_large { float:left !important; max-width:49% !important; margin:0px 2% 0px 0px; clear:none; } .link_button_large:last-of-type { margin:0px; } .home_button_small { float:left; max-width:100%; } .home_button_small:last-of-type { margin-right:0%; } im trying to update a GPS table for example: UPDATE markers SET lat = '38.40907441955112' AND lng = '14.943452775478363' WHERE ApID = '35911' i tried with float 20,18 it isnt updatng correctly I know I may sound like a nab, but here I go: Code: [Select] $ric=10; $price=$price+($price/$ric); Basically I want to add a 10% to the price value this is inside a "for", and price values is taken from a csv file. Said value is for example: $price=4,36 after the script takes place, the value goes to: $price=4796 While i aimed to obtain 4,80 (a 2 precision float). Can anyone help me? It's the first time I mess with float numbers. EDIT: Or at least direct me to a more efficient way to apply % on doubles, while preserving the original precision Hi, in Mysql have a row with FLOAT 10,2 In form put the number 20.20 and after reload in form show: 20.200000762939 |