Alffallen Posted April 1, 2007 Share Posted April 1, 2007 Hello I'm working on learning to write good php. I'm by night a .NET student at a trade school. So I'm learning the same concepts there. Just not having the smoothest transition of working it into php. Here is a quick over view of what I'm starting with. I'm working on writing a classes file to handle some routine procedures for me. The first one of course has a function that should be loading an array for the logged in user. <?php #User Info class to load user info Class inventory { #load a variable slot and set it equal to the characterName kept in session var $user; #query the users table for the row of the selected player function userinfo($user){ mysql_select_db($database_thegame, $thegame); $result = mysql_query("SELECT * FROM users where users.characterName ='$user'"); $row_reuslt = array(mysql_fetch_assoc($result)); mysql_query($result, $thegame) or die(mysql_error()); return $row_result; } }?> The second class I have written so far has a function with three arguements. The field I want updated. The value I want inserted into that field. Then the username to select the row to update. My code thus far is as follows. <?php #Updating a single value within the database related to the user. Class updateone { var $field; var $fieldValue; var $user; Function updatefield ($field, $fieldValue, $user) { mysql_select_db($database_thegame, $thegame); $update = mysql_query("UPDATE '$users' SET '$field' = '$fieldValue' WHERE characterName = '$user'"); mysql_query($update, $thegame) or die(mysql_error()); $result = "updated " . $field . " with " . $fieldValue . " for character " . $user . "."; return $result2; } } ?> These are both within the same script with my connection string include. However I get the following output Value: Object id #2 This is the page im using and im not sure which the problem is in.. The table is not updated with the passed values. Or I just don't understand how to call or return my function results.. Any help appreciated. <?php #this is my classes script include('inventory.php'); #setting a variable equal to session username $characterName=$_SESSION['MM_Username']; #attempting to call the first function. $userRow = new inventory($characterName); #display array results foreach ($userRow as $value) { echo "Value: " . $value . "<br />"; } #pass arguements to 2nd function for updating $updateMoney = new updateone(money,500000,$characterName); echo $updateMoney; ?> Matthew Todd Link to comment https://forums.phpfreaks.com/topic/45199-new-to-classes-and-functions/ Share on other sites More sharing options...
trq Posted April 1, 2007 Share Posted April 1, 2007 You really need to read some tutorials by the look of it, your code is quite all over the place. For your first example you would need something like.... <?php class inventory { function userinfo($user){ mysql_select_db($database_thegame, $thegame); if ($result = mysql_query("SELECT * FROM users where users.characterName ='$user'")) { if (mysql_num_rows($result)) { return mysql_fetch_assoc($result)); } } return false; } } $inv = new inventory() $user = $inv->userinfo('foo'); foreach ($user as $udata) { echo $udata['fldname']; } ?> That might help you out some, but like I said, looks like you need more tutorials. Link to comment https://forums.phpfreaks.com/topic/45199-new-to-classes-and-functions/#findComment-219455 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.