Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to save array value to variable so that I can insert that data/value into mysql using PHP for that I am using a pain taking long code so I want simple method to do it without writing it again and again.

Array shows data like this

Array
(
    [0] => Array
        (
            [0] => a><p class="wp-caption-text">Pendant Heart Images</p></div>
            [1] => a><p class="wp-caption-text">Pendant Heart Photos</p></div>
            [2] => a><p class="wp-caption-text">Pendant Heart Photos</p></div>
        )

    [1] => Array
        (
            [0] => Pendant Heart Images
            [1] => Pendant Heart Photos
            [2] => Pendant Heart Photos

      )

)

Code which I am using to save array value

$a = $g_img[1][0];
$b = $g_img[1][1];
$c = $g_img[1][2];

$a1 = $title[1][0];
$b1 = $title[1][1];
$c1 = $title[1][2];

Mysql query to save data

mysqli_query($con,"INSERT INTO data (Link, Title) VALUES ('$a','$a1')");
mysqli_query($con,"INSERT INTO data (Link, Title) VALUES ('$b','$b1')");
mysqli_query($con,"INSERT INTO data (Link, Title) VALUES ('$c','$c1')");

So If array data increases I have to assign each array value to different variable which is huge process I know there would be a shortcut

Plz Help

share|improve this question
    
How is that MySQL query supposed to work? –  TheWolf 2 days ago
    
I have updated MySQL query check @TheWolf –  user3478845 2 days ago
add comment

2 Answers

up vote 1 down vote accepted

The query will not work because to start with, the number of field name are not equal to the number of values.

You could have passed your query in the following way:

for ($counter = 0; $counter <=2; $counter++){
    $query = "INSERT INTO data (Link, Title) VALUES ('{$g_img[1][$counter]}', '{$title[1][$counter]}')";
    $result = mysqli_query($con, $query);

}

Here I have assumed that $g_img and $title correspond to the array of links and titles respectively and the data connection is $con.

share|improve this answer
    
yeah thanks it's working for me tell me if array increases from 2 to 10 should I change <=2 to <=10 –  user3478845 2 days ago
    
use count(). It will give you more flexibility. –  Adarsh 2 days ago
    
How to ignore empty array like if array [1][0] => empty –  user3478845 2 days ago
    
The function count() will help you in this too just put it in an if-conditional. –  Adarsh 2 days ago
    
okay I used count() and it's working –  user3478845 2 days ago
add comment

Following array data from a PHP array variable stored insert into MySQL database table. This should be a two-dimensional array, in which each element of the array is an array of field values for one database record.

Array Data

$data = array(
            array(
            [0] => a><p class="wp-caption-text">Pendant Heart Images</p></div>
            [1] => a><p class="wp-caption-text">Pendant Heart Photos</p></div>
            [2] => a><p class="wp-caption-text">Pendant Heart Photos</p></div>
        ),
            array(
            [0] => Pendant Heart Images
            [1] => Pendant Heart Photos
            [2] => Pendant Heart Photos
        )
 );

PHP

$data = array_filter($data);

foreach ($data as $row) {
    $result = mysqli_query($con,"INSERT INTO data (Link, Title) VALUES ('$row[0]', '$row[1]')");

    if($result){
        $success .= sprintf("%s has been entered in database.<br />", $row[0]);    
    }else{
        $fail .= sprintf("%s insert failed: %s\n<br />", $row[0], mysqli_error($con));
    }
}

print $success;
print $fail;

Hope this help you!

share|improve this answer
    
@Adrash code is working for me and your's too so also one thanks goes to you :) –  user3478845 2 days ago
    
You wecome.... :) –  user1153551 2 days ago
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.