-2

The coding is comming up with Illegal string offset for:

echo $row['rowa']. ' - '. $row['rowb']. ' - '. $row['rowc']. ' - '. $row['rowd']. ' - '. $row['rowe'].'<br />';

<?php

   $connection=mysqli_connect("localhost"/*hostname*/,
                              "username"/*username*/,
                              "password"/*password*/,
                              "dbname"/*database name*/);

try {
    // Setting the query and runnin it...
    $result = mysqli_query($connection,"SELECT * FROM table");
    if(mysqli_num_rows($result)>0)
){ 
            foreach(mysqli_fetch_array($result) as $row) {
                echo $row['rowa']. ' - '. $row['rowb']. ' - '. $row['rowc']. ' - '. $row['rowd']. ' - '. $row['rowe'].'<br />';
            }
        }else{
            echo "QUERY IS NOT OKAY";
        }  // Closing the connection.
        $connection = null;
    }catch(PDOException $e) {
        echo $e->getMessage();
    } 

?>

The username, password, database and table name are not the actual names used in the code.

6
  • 2
    You are mixing different database connectors: mysql_... and mysqli_... are two quite separate things. You cannot mix them, you have to decide which one to use. Since the old mysql_... connector has been deprecated a while ago you should go for the newer, more secure and flexible mysqli_... connector. Also you want to read about the advantages of "prepared statements" to prevent vulnerability to sql injection. Commented May 12, 2014 at 8:48
  • 1
    Is your password really password, the username really username and the dbname really dbname? If your answer is yes, you are the most careless programmer i have ever seen in my entire life. Commented May 12, 2014 at 8:48
  • table is a reserved word. You'll need to wrap it in backticks if you really want to use it as a table name. You'd do better to use a different table name. Commented May 12, 2014 at 8:50
  • @MikeW I am not using the actual varaiables. Commented May 12, 2014 at 9:01
  • You have one bracket ) too many in try {// Setting the query and runnin it... $result = mysqli_query($connection,"SELECT * FROM table"); if(mysqli_num_rows($result)>0) ){ change to try {// Setting the query and runnin it... $result = mysqli_query($connection,"SELECT * FROM table"); if(mysqli_num_rows($result)>0) { @TheOkayMan Commented May 12, 2014 at 10:40

5 Answers 5

3

You're mixing mysqli with PDO exceptions try/catch, plus there's an extra bracket in your try block. Those two APIs do not mix.

See the comments in your code: (and the fixed code below that)

Sidenotes: You may want to change foreach(mysqli_fetch_array($result) as $row) to while ($row = mysqli_fetch_assoc($result))

Using foreach(mysqli_fetch_array($result) as $row) will produce repeated results of the rows and only the first letter from each row.

try {
    // Setting the query and runnin it...
    $result = mysqli_query($connection,"SELECT * FROM table");
    if(mysqli_num_rows($result)>0)

    ){
//  ^-- one bracket too many

            foreach(mysqli_fetch_array($result) as $row) {
                echo $row['rowa']. ' - '. $row['rowb']. ' - '. $row['rowc']. ' - '. $row['rowd']. ' - '. $row['rowe'].'<br />';
            }
        }else{
            echo "QUERY IS NOT OKAY";
        }  // Closing the connection.
        $connection = null;
    }catch(PDOException $e) { // this should be catch (Exception $e)
        echo $e->getMessage();
    } 

Change it to this:

try {
    // Setting the query and runnin it...
    $result = mysqli_query($connection,"SELECT * FROM `table`");
    if(mysqli_num_rows($result)>0)
{ 
            foreach(mysqli_fetch_array($result) as $row) {
                echo $row['rowa']. ' - '. $row['rowb']. ' - '. $row['rowc']. ' - '. $row['rowd']. ' - '. $row['rowe'].'<br />';
            }
        }else{
            echo "QUERY IS NOT OKAY";
        }

}

catch (Exception $e)
{
    echo $e->getMessage();
}

Add error reporting at the top of your file(s)

error_reporting(E_ALL);
ini_set('display_errors', 1);

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

Rewrite:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

$connection=mysqli_connect("localhost"/*hostname*/,
                          "username"/*username*/,
                          "password"/*password*/,
                          "dbname"/*database name*/);

    try {
        // Setting the query and runnin it...
        $result = mysqli_query($connection,"SELECT * FROM `table`");
        if(mysqli_num_rows($result)>0)
    { 

// use while instead of foreach
// while ($row = mysqli_fetch_assoc($result)){
                foreach(mysqli_fetch_array($result) as $row) {
                    echo $row['rowa']. ' - '. $row['rowb']. ' - '. $row['rowc']. ' - '. $row['rowd']. ' - '. $row['rowe'].'<br />';
                }
            }else{
                echo "QUERY IS NOT OKAY";
            }

    }

    catch (Exception $e)
    {
        echo $e->getMessage();
    }
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for time and effort
1

instead of $result = mysql_query($connection,"SELECT * FROM table");

use $result = mysqli_query($connection,"SELECT * FROM table");

instead of mysql_fetch_array($result)

use mysqli_fetch_array($result)

3 Comments

I get the same error as for DhruvPathak's answer.
put foreach() in this if condition if(mysqli_num_rows($result)>0){ foreach(mysql_fetch_array($result) as $row){......}}
Illegal string offset I have updated the coding
0

I suppose you should be using mysqli_query along with mysqli_connect and not mysql_query

2 Comments

With the 'i' the error I get is Illegal string offset for line echo $row['Name']. ' - '. $row['Club']. ' - '. $row['Level']. ' - '. $row['App']. ' - '. $row['Score'].'<br />';
check the names of your columns. mysqli_fetch_array() builds a mixed array with numeric and text indexes. Use mysqli_fetch_assoc() to get an associative array only so your array isn't double the size. Your columns may not be the same so your index doesn't exist.
0

Try this out

<?php
 $mysqli = new mysqli("localhost", "username", "password", "Database name");
  if (mysqli_connect_errno()) {
  printf("Connect failed: %s\n", mysqli_connect_error());
  exit();
}

  // Setting the query and runnin it...
  $result = $mysqli->prepare("SELECT * FROM table");

  /* execute query */
  $result->execute();
  $result = $mysqli->get_result();

     while ($row= $result->fetch_assoc()) 
     { 

            echo $row['rowa']. ' - '. $row['rowb']. ' - '. $row['rowc']. ' - '. $row['rowd']. ' - '. $row['rowe'].'<br />';

     }


    ?>

5 Comments

Call to a member function execute() on a non-object in``$result->execute();
what is your table name
the table name is score
Are you putting any where condition in the query or using as it is ?
Fot the moment as it is; unless it will make the coding work.
0

Most probably, something is wrong with your connection. As mysqli* don't throw exceptions, you can't catch them.

Those functions return valid objects or FALSE, 0 or null on error. So you need to check for that.

Here is the code, that should tell you what's wrong with your communication with the DB.

<?php

function ctest() {
    $connection=mysqli_connect("localhost"/*hostname*/,
                               "username"/*username*/,
                               "password"/*password*/,
                               "dbname"/*database name*/);

    if($connection) {
        $result = mysqli_query($connection,"SELECT * FROM `table`");
        if($result) {
            if(mysqli_num_rows($result)>0) {
                foreach(mysqli_fetch_array($result) as $row) {
                    echo $row['rowa']. ' - '. $row['rowb']. ' - '
                                          . $row['rowc']. ' - '
                                          . $row['rowd']. ' - '
                                          . $row['rowe'].'<br />';
                }
                return;
            }
            echo "0 rows";
            return;
        }
        echo "No result";
        return;
    }
    echo "No connection";
    $connection = null;
}
?>

4 Comments

Illegal string offset
Column name are correctly spelt and correct case.
It should tell you, precisely which offset is incorrect. Take it from there.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.