needsomephphelp Posted March 19, 2013 Share Posted March 19, 2013 (edited) I have created a user defined function to return a string value function getModel($part) { $root = substr($part,0,4); $query = "SELECT Root, Model FROM tbl_Root WHERE Root = '$root'"; $result = mysql_query($query) or die ("Query Root failed: " . mysql_error()); while ($rec = mysql_fetch_array($result, MYSQL_ASSOC)) { if (in_array($root, $rec)): $Model = $rec['Model']; else: $Model = 'UNKNOWN'; endif; } return $Model; } When trying to find the attributes of several hundred part numbers, I pass them to a MySQL temp table that would then select the distinct part numbers and loop them through my functions. $query = "SELECT DISTINCT A FROM TempAttributes"; $result = mysql_query($query) or die ("Query failed: " . mysql_error()); while ($rec = mysql_fetch_array($result, MYSQL_ASSOC)) { $part = mysql_escape_string($rec['A']); $Model = getModel($part); $query = "REPLACE INTO tbl_Attributes (Part, Model) VALUES ('$part', '$Model')"; $endresult = mysql_query($query) or die ("Query Load failed: " . mysql_error()); } The line of code that stops the execution of my loop is $Model = getModel($part);I executed the loop with this line of code in and it stops after the first part number. When I comment this line of code out, my loop processes all of my part numbers, but of course not with the return values I am looking for. Why does my user defined function not process through the while loop to assign the $Model variable the return value of the getModel function? Please help! Edited March 19, 2013 by Zane Quote Link to comment https://forums.phpfreaks.com/topic/275881-assign-user-defined-function-return-value-to-a-variable-in-a-while-loop/ Share on other sites More sharing options...
requinix Posted March 19, 2013 Share Posted March 19, 2013 My first guess is that PHP doesn't think the function is defined. Very first debugging steps: make sure you have error_reporting = -1 display_errors = onin your php.ini, restart the web server if not (and after you add them), then try the page again. Now, what error(s) do you see? Quote Link to comment https://forums.phpfreaks.com/topic/275881-assign-user-defined-function-return-value-to-a-variable-in-a-while-loop/#findComment-1419656 Share on other sites More sharing options...
needsomephphelp Posted March 19, 2013 Author Share Posted March 19, 2013 My first guess is that PHP doesn't think the function is defined. Very first debugging steps: make sure you have error_reporting = -1 display_errors = onin your php.ini, restart the web server if not (and after you add them), then try the page again. Now, what error(s) do you see? Interesting, I tried that and now it appears that my script breaks when I try to assign the $Model variable the result of the getModel function. I dont get any errors, just a broken script. If I comment my $Model = getModel($part); line out, it processes. So theoretically I should be able to create a function return, and call that function on another page with an include to my function library and have a while loop pass through my $part array and assign the return value of that function to a variable? I dont know if the return is exiting the loop? Seems like it my problem is strictly tied to the line where I assign $Model the value of my getModel function. Quote Link to comment https://forums.phpfreaks.com/topic/275881-assign-user-defined-function-return-value-to-a-variable-in-a-while-loop/#findComment-1419666 Share on other sites More sharing options...
Zane Posted March 19, 2013 Share Posted March 19, 2013 before the return statement in your function, put this echo "DEBUG<br />\n"; If you don't see the word DEBUG pop up, then you have a problem with your function. Quote Link to comment https://forums.phpfreaks.com/topic/275881-assign-user-defined-function-return-value-to-a-variable-in-a-while-loop/#findComment-1419670 Share on other sites More sharing options...
KevinM1 Posted March 19, 2013 Share Posted March 19, 2013 (edited) EDIT: Why are you looping at all? Your query makes it look like you're only trying to obtain one value. If that's the case, don't loop: function getModel($part) { $root = substr($part,0,4); $query = "SELECT Model FROM tbl_Root WHERE Root = '$root'"; $result = mysql_query($query) or die ("Query Root failed: " . mysql_error()); $rec = mysql_fetch_array($result, MYSQL_ASSOC); if (in_array($root, $rec)) { $Model = $rec['Model']; } else { $Model = 'UNKNOWN'; } return $Model; } Edited March 19, 2013 by KevinM1 Quote Link to comment https://forums.phpfreaks.com/topic/275881-assign-user-defined-function-return-value-to-a-variable-in-a-while-loop/#findComment-1419671 Share on other sites More sharing options...
needsomephphelp Posted March 19, 2013 Author Share Posted March 19, 2013 Just to clarify, my function getModel() should only return one value for each part number. Essentially what I am trying to do is have the script read into a list of part number strings ($part) and find various attributes of the part numbers. In this case, my getModel function will return the model string value of one $part. One $part will only ever have one Model, returned from the getModel() function. I have a script that can read one $part and it properly finds the correct getModel. The stumbling block that I have is how to really make use of my getModel function by finding the getModel for hundreds or thousands of part numbers. I have created several functions that get the $part passed to them to find the one return value. So the reason for my loop then is to take several hundred or more $part (part numbers) and pass it through to find out what the model is, etc. I hope I am making sense here.... Quote Link to comment https://forums.phpfreaks.com/topic/275881-assign-user-defined-function-return-value-to-a-variable-in-a-while-loop/#findComment-1419675 Share on other sites More sharing options...
Zane Posted March 19, 2013 Share Posted March 19, 2013 You should never ever never put a query inside a loop. You should grab all of your part values into an array, and then put that array as a parameter to your function, Of course, you will have to code around that though. You might start by renaming your function to getModels and have it return an array of models.... Quote Link to comment https://forums.phpfreaks.com/topic/275881-assign-user-defined-function-return-value-to-a-variable-in-a-while-loop/#findComment-1419680 Share on other sites More sharing options...
jcbones Posted March 19, 2013 Share Posted March 19, 2013 Why not just let MySQL do everything? Something like: Un-Tested REPLACE INTO tbl_Attributes (Part, Model) SELECT b.A,a.Model FROM TempAttributes AS b JOIN tbl_Root AS a ON SUBSTR(b.A,0,4) = a.Root GROUP BY b.A Not saying it will work without tweaking, but it is worth a shot. Quote Link to comment https://forums.phpfreaks.com/topic/275881-assign-user-defined-function-return-value-to-a-variable-in-a-while-loop/#findComment-1419689 Share on other sites More sharing options...
needsomephphelp Posted March 20, 2013 Author Share Posted March 20, 2013 You should never ever never put a query inside a loop. You should grab all of your part values into an array, and then put that array as a parameter to your function, Of course, you will have to code around that though. You might start by renaming your function to getModels and have it return an array of models.... Not quite sure I understand here. Are you saying I have to let the function pass in an array of my part numbers or return an array of return values? Not exactly sure how to go about doing that? Quote Link to comment https://forums.phpfreaks.com/topic/275881-assign-user-defined-function-return-value-to-a-variable-in-a-while-loop/#findComment-1419821 Share on other sites More sharing options...
needsomephphelp Posted March 20, 2013 Author Share Posted March 20, 2013 Why not just let MySQL do everything? Something like: Un-Tested REPLACE INTO tbl_Attributes (Part, Model) SELECT b.A,a.Model FROM TempAttributes AS b JOIN tbl_Root AS a ON SUBSTR(b.A,0,4) = a.Root GROUP BY b.A Not saying it will work without tweaking, but it is worth a shot. I would agree that if every function of finding part number attributes were that straight forward, letting MySQL do the work would be much easier. The problem is, not every attribute can use one layer of substr logic. The substr is a key function here, but then depending on the part model, each piece of the part number string can mean something different. Quote Link to comment https://forums.phpfreaks.com/topic/275881-assign-user-defined-function-return-value-to-a-variable-in-a-while-loop/#findComment-1419822 Share on other sites More sharing options...
KevinM1 Posted March 20, 2013 Share Posted March 20, 2013 (edited) Howabout you show us the kinds of part number data you're dealing with? Zane and jcbone's point is still right on, however: you have two problems you need to solve - 1. Taking part number data and parsing it into something useable 2. Passing that parsed/useable part number data to the database in order to get a model number/type They don't need to (and likely shouldn't) be the same function. You should have a single function that handles just the parsing of part numbers in all their variations. Pseudo-code of the general workflow: function parsePartNumber($part) { // regex/explode/use string functions on $part return $part; } function getModels($partList) { $parsedParts = array(); foreach ($partList as $part) { $parsedParts[] = parsePartNumber($part); } foreach ($parsedParts as $parsed) { $query = // generate a SQL query on them } $result = mysql_query($query); // NOTE: you should really use MySQLi or PDO here as the mysql_* functions are deprecated. Also note that it's a singular query, NOT queries executed in a loop // grab the models from the result return $models; // an array } // so, at the end, all you should do in your main code would be: $models = getModels($parts); Edited March 20, 2013 by KevinM1 Quote Link to comment https://forums.phpfreaks.com/topic/275881-assign-user-defined-function-return-value-to-a-variable-in-a-while-loop/#findComment-1419829 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.