Jump to content

SetToLoki

Members
  • Posts

    123
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

SetToLoki's Achievements

Member

Member (2/5)

0

Reputation

  1. solved: schoolboy error I in the last function I never told the is connected variable to be false after mysqli->close() you laugh now, but it happens to the best of us
  2. Same as before query works in query window but not when called by php.
  3. yeah, your right about the variables lol I missed thsat, this it of code has never been asked to run yet, so not hit that problem yet, but thanks for freeing me of the pain later lol!
  4. it doesn't return anything and the mysqli->errorno is 0 the query runs fine in mysql directly just not via the site just tried changing it to an insert on duplicate key update again query runs in mysql but once I put it in the script it won't run, so I am guessing there is something wrong with the insert_id that is being returned but if I echo $insert_id out it is null. I don't seem to be able to generate any errors from mysql and the query is being asked to run, but it must be the query failing as nothing is being added to the database before the function fails.
  5. on a side note, I am only worried at this point about getting the INSERT query to work, and I have had to take out the real_escape_strings caus if they are uncommented they return null values for everything?? I am not sure why mysqli->error does not return anything and the only other method I reference in the above function is DBConnect private function DBConnect() { $mysqli = new mysqli(SERVER, USER, PASSWORD, DATABASE); if ( mysqli_connect_errno() ) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $this->mysqli = $mysqli; $this->connected = true; }
  6. private function AddBusinessData( $user_id, $Forename, $Surname, $BusinessName = "", $AddressOne = "", $AddressTwo = "", $Number = "", $PostCode = "", $City = "", $County = "", $Country = "", $Title = "", $business_id = 0 ) { if( !$this->connected ) { $this->DBConnect( ); } //$Forename = $this->mysqli->real_escape_string( $Forename ); //$Surname = $this->mysqli->real_escape_string( $Surname ); //$BusinessName = $this->mysqli->real_escape_string( $BusinessName ); //$AddressOne = $this->mysqli->real_escape_string( $AddressOne ); //$AddressTwo = $this->mysqli->real_escape_string( $AddressTwo ); //$Number = $this->mysqli->real_escape_string( $Number ); //$PostCode = $this->mysqli->real_escape_string( $PostCode ); //$City = $this->mysqli->real_escape_string( $City ); //$County = $this->mysqli->real_escape_string( $County ); //$Country = $this->mysqli->real_escape_string( $Country ); //$Title = $this->mysqli->real_escape_string( $Title ); //UPDATE or INSERT? if( $business_id == 0 ) { //Create New Entry $sql = "INSERT INTO `smartit`.`businesses` (`businesses`.`user_id`,`businesses`.`BusinessName`,`businesses`.`AddressOne`,`businesses`.`AddressTwo`,`businesses`.`Number`,`businesses`.`PostCode`,`businesses`.`City`,`businesses`.`County`,`businesses`.`Country`,`businesses`.`Forename`,`businesses`.`Surname`,`businesses`.`Title`) VALUES ( '$user_id', '$BusinessName', '$AddressOne', '$AddressTwo', '$Number', '$PostCode', '$City', '$County', '$Country', '$Forename', '$Surname', '$Title' )"; } else { //Update Current Record $sql = "REPLACE INTO `businesses` (`$business_id`, `user_id`,`BusinessName`,`AddressOne`,`AddressTwo`,`Number`,`PostCode`,`City`,`County`,`Country`,`Forename`,`Surname`,`Title`) VALUES ( $business_id, $user_id, '$BusinessName', '$AddressOne', '$AddressTwo', '$Number', '$PostCode', '$City', '$County', '$Country', '$Forename', '$Surname', '$Title' )"; } $this->mysqli->query($sql); $insert_id = $this->mysqli->insert_id; $error = $this->mysqli->error; $this->mysqli->close(); if( $insert_id > 0 ) { return $insert_id; } else { $this->error = "FAILED TO INSERT BUSINESS DATA TO DATABASE <br />".$error."<br />".$sql; return false; } } Example Query INSERT INTO `smartit`.`businesses` (`businesses`.`user_id`,`businesses`.`BusinessName`,`businesses`.`AddressOne`,`businesses`.`AddressTwo`,`businesses`.`Number`,`businesses`.`PostCode`,`businesses`.`City`,`businesses`.`County`,`businesses`.`Country`,`businesses`.`Forename`,`businesses`.`Surname`,`businesses`.`Title`) VALUES ( '2', '', '', '', '01257-858746', '', '', '', '', 'Jimmy', 'Biggear', 'Mr.' ) Hi, this code is driving me nuts. mysqli does not return an error and the query will work if I paste it directly into mysql. I've changed the query over and over trying to get it to work but no matter what I do the function returns false and nothing is inserted into the database. I have a feeling it will be some obvious mistake, just not one that is obvious to me Thanks in advance for any help. Tom
  7. Thanks I knew it was there somewhere just couldn't think of it
  8. I just created a form that allows people to insert their email, but when the data is passed to the next page my @ sign is converted to %40 is there a php function to change this back to an @ sign? I tried htmlentities but it doesn't look for %40, any help appriciated. Cheers, Tom
  9. yes if you return $this you can then make an object like so SetUser($user){ $this->user = $user return $this; } SetPass($pass){ $this->pass = $pass; return $this; } SomeFunc(){ echo "{$this->pass} <br /> {$this->user}"; } //usage $user->SetUser("name")->SetPass("pass")->SomeFunc();
  10. no problems, just keep forgetting to close connections etc, wanted a tidier system so thought I might use sessions so I could follow what connections are being made, as I used multiple classes all of which make database connections at different time for different reasons so wanted to make some system that means when a connection is required it would use an open one or open a new one if none exists rather than just creating a new one regardless of an open unused (probably because some messy code forgot to close it), I thought about making a class that just manages the database connections all other classes that connect to the database do so through this class. if that makes sense - I sort of know what I am after but struggle to put it into words
  11. ahh that explains that, is there a way to limit the amount of connections used, it's hard to explain what I am trying to do I am trying to cut down the database connections made to one per users by giving them a connection when they log in and anything that requires that requires a db connection can be done using the one stored in their session rather than creating a new one when they need it etc.
  12. the class is going but in the class I create a mysqli object and store it in a variable within the class heres my class <?php class users { private $user; private $user_id; private $password; private $db_connection; private $errors; function initiate_connection(){ $this->db_connection = new mysqli(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DATABASE); } function getLiveDatabaseConnection(){ if(!is_object($this->db_connection)){ $this->initiate_connection(); return $this->db_connection; }else { return $this->db_connection; } } function setUserName($username){ $this->user = $username; return $this; } function setPassword($password){ $this->password = $password; return $this; } function setUserID($uid){ $this->user_id = $uid; return $this; } function getUserInformation(){ if(isset($this->user_id)){ $uid = $this->user_id; } else { $this->errors[] = "getUserInformation() :: No User ID Set"; return false; } $sql = "select * from berserke_Rogue.users WHERE UserID = '$uid'"; $mysql = $this->getLiveDatabaseConnection(); $result = $mysql->query($sql); if(!$result){ $this->errors[] = "getUserInformation() :: Database Connection Fail <br /> $sql <br /> {$this->db_connection->error}"; return false; } else { $row = $result->fetch_assoc(); echo $row['UserName']." ".$row['UserPass']; } } function ShowErrors(){ if(is_array($this->errors)){ foreach ($this->errors as $error){ echo $error."<br />"; } } else { echo $this->errors; } } } ?> I have altered it abit since my original draft but this is jist of it. on page one I would do <?php session_start(); require_once('phpclasses/class_users.php'); $user = new users(); $user->setUserName("tom")->setUserID("1"); $user->getUserInformation(); //Set Session Variables $_SESSION['oUser'] = serialize($user); var_dump($user); ?> the var_dump for this page object(users)#1 (5) { ["user:private"]=> string(3) "tom" ["user_id:private"]=> string(1) "1" ["password:private"]=> NULL ["db_connection:private"]=> object(mysqli)#2 (0) { } ["errors:private"]=> NULL } page 2 goes like this <?php require_once('phpclasses/class_users.php'); session_start(); $user = unserialize($_SESSION['oUser']); $user->getUserInformation(); var_dump($user); ?> the var dump and error code is Warning: mysqli::query() [mysqli.query]: Couldn't fetch mysqli Warning: users::getUserInformation() [users.getuserinformation]: Couldn't fetch mysqli object(users)#1 (5) { ["user:private"]=> string(3) "tom" ["user_id:private"]=> string(1) "1" ["password:private"]=> NULL ["db_connection:private"]=> object(mysqli)#2 (0) { } ["errors:private"]=> array(1) { [0]=> string(117) "getUserInformation() :: Database Connection Fail select * from berserke_Rogue.users WHERE UserID = '1' " } }
  13. Hi, I have created a small class, that initiates a mysqli connection within it and stores it as an object. the class works fine untill I try to store it in a session and pass it to a new page. I define the class before I call session start, and the class in itself gets copied across, its just the mysqli connection that doesn't. I get the error Warning: mysqli::query() [mysqli.query]: Couldn't fetch mysqli Warning: users::getUserInformation() [users.getuserinformation]: Couldn't fetch mysqli var_dump of class object object(users)#1 (5) { ["user:private"]=> string(3) "tom" ["user_id:private"]=> string(1) "1" ["password:private"]=> NULL ["db_connection:private"]=> object(mysqli)#2 (0) { } ["errors:private"]=> array(1) { [0]=> string(117) "getUserInformation() :: Database Connection Fail select * from database.users WHERE UserID = '1' " } }
  14. Thanks to you all for your advice could you explain a little more about the :: in "Registry::getInstance()" or at least point me to what to search for not sure of its's name or what it does also object's destructors are new to me, sound fun though
  15. I have created a class to manage users in a database. It's the first class I have ever made, now it all works or at least seems to but as it is the first time I have tried anything like this would like some gurus to give it a once over and point out if they can see any flaws mistakes or better ways to do things. here's my class <?php class users{ private $mysql; private $pass; public $id; public $user; public $userlevel; public $error = ""; function __construct($db_name, $db_user, $db_pass, $db_table){ $this->mysql = new mysqli($db_name, $db_user, $db_pass, $db_table); } function SetUser($user){ $this->user = $user; return $this; } function SetPass($pass){ $pass = sha1($pass); $this->pass = $pass; return $this; } function SetUserLevel($userlevel){ $this->userlevel = $userlevel; return $this; } function SetId($id){ $this->id = $id; return $this; } function AddUser(){ if(!$this->CheckUser()){ $username = $this->mysql->real_escape_string($this->user); $password = $this->mysql->real_escape_string($this->pass); $userlevel = $this->mysql->real_escape_string($this->userlevel); $sql = "INSERT INTO users (username, password, userlevel) VALUES ('$username', '$password', '$userlevel')"; if(!$this->mysql->query($sql)){ $this->mysql->free(); return true; } else { return false; $this->error .= "Failed to Add User to Database. <br />"; $this->error .= $this->mysql->error."<br />"; } } else { return false; $this->error .= "User All Ready Exists. <br />"; } } function EditUser(){ $username = $this->mysql->real_escape_string($this->user); $password = $this->mysql->real_escape_string($this->pass); $userlevel = $this->mysql->real_escape_string($this->userlevel); $id = $this->id; if(!$this->CheckUser()){ $sql = "INSERT INTO users (username, password, userlevel) VALUES ('$username', '$password', '$userlevel')"; } else { $sql = "UPDATE users SET username = '$username', password = '$password', userlevel = '$userlevel' WHERE id = '$id'"; } if(!$this->mysql->query($sql)){ $this->mysql->free(); return true; } else { return false; $this->error .= "Failed to Edit User. <br />"; $this->error .= $this->mysql->error."<br />"; } } function CheckUser(){ $username = $this->mysql->real_escape_string($this->user); $password = $this->mysql->real_escape_string($this->pass); $sql = "SELECT id FROM users WHERE username='$username' AND password='$password' LIMIT 1"; $result = $this->mysql->query($sql); $row_cnt = $result->num_rows; $result->free(); if($row_cnt > 0){ return true; } else { return false; } } function DeleteUser(){ $id = $this->id; $sql = "DELETE FROM users WHERE id = '$id'"; $result = $this->mysql->query($sql); if($result){ return true; $result->free(); } else { $this->error .= "Failed to Delete User. <br />"; $this->error .= $this->mysql->error."<br />"; return false; } } function CloseMysql(){ $this->mysql->close(); } } ?> and usage <?php require_once('php/users.php'); $user = new users($db_host, $db_user, $db_pass, $db_name); $user->SetUser('bobo')->SetPass('test')->AddUser(); echo $user->error; if($user->CheckUser()){ echo "User Exists"; }else{ echo "User Doesn't Exist"; } ?> thanks in advance for any input
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.