-2
<?php

mysql_connect("localhost","root","");
mysql_select_db("rohan");
$s="select * from black";
$resource=mysql_query($s);
$row = mysql_fetch_row($resource);


            echo "ID: ".row[0]."<br>";
            echo "Name: ".row[1]." ".row[2]."<br>";
            echo "E-mail: ".row[3]."<br>";
            echo "country: ".row[4]."<br>";
            echo "salary: ".row[5]."<br>";

I can't connect to my database with this code. It gives me this error.

Parse error: syntax error, unexpected '[', expecting ',' or ';' in C:\xampp\xampp\htdocs\rohan\mysql_fetch_row.php on line 9

What is the problem?

Maks3w
  • 6,014
  • 6
  • 37
  • 42
  • 1
    What do you mean by not working??? – Saty Nov 05 '15 at 09:39
  • 1
    You should include the exception details and the stack trace in your question – Oguz Ozgul Nov 05 '15 at 09:44
  • show this error Parse error: syntax error, unexpected '[', expecting ',' or ';' in C:\xampp\xampp\htdocs\rohan\mysql_fetch_row.php on line 9 – Rohan Saini Nov 05 '15 at 09:57
  • Welcome to Stack Overflow. I have merged your error into your post. I marked it by using >. Please do not use comments to enhance your post, instead edit it. Sometimes , it is better to indicate where you have edited it. – Rohit Gupta Nov 07 '15 at 10:45

5 Answers5

0

You retrieve row not an array, so change your code from:

 $row = mysql_fetch_row($resource);

to

 $row = mysql_fetch_array($resource);
Nere
  • 4,097
  • 5
  • 31
  • 71
0

change

$row = mysql_fetch_row($resource);

to

 $row = mysql_fetch_assoc($resource);

done

William Madede
  • 727
  • 4
  • 8
0

for this line you don't connect to database:

$resource=mysql_query($s);

I think you should replace by this:

  $sql=mysql_connect("localhost","root","");
  mysql_select_db("rohan");
  $s="select * from black";
  $resource=mysql_query($s,$sql);
  $row = mysql_fetch_row($resource);
  echo "ID: ".$row[0]."<br>";
  echo "Name: ".$row[1]." ".$row[2]."<br>";
  echo "E-mail: ".$row[3]."<br>";
  echo "country: ".$row[4]."<br>";
  echo "salary: ".$row[5]."<br>";
?>
Chanthy
  • 16
  • 4
  • Parse error: syntax error, unexpected '[', expecting ',' or ';' in C:\xampp\xampp\htdocs\rohan\mysql_fetch_row.php on line 7 – Rohan Saini Nov 05 '15 at 10:14
0

You didn't use $ for local variables.

Instead

echo "ID: ".row[0]."<br>";

MUST be

echo "ID: ".$row[0]."<br>";
Maks3w
  • 6,014
  • 6
  • 37
  • 42
0

Fist make sure your query is run correctly.

Second make sure there are some records in assumed table.

I modified your code a bit like below.

<?php

mysql_connect("localhost","root","");
mysql_select_db("rohan");
$s="select * from black";
$resource=mysql_query($s);
if (!$resource) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}
$row = mysql_fetch_row($resource);
if (!$row || !is_array($row)) {
    echo 'row is no valid: ' . mysql_error();
    exit;
}

            echo "ID: ".row[0]."<br>";
            echo "Name: ".row[1]." ".row[2]."<br>";
            echo "E-mail: ".row[3]."<br>";
            echo "country: ".row[4]."<br>";
            echo "salary: ".row[5]."<br>";
Mohsen Alizadeh
  • 1,595
  • 12
  • 25