Jump to content

Trouble inserting array variables into mysql


python72

Recommended Posts

I am trying to insert values stored within two dimensional array into mysql database but it does not work as I would expect it. The locations in mysql are defined as char length of 2. When I print_r the array it shows:

Array( [0] => Array ( [0] => 04 [1] => 22 [2] => 27 [3] => 28 [4] => 39 [5] => 43 [6] => 47 )) 

but when I insert them into the mysql like this:

Number1A='$MaxMillionsNumber[0][0]',
Number1B='$MaxMillionsNumber[0][1]',
Number1C='$MaxMillionsNumber[0][2]',
Number1D='$MaxMillionsNumber[0][3]',
Number1E='$MaxMillionsNumber[0][4]',
Number1F='$MaxMillionsNumber[0][5]',
Number1G='$MaxMillionsNumber[0][6]';

 

my values in mysql all show as Ar

What am I doing wrong?

 

 

Link to comment
Share on other sites

mysql_query("INSERT INTO MaxMillionsNum SET
	Day='$weekday',
	DrawDate='$CompleteDrawDate',
	DateTime='$timestamp',
	Number1A='$MaxMillionsNumber[0][0]',
	Number1B='$MaxMillionsNumber[0][1]',
	Number1C='$MaxMillionsNumber[0][2]',
	Number1D='$MaxMillionsNumber[0][3]',
	Number1E='$MaxMillionsNumber[0][4]',
	Number1F='$MaxMillionsNumber[0][5]',
	Number1G='$MaxMillionsNumber[0][6]';

Link to comment
Share on other sites

See what the query string actually holds:

$query = "INSERT INTO MaxMillionsNum SET
Day='$weekday',
DrawDate='$CompleteDrawDate',
DateTime='$timestamp',
Number1A='$MaxMillionsNumber[0][0]',
Number1B='$MaxMillionsNumber[0][1]',
Number1C='$MaxMillionsNumber[0][2]',
Number1D='$MaxMillionsNumber[0][3]',
Number1E='$MaxMillionsNumber[0][4]',
Number1F='$MaxMillionsNumber[0][5]',
Number1G='$MaxMillionsNumber[0][6]'";
echo $query;

Link to comment
Share on other sites

Here is the result of $query

 

INSERT INTO MaxMillionsNum SET Day='Fri', DrawDate='2010-12-24', DateTime='2010-12-30 19:02:31', Number1A='Array[0]', Number1B='Array[1]', Number1C='Array[2]', Number1D='Array[3]', Number1E='Array[4]', Number1F='Array[5]', Number1G='Array[6]'

 

Now I know why I don't get the expected vaules saved in mysql but why this happens?

Link to comment
Share on other sites

Here is the result:

 

Array
(
    [0] => Array
        (
            [0] => 04
            [1] => 22
            [2] => 27
            [3] => 28
            [4] => 39
            [5] => 43
            [6] => 47
        )
)

Link to comment
Share on other sites

Try

<?php
$query = "INSERT INTO MaxMillionsNum SET
Day='$weekday',
DrawDate='$CompleteDrawDate',
DateTime='$timestamp',
Number1A='{$MaxMillionsNumber[0][0]}',
Number1B='{$MaxMillionsNumber[0][1]}',
Number1C='{$MaxMillionsNumber[0][2]}',
Number1D='{$MaxMillionsNumber[0][3]}',
Number1E='{$MaxMillionsNumber[0][4]}',
Number1F='{$MaxMillionsNumber[0][5]}',
Number1G='{$MaxMillionsNumber[0][6]}'";
echo $query;
?>

 

Ken

Link to comment
Share on other sites

kenrbnsn,

This works, I guess my question is why I need these brackets and where I can find more info on when to use different ftypes of brackets and quotes? I have huge problem to decide when single or double quotes should be used as well as when different types of brackets should be used and can't seem to find this info in concise form so far. I just see them in examples so description comparing them would be helpful.

Thanks a lot for all the help guys.

Link to comment
Share on other sites

Double quoted strings should be used when you want variables to be evaluated in the string or you want special characters like "\n" to work. You should put "{ }" around arrays in a double quoted string.

 

Some examples.

Compare the results of

<?php
$var = 'This is a test';
$arr = array('one'=>'Array entry');
echo '$var<br>';
echo "$var<br>";
echo 'The first entry in the array $arr is $arr["one"]<br>';
echo "The first entry in the array $arr is $arr['one']<br>"; // this will give you a syntax error
echo "The first entry in the array \$arr is {$arr['one']}<br>"; //the "$" is quoted so PHP won't try to evaluated the variable
?>

 

Ken

Link to comment
Share on other sites

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.