Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm not an experienced coder, so please, understand that while I have researched this error I cannot find an example which fits this code insofar as I understand it. I'm getting the string offset error in the subject line from the following snippet of code:

46  $result = mysql_query("SELECT username, pass, reply, authdate FROM somedatabase  WHERE username = '$user' ORDER BY authdate DESC LIMIT 5");
47  if($result == false) {
48      echo mysql_error();
    }       
49  var_dump($result);
50  echo "Number of Rows: " . mysql_num_rows($result). "\n";
51  while ($row = mysql_fetch_array($result)){
52      $data[] = $row;
53      foreach ($data as $col){
54          echo $col['username']['pass']['reply']['authdate'];
55      }
56      echo '<br>';
57  }

This is the output from the command line:

resource(6) of type (mysql result) Number of Rows: 5

PHP Fatal error: Cannot use string offset as an array in /usr/lib/tadpole/CL-wifiacct-full-info.php on line 54

When I declare $result as an array (using the code $result[]) in line 46, I get:

PHP Warning: mysql_num_rows() expects parameter 1 to be resource, array given in /pathtofile-blah.php on line 50 Number of Rows:

PHP Warning: mysql_fetch_array() expects parameter 1 to be resource, array given in /pathtofile-blah/.php on line 51

share|improve this question
 
I think the best thing would be for you to look at some mysql/php tutorial / samples, and see how they work. Or insert some print_r statements into your code so you can look at the actual data structures. –  Aerik Feb 19 at 18:06

closed as too localized by cryptic ツ, NullPoiиteя ღ, tereško, PeeHaa, j0k Feb 20 at 8:24

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

3 Answers

Change:

47  if($result == false) {
48      echo mysql_error();}  

To:

if($result == false) {
    echo mysql_error();
} else {

The way it is right now it doesn't 'care' if the query ran or not, it still tries to access the result set. You also have an unmatched } on line 57 which I assume is the result of removing the else that used to be there.

share|improve this answer

That bracket on line 57 actually isn't unmatched. It belongs to the while on line 51, so that while contains the foreach loop.

If I am reading it clearly, it seems like the value of $col in the foreach loop shouldn't be arrays, but the value of the column. Also, setting $data=$row is unnecessary. Is something like the below what you're after?

$result = mysql_query("SELECT username, pass, reply, authdate FROM somedatabase  WHERE username = '$user' ORDER BY authdate DESC LIMIT 5");
if($result == false) {
    echo mysql_error();
}        
var_dump($result);
echo "Number of Rows: " . mysql_num_rows($result). "\n";
while ($row = mysql_fetch_array($result)){
    foreach ($row as $col=>$val){
        echo $col.": ".$val;
    }
    echo '<br>';
}

I believe that would print every column or you could try the below if you just wanted to specify those particular columns.

$result = mysql_query("SELECT username, pass, reply, authdate FROM somedatabase  WHERE username = '$user' ORDER BY authdate DESC LIMIT 5");
if($result == false) {
    echo mysql_error();
}        
var_dump($result);
echo "Number of Rows: " . mysql_num_rows($result). "\n";
while ($row = mysql_fetch_array($result)){
    echo $row['username'].", ".$row['pass'].", ".$row['reply'].", ".$row['authdate'];
    echo '<br>';
}
share|improve this answer
 
I copped out and did this:code $result = mysql_query("SELECT username, pass, reply, authdate FROM radpostauth WHERE username = '$user' ORDER BY authdate DESC LIMIT 5"); if($result == false) { echo mysql_error(); } else{ while ($row = mysql_fetch_array($result)){ $login = $row['username']; $pword = $row['pass']; $radreply = $row['reply']; $when = $row['authdate']; echo "$login &nbsp;&nbsp;&nbsp;&nbsp;", "$pword &nbsp;&nbsp;&nbsp;&nbsp;", "$radreply &nbsp;&nbsp;&nbsp;&nbsp;", "$when <br>"; } code –  Bill Isaacs Feb 19 at 20:01
 
Sorry, let me try that again, lol. I copped out and did this: $result = mysql_query("SELECT username, pass, reply, authdate FROM radpostauth WHERE username = '$user' ORDER BY authdate DESC LIMIT 5"); ` if($result == false) {` echo mysql_error(); } else{ ` while ($row = mysql_fetch_array($result)){` ` $login = $row['username'];` ` $pword = $row['pass'];` ` $radreply = $row['reply'];` ` $when = $row['authdate'];` ` echo "$login &nbsp;&nbsp;&nbsp;&nbsp;", "$pword &nbsp;&nbsp;&nbsp;&nbsp;", "$radreply &nbsp;&nbsp;&nbsp;&nbsp;", "$when <br>";` ` }` –  Bill Isaacs Feb 19 at 20:08
 
That will work too. :) –  seagoj Feb 19 at 20:22

Seems like your #data is not an array

if(is_array($data) && count($data)>0){
      foreach ($data as $col){
       echo $col['username']['pass']['reply']['authdate'];
      }
}
share|improve this answer

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