Jump to content

Recommended Posts

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;

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

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?

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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