Jump to content

Calling Function by Click and Result Throwing Error


OldWest

Recommended Posts

I've been hacking at this for about 4 hours now.. Throwing the towel in! Any feedback or suggestions please:

 

I'm calling a function like so:

 

<!--START :: Run report and update records -->
<p>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<!--<input type="hidden" name="post_var">-->
  <input type="submit" name="runUpdate" value="Run Update">
</form>
</p>
<?php
if (isset($_POST['runUpdate'])) {
		updateRecords();
	};
?>
<!--END :: Run report and update records -->

 

Here are the function details:

 

<?php
function updateRecords() {
// START :: Query to replace matches
mysql_query("UPDATE orig_codes_1a AS a
  					   JOIN old_and_new_codes_1a AS b ON concat(a.orig_code_1, a.orig_code_2) = concat(b.old_code_1, b.old_code_2)
				   SET a.orig_code_1 = b.new_code_1,
                           a.orig_code_2 = b.new_code_2") or die(mysql_error());
// END :: Query to replace matches

echo "<p><table border='1' cellpadding='3' width='100%'>";
echo "<tr>
<th>Updated Code 1</th>
<th>Updated Code 2</th>
</tr>";
// START :: While loop - keeps getting the next row until there are no more to get
while($row = mysql_fetch_array($result)) {
//START :: If to find matching criteria class it w/ bg color
if ($row['orig_code_1'] < '10000') {
		$changedClass = "changedClassCSS";
} else {
		$changedClass = "";
};
//END :: If to find matching criteria class it w/ bg color

//START :: Print out the contents of each row into a table
echo "<tr><td class=\"$changedClass\">"; 
echo $row['orig_code_1'];
echo "</td><td class=\"$changedClass\">"; 
echo $row['orig_code_2'];
echo "</td>"; 
}
echo "</table></p>";
//END :: Print out the contents of each row into a table

// START //
//echo "$tableOutput";
// END //
};
?>

 

I'm getting this error:

 

Notice: Undefined variable: result in C:\wamp\www\php_practice\apps\records_updater_2\functions.php on line 27

 

Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in C:\wamp\www\php_practice\apps\records_updater_2\functions.php on line 27

 

Now what I don't understand is 1) This script works fine (even though the $result is not defined) when all of the code is in one file not being called as a function. And 2) Why the hell would it work as in number 1??

 

Please please please tell me what I am doing wrong here!

 

You never define result anywhere in the function before you try to access it. Perhaps you meant to use $result = mysql_query( . . . on the third line of the function definition?

 

Fixing the above should resolve the mysql_fetch_array() expects . . . error as well. Right now the query executes, but nothing is done with the result resource, such as storing it in a variable.

I should have posted more data about what I tried so far. I added the $result to the query, and I get this:

 

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\php_practice\apps\records_updater_2\functions.php on line 27

 

I am not sure if this is true, but can and UPDATE query be stored in a variable? For some reason I thought SELECT could be but not UPDATE (I could be saying total nonsense here!)

 

Anyhow... It's just driving me bonkers cause I need to store something for an argument in that fetch_array, just don't know what  :'(

Jeez. I must be tired. I just saw the query being executed and didn't even notice it was actually an UPDATE. So, that brings up the next question: what are you trying to display with the mysql_fetch_array(), since there's no SELECT query in the function?

Pikachu2000, I think you just asked the right question. I'm probably just as tired as you missing this simple point. What hours of studying php do to the mind - melt...

 

Brain on php  :confused:

 

You made me wonder this simple question: How in the heck could I expect an UPDATE to output results?

 

So I added a SELECT query and it's all working now:

 

$result = mysql_query("SELECT * FROM orig_codes_1a") or die(mysql_error()); 

 

Now I just need to find out what the heck my app is doing  :shrug:

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.