kingnutter Posted May 6, 2009 Share Posted May 6, 2009 The object of the following code is to retrieve the variable moj_date (a text date) and EXPLODE it back into elements that can be used as SELECTED in an edit date dropdown form. However, I cannot seem to turn the result into a usable resource. echo moj_date_exp just gives me ArrayArray. I'm pretty sure I need a fetch function other than $id = $_GET['id']; $query = "SELECT moj_title, moj_issue, moj_summary, moj_genre FROM mojocd WHERE moj_id = '$id'"; $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); $query = "SELECT moj_date FROM mojocd WHERE moj_id = '$id'"; $result2 = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); $moj_date_string = mysql_fetch_object('$result2'); $moj_date_exp = explode(' ', $moj_date_string); echo $moj_date_exp; Quote Link to comment https://forums.phpfreaks.com/topic/157129-solved-turning-a-mysql-result-into-a-usable-array/ Share on other sites More sharing options...
premiso Posted May 6, 2009 Share Posted May 6, 2009 print_r($moj_date_exp); Read up on array's for how to use the array items. Basically $moj_date_exp[0] will be the first element (the item before the first space) and so on. EDIT: Also note: $moj_date_string = mysql_fetch_object('$result2'); Is probably throwing an error, single quotes like you used there takes $ literally. Use double or no quotes: $moj_date_string = mysql_fetch_object($result2); Quote Link to comment https://forums.phpfreaks.com/topic/157129-solved-turning-a-mysql-result-into-a-usable-array/#findComment-827867 Share on other sites More sharing options...
Ken2k7 Posted May 6, 2009 Share Posted May 6, 2009 1. Add moj_date into your first SQL so you don't have to run another one. 2. Read mysql_fetch_assoc Quote Link to comment https://forums.phpfreaks.com/topic/157129-solved-turning-a-mysql-result-into-a-usable-array/#findComment-827869 Share on other sites More sharing options...
kingnutter Posted May 6, 2009 Author Share Posted May 6, 2009 Thanks for the quick replies. Sorry my trigger happy mouse submitted the post before I'd finished. I was about to address my double query so thanks for the spot. Just to let you all know that I seriously read up on all topics before I even think of posting on here. I shall have another look at mysql_fetch_assoc, but from what I remember this puts things into associative arrays i.e. this => that. My result could be "3 March 2009", "March 2009" or even just "2009" so is mysql_fetch_assoc still the way to go? Quote Link to comment https://forums.phpfreaks.com/topic/157129-solved-turning-a-mysql-result-into-a-usable-array/#findComment-827875 Share on other sites More sharing options...
Ken2k7 Posted May 6, 2009 Share Posted May 6, 2009 Read it. It just goes through your SQL resource result and grab the record at the internal data pointer while moving it forward each time you call it. In other words, it just grabs all your results. It doesn't care what the result looks like. Quote Link to comment https://forums.phpfreaks.com/topic/157129-solved-turning-a-mysql-result-into-a-usable-array/#findComment-827879 Share on other sites More sharing options...
premiso Posted May 6, 2009 Share Posted May 6, 2009 I shall have another look at mysql_fetch_assoc, but from what I remember this puts things into associative arrays i.e. this => that. My result could be "3 March 2009", "March 2009" or even just "2009" so is mysql_fetch_assoc still the way to go? You can pull data out how you want, an object, associative array or regular array. It will not matter. But explode puts the items into a non-associative array and you were trying to print the array and found out it just produces "array". Instead you have to print it out by index since explode does not make an array associative. $id = $_GET['id']; $query = "SELECT moj_title, moj_issue, moj_summary, moj_genre FROM mojocd WHERE moj_id = '$id'"; $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); $query = "SELECT moj_date FROM mojocd WHERE moj_id = '$id'"; $result2 = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); $moj_date_string = mysql_result($result2, 0, 0); // you were not really using the object right, but since you are just pulling one column/row mysql_result will work great $moj_date_exp = explode(' ', $moj_date_string); // if you used the fetch_object, this line should be $moj_date_exp = explode(' ', $moj_date_string->moj_date); $moj_date_exp = array_reverse($moj_date_exp); // make sure the year is first no matter what. $year = isset($moj_date_exp[0])?$moj_date_exp[0]:''; $month = isset($moj_date_exp[1])?$moj_date_exp[1]:''; $day = isset($moj_date_exp[2])?$moj_date_exp[2]:''; mysql_result is what I tend to use when I am only expecting 1 column from 1 row. Just easier. The ? and : are the ternary operators which is a shortended if/else to prevent errors if the index of the array is not there. Anyhow, hopefully that was what you were looking for. Quote Link to comment https://forums.phpfreaks.com/topic/157129-solved-turning-a-mysql-result-into-a-usable-array/#findComment-827886 Share on other sites More sharing options...
kingnutter Posted May 6, 2009 Author Share Posted May 6, 2009 Yes this will work for me very well. To be honest I felt that fetch_result was best for a single column / row (field?) variable but I hadn't coded it correctly. I had also tried fetch_object but stupidly forgotten the ->moj_date bit. So, not sure I'm ever going to get my head round this stuff but small steps... Thanks guys. Quote Link to comment https://forums.phpfreaks.com/topic/157129-solved-turning-a-mysql-result-into-a-usable-array/#findComment-827929 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.