Jump to content

[SOLVED] function calling question.


Alffallen

Recommended Posts

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

<?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;


}  
?>

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.

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.