Solved

Illegal string offset Warning PHP

Posted on 2015-02-22
8
437 Views
Last Modified: 2015-03-11
Hi,

I keep getting Illegal string offset Warning PHP, I know its connected with an array, how can I improve the query so ot to get the warning?

<br /><b>Warning</b>:  Illegal string offset 'username' in <b>C:\xampp\htdocs\mySeatXT\web\includes\users_form.inc.php</b> on line <b>24</b><br /><br /><b>Notice</b>:  Uninitialized string offset: 0 in <b>C:\xampp\htdocs\web\includes\users_form.inc.php</b> on line <b>24</b><br />


case 'user_data':
$result = query("SELECT userID,username,realname,password,email,role,property_id,active,confirm_code,last_ip,last_login,created,modified
      FROM `plc_users`
WHERE `userID` ='%d'
      LIMIT 1",$_SESSION['userID']);
      return getRowListarray($result);
break;
0
Comment
Question by:hellblazeruk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 40624371
I can't see anything in that code that causes the error. I'm guessing the error is coming from your getRowListarray() function, so we'd need to see that. Also, what exactly is your query() function doing. That doesn't seem part of standard PHP, so I'm guessing it's your own function - again, may need to see that.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40624390
Usually when you get a PHP message, you get a line number with it.  It looks like maybe that is line 24 in these messages.  What does that line contain?  Also, have you used var_dump() to print out the contents of the variables that are used in that line?
0
 
LVL 34

Expert Comment

by:Slick812
ID: 40624416
I'd try and use the PHP  sprintf( ) , usually if it talks about string offset it' got something to do the string formatting?
case 'user_data':
$sql = sprintf("SELECT userID,username,realname,password,email,role,property_id,active,confirm_code,last_ip,last_login,created,modified
      FROM `plc_users`
WHERE `userID` ='%d'
      LIMIT 1", $_SESSION['userID']);

$result = query($sql);
      return getRowListarray($result);
break; 

Open in new window

0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40624435
Here is a code simulation that will trigger one of the messages.  It happens because PHP allows the same notation for strings and arrays - square brackets on the end of the variable name.  Possibly the script assumes it is going to get an array variable, but it got a string variable instead?  That might happen with an error message.

<?php // temp_hellblazeruk.php

/**
 * See http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28622093.html
 *
 * Warning:  Illegal string offset 'username' in C:\xampp\htdocs\mySeatXT\web\includes\users_form.inc.php on line 24
 * Notice:  Uninitialized string offset: 0 in C:\xampp\htdocs\web\includes\users_form.inc.php on line 24
 */
error_reporting(E_ALL);

// SET UP THE NECESSARY CONFUSION
$str = 'ABC';
$str[0] = NULL;
var_dump($str);

// TRY TO USE 'username' AS A STRING INDEX
echo $str['username'];

Open in new window

0
 

Author Comment

by:hellblazeruk
ID: 40624510
line 24
is
            <input type="text" name="username" id="username" class='required' minlength='10' maxlength='30' title=' ' value="<?php echo $row['username'];?>"/>
users-form.inc.php
0
 
LVL 34

Expert Comment

by:Slick812
ID: 40624524
that PHP starts out with -

if ($_SESSION['button']==2 || $_SESSION['page'] == 7) {
      $row = ""; // ERROR is HERE
}

where the $row is set to an empty string, you CAN not use that $row String variable like -
    $row['username'];

you will need to Exit the page so there are no <form> written with the $row['username'];

OR

create a Default $row array with all of the array parts like 'username' and 'email', so there can be something to use the $row array
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 40624561
There are a couple of points in your code that have the capacity for failure. The error you're getting is because $row['username'] doesn't exist. This could be caused by $row being set to "" or by your querySQL() function not returning what you expect.

Because you never quite know what $row is, you should ensure that you check it before trying to use it. In your particular case, something like this:

<input type="text" name="username" id="username" class='required' minlength='10' maxlength='30' title=' ' value="<?php echo (isset($row['username'])) ? $row['username'] : ""; ?>"/>

Open in new window


The echo statement now uses a ternary operator, which checks if $row['username'] is set. If it is, then it gets used. If it isn't then an empty string is echoed - no more errors.
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40624563
Suggest you add error_reporting(E_ALL) to the top of your PHP scripts.  A correctly written script will never rely on an undefined variable, and E_ALL will help you avoid that.  It will raise a Notice message at run-time if the script uses an undefined variable.  In this case, it would have given you a little more information about the $row variable, including the name of the variable that was causing trouble.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question