mysql - Proper way of connecting to SQL with PHP and PDO -


this php file hosted on web server connects sql database. create account function works having issues getting values out of table once put in.just more context using http_get() function in game maker: http://docs.yoyogames.com/source/dadiospice/002_reference/asynchronous%20functions/http_get.html

sql table has 4 columns:
p-id - auto incrementing primary key
user - username value
pass - encrypted password value
savestring - important value string trying access game have created. system working decided change pdo , php novice

<?php       $mysql_server = "server";     $mysql_username = "username";     $mysql_password = "password";     $mysql_database = "database";     $mysql_table = "table";        try{         $conn = new pdo("mysql:host=$mysql_server;dbname=$mysql_database", $mysql_username, $mysql_password);         $conn->setattribute(pdo::attr_errmode, pdo::errmode_exception);     } catch(pdoexception $e){         echo "couldn't connect database";     }      $f = $_get['f']; //function operator     $puser = $_get['puser'];     $pword = $_get['pword'];     $pss =  $_get['pss'];        $salt = "supersecretsalt"; // super secret encoder     $epword = crypt($pword,$salt);       function create_account($conn, $mysql_table, $puser, $epword, $pss)      {         try{             $sql = "insert $mysql_table (user, pass, savestring) values('$puser','$epword','$pss') ";             $conn->exec($sql);         } catch (pdoexception $e){             echo "exception connecting server";         }         $conn = null;     }     // function save information existing account     function save_info($conn, $mysql_table, $puser, $pword, $pss)     {            try{             $sql = "update $mysql_table set savestring = $pss  user = '$puser'";             $conn->exec($sql);         } catch (pdoexception $e){             echo "save didn't work";         }         $conn = null;     }     // function pull account information     function load_info($conn, $mysql_table, $puser, $epword)     {         try{             $statement = $conn->prepare("select * $mysql_table user = '$puser' , pass = '$epword'");             $statement->execute();             $row = $statement->fetch();             echo $row['savestring'];         } catch(pdoexception $e){             echo "0";         }         $conn = null;     }       // determines function call based on $f parameter passed in url.     switch($f)     {         case na: create_account($conn, $mysql_table, $puser, $epword, $pss); break;         case sv: save_info($conn, $mysql_table, $puser, $epword, $pss); break;         case ld: load_info($conn, $mysql_table, $puser, $epword); break;     break;             default: echo"error";     }      ?> 

the cases of switch statement should in quotes since strings. had break on last case. give try,

switch($f) {      case 'na':            create_account($conn, $mysql_table, $puser, $epword, $pss);       break;      case 'sv':            save_info($conn, $mysql_table, $puser, $epword, $pss);       break;      case 'ld':            load_info($conn, $mysql_table, $puser, $epword);       break;      default:            echo "error"; } 

also case of $f important if na still fail. make sure lowercase use strtolower, http://php.net/strtolower, switch(strtolower($f)) {. alternatively put in multiple cases seems waste, example,

case 'na':  case 'na': 

with approach though na , na still aren't accounted for.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -