Asked  7 Months ago    Answers:  5   Viewed   30 times

I'm getting information from database, saving it in array and echoing it in a form with loop structure and I'm having problems when I try to save the modified information to database.

I'm getting this error:

Fatal error: [] operator not supported for strings in....

Code:

$namesql1 = "SELECT name,date,text,date2 FROM table WHERE something= '$something'";
$nameresult1 = mysql_query($namesql1);
$countrows = mysql_num_rows($nameresult1);
while ($row = mysql_fetch_array($nameresult1, MYSQL_ASSOC)) {
    $name[] = $row['name'];
    $date[] = $row['date'];
    $text[] = $row['text'];
    $date2[] = $row['date2 '];
}

/** SOME CODE HERE **/

    
$wrotesql = "UPDATE service_report SET  name ='$name' , $date = '$date',$text = '$text[$nro]', ser_date = '$date2[$nro]' WHERE something = '$something')";

$wroteresult = mysql_query($wrotesql);

Could somebody please give me a hint what I'm doing wrong?

 Answers

55

You get this error when attempting to use the short array push syntax on a string.

For example, this

$foo = 'foo';
$foo[] = 'bar'; // ERROR!

I'd hazard a guess that one or more of your $name, $date, $text or $date2 variables has been initialised as a string.

Edit: Looking again at your question, it looks like you don't actually want to use them as arrays as you're treating them as strings further down.

If so, change your assignments to

$name = $row['name'];
$date = $row['date'];
$text = $row['text'];
$date2 = $row['date2'];

It seems there are some issues with PHP 7 and code using the empty-index array push syntax.

To make it clear, these work fine in PHP 7+

$previouslyUndeclaredVariableName[] = 'value'; // creates an array and adds one entry

$emptyArray = []; // creates an array
$emptyArray[] = 'value'; // pushes in an entry

What does not work is attempting to use empty-index push on any variable declared as a string, number, object, etc, ie

$declaredAsString = '';
$declaredAsString[] = 'value';

$declaredAsNumber = 1;
$declaredAsNumber[] = 'value';

$declaredAsObject = new stdclass();
$declaredAsObject[] = 'value';

All result in a fatal error.

Wednesday, March 31, 2021
 
TheLovelySausage
answered 7 Months ago
71

you fill the array $emails_list[] in the second while with $contacts2["email"]. in the next line, you implode the array to a string.

for the next result (first while loop) $emails_list is a string. now you can't convert a string to an array

try this:

        $emailsListData = array();
        while($contacts2=mysql_fetch_array($rs2))
        {
            //generate the list of emails address in an array
            $emailsListData[] = $contacts2["email"];
        }
        $emails_list = implode(',',$emailsListData);
Saturday, May 29, 2021
 
Saxophlutist
answered 5 Months ago
36

WORKING DEMO

$SchoolNames = Array(10003, "Southwestern College", "National University", "Western Governors University", "Southwestern College Admissions Center - Evaluations Dept");
$data = array(
    0 =>  Array(
        'STU_MANG_fname' => "Jennifer",
        'STU_MANG_lname' => "patel",
        'SchoolName' => "Southwestern College Admissions Center - Evaluations Dept",
        'ShipAddress1' => "900 Otay Lakes Road",
        'ShipState' => "CALIFORNIA" 
    )
);


foreach($data as $studen_info){
    foreach($SchoolNames as $id=>$school_name){
        if($studen_info['SchoolName'] == $school_name){
            $student_names[$school_name] = $id;
            //$student_names[$school_name] = $student_info['StudentId'];;
        }
    }
}

print_r($student_names);

there was no 'StudentId' in the student info array you gave me so I assume you want to use the key of the student array if there is in fact a 'StudentId' use line i commented out

Saturday, May 29, 2021
 
matthy
answered 5 Months ago
71

you can't print the result from mysqli_query, it is mysqli_resource and for dumping the error you need to change mysql_error() to mysqli_error()

$username = "bob";
$db = mysqli_connect("localhost", "username", "password", "user_data");
$sql1 = "select id from user_information where username='$username'";
$result = mysqli_query($db, $sql1) or die(mysqli_error());
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { 
    echo $row['id'].'<br>'; 
} 
Saturday, May 29, 2021
 
pamelus
answered 5 Months ago
39

yo need create the user "pma" in mysql or change this lines(user and password for mysql):

/* User for advanced features */
$cfg['Servers'][$i]['controluser'] = 'pma'; 
$cfg['Servers'][$i]['controlpass'] = '';

Linux: /etc/phpmyadmin/config.inc.php

Tuesday, July 13, 2021
 
ShadowZzz
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :