0

I'm trying to update a database using arrays, but I can't seem to do that. I can only update it using strings, and numbers, but not arrays. Then I tried to echo out the string, number AND the array, just to see if I made an mistake in making the array. Please take a look:

$string = "string";
$num = 10;
$array[0][0] = "array";

echo $string."</br>";
echo $num."</br>";
echo $array[0][0]."</br>";

output:

string
10
array

But inserting data into a table using mysql:

$table = "userLogin";
$column = "username";

Using string:

$query= "INSERT INTO $table($column)VALUES($string)";
$result = mysql_query($query);

Output in table:

string

Using number:

$query= "INSERT INTO $table($column)VALUES($number)";
$result = mysql_query($query);

Output in table:

10

But when you're using an array:

$query= "INSERT INTO $table($column)VALUES($array[0][0])";
$result = mysql_query($query);

Output in table:

There is no change. Why is that? I figure that since all strings, numbers, and arrays can be echo, using the echo command, they can be use likewise when updating a database. But it seems like you can only update them using strings and numbers. Unless, I'm doing something wrong. If I'm doing something wrong, please let me know. Thank you.

10
  • 1
    Have you tried using curly brackets? $query= "INSERT INTO $table($column)VALUES({$array[0][0]})"; By the way, if those variables contain user input it's better to use prepared statements to prevent SQL injection. Commented May 28, 2015 at 18:59
  • WARNING: mysql_query is an obsolete interface and should not be used in new applications as it's being removed in future versions of PHP. A modern replacement like PDO is not hard to learn. If you're new to PHP, a guide like PHP The Right Way can help explain best practices. Commented May 28, 2015 at 19:01
  • echo $array."</br>"; does it not showing any php notice like this Notice: Array to string conversion because you can't echo array Commented May 28, 2015 at 19:01
  • @TomasoAlbinoni I tried it. Sadly, it didn't work. Also, I take all tags out of user input, so users can't inject database. Commented May 28, 2015 at 19:15
  • 1
    Definitely read about SQL injection, it's an important topic. And don't use a deprecated API. And use prepared statements. Good luck! Commented May 28, 2015 at 19:31

1 Answer 1

1

PHP arrays-in-strings 201 (intermediate level): PHP's string parser is NOT greedy, and by default will ignore multi-dimensional array references inside double-quoted strings.

e.g.

$foo = array();
$foo[0] = array();
$foo[0][1] = 'bar';

echo $foo;          // output: Array
echo "$foo[0]"      // output: Array
echo "$foo[0][1]"   // output: Array[1]
echo "{$foo[0][1]}" // output: bar
      ^----------^
echo $foo[0][1]     // output: bar

If you want to use multidimensional arrays in double-quoted strings, then, as above, you have two choice: Don't, concatenate them into the string; or use the {} notation to force PHP to parse the ENTIRE array reference, not just the first dimension.

The specifics of your problem:

$query= "INSERT INTO $table($column)VALUES($array[0][0])";

actually becomes

$query= "INSERT INTO $table($column)VALUES(Array[0])";
                                           ^^^^^

where Array is an unknown/invalid field name, and [0] is a flat-out syntax error.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.