Alffallen Posted April 3, 2007 Share Posted April 3, 2007 Hello. Still trying to learn how to use functions ect. I've been looking at code and tutorials a bit. Though I'm still having problems. Here is my code: $username = updateField("money", "500000", "characterName", "Alffallen"); $thequery = mysql_query($query, $thegameconstring); Function updateField($field, $fieldValue, $method, $string) { // The update string $query = "Update '$table' Set '$field`='$fieldValue' where `$method`='$string'"; // Query the update // return $query; } ?> I don't think I am passing my arquements correctly. Thought this should be building a simple update query string and return it for querying. I was trying to make it update the passed variables within the function though I ran across a example that showed returning the query string to the variable storing the function call. Then executing the query. Any advice on what im doing wrong would be much appreciated. The script should be updating the value of the field money with 500000 where the field characterName is equal to Alffallen. All test data within my DB. Link to comment https://forums.phpfreaks.com/topic/45468-solved-function-calling-question/ Share on other sites More sharing options...
per1os Posted April 3, 2007 Share Posted April 3, 2007 <?php // this needs to be set to $query as it is returning the query // if you do not want to re-define $query than make it a global variable // just like I made $table below. $query = updateField("money", "500000", "characterName", "Alffallen"); $thequery = mysql_query($query, $thegameconstring); function updateField($field, $fieldValue, $method, $string) { global $table; //makes $table accessible to the function if defined outside of it // The update string // Where is $table set??? $query = "Update '$table' Set '$field`='$fieldValue' where `$method`='$string'"; // Query the update return $query; } ?> Link to comment https://forums.phpfreaks.com/topic/45468-solved-function-calling-question/#findComment-220766 Share on other sites More sharing options...
Barand Posted April 3, 2007 Share Posted April 3, 2007 $table and $field need to be enclosed in `...` and not '...' (as you have with `$method`) Link to comment https://forums.phpfreaks.com/topic/45468-solved-function-calling-question/#findComment-220779 Share on other sites More sharing options...
Alffallen Posted April 4, 2007 Author Share Posted April 4, 2007 Okay so I meant to have the $table variable pass within the arguements as well so my code now reads. <?php require('Connections/conectionfilenameishere.php'); #Note that my connection file is correct and works fine when updating through other scripts ?> <?php $username = updateField("users", "money", "500000", "characterName", "Alffallen"); $thequery = mysql_query($query, $thegame); Function updateField($table, $field, $fieldValue, $method, $string) { // The update string $query = "Update `$table` Set `$field`=`$fieldValue` where `$method`=`$string"; return $query; } ?> Though when you run the script no errors are reported still and the field money is not being updated with 500000 where characterName = Alffallen. So im not sure what my problem is now. Link to comment https://forums.phpfreaks.com/topic/45468-solved-function-calling-question/#findComment-220887 Share on other sites More sharing options...
trq Posted April 4, 2007 Share Posted April 4, 2007 $thequery = mysql_query($query, $thegame); Needs to be... $thequery = mysql_query($username); per your example. Link to comment https://forums.phpfreaks.com/topic/45468-solved-function-calling-question/#findComment-220903 Share on other sites More sharing options...
Alffallen Posted April 4, 2007 Author Share Posted April 4, 2007 Okay getting closer.. Thanks I guess I renamed the variable at some point testing syntax and forgot to switch it over. Heres the code now. <?php $username = updateField("users", "money", "500000", "characterName", "Alffallen"); mysql_select_db($database_thegame, $thegame); $thequery = mysql_query($username, $thegame) or die(mysql_error()); Function updateField($table, $field, $fieldValue, $method, $string) { // The update string $query = "Update `$table` Set `$field`=`$fieldValue` where `$method`=`$string"; return $query; } ?> I added the or die command to find out why it still was not working and I get the following mysql error. Unknown column 'Alffallen' in 'where clause' I'm not sure why this is though the update string should be reading update users set money = 500000 where characterName=Alffallen. So im not sure why the error says unknown column Alffallen since thats not the column im telling it to look for at all. Link to comment https://forums.phpfreaks.com/topic/45468-solved-function-calling-question/#findComment-220916 Share on other sites More sharing options...
trq Posted April 4, 2007 Share Posted April 4, 2007 Whats with all the backticks? $query = "UPDATE $table SET $field = '$fieldValue' WHERE $method = '$string'"; Link to comment https://forums.phpfreaks.com/topic/45468-solved-function-calling-question/#findComment-220918 Share on other sites More sharing options...
Alffallen Posted April 4, 2007 Author Share Posted April 4, 2007 Sweet thank you. Dunno someone above said to use ` instead of 's so I tried it. Still new to grasping php syntax. Problem is solved thanks all. Link to comment https://forums.phpfreaks.com/topic/45468-solved-function-calling-question/#findComment-220922 Share on other sites More sharing options...
trq Posted April 4, 2007 Share Posted April 4, 2007 Still new to grasping php syntax Sorry to inform you, but all those bits within your $query variable are SQL syntax. if you want to interact with a database using php you'll also need to learn another language, SQL. Its ok though, most of the time SQL is pretty straight forward. Thats not to say it can't be very powerful though. just something to keep in mind. Link to comment https://forums.phpfreaks.com/topic/45468-solved-function-calling-question/#findComment-220930 Share on other sites More sharing options...
per1os Posted April 4, 2007 Share Posted April 4, 2007 Remember that tables and columnnames CAN be surrounded by ` but values must always be surrounded by ' unless it is numeric which than it does not require either, but can still be surrounded by ' Link to comment https://forums.phpfreaks.com/topic/45468-solved-function-calling-question/#findComment-220946 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.