On 31-03-2012 10:29, tamouse mailing lists wrote:
On Sat, Mar 31, 2012 at 1:45 AM, saeed ahmed<mycomputerbo...@gmail.com>  wrote:
i have made a php script with a tutorial help....i dont know where is a
error.please have a look

Before you actually run code in a browser, check your syntax with php
-l (for "lint", the old command to check C syntax on unixy systems) on
a command line. This will improve your life immeasurably since you
won't have to chase down where the server logs errors just to see if
your code was syntactically correct.

miishka:Sites tamara$ php -l test.saeed.php
PHP Parse error:  syntax error, unexpected T_STRING in test.saeed.php on line 6
Errors parsing test.saeed.php
miishka:Sites tamara$

Which points out the syntax error on line 6 where you have:

   $result=mysql_query("select("SELECT*FROM COLLEAGUE");

Which as an improperly formed select sql, and an unclosed string (thus
sucking up part of your php code and html document. To fix this, it
should be:

   $result=mysql_query($db, "SELECT * FROM COLLEAGUE");

Wrong, it should be the other way around. First the query, and then an OPTIONAL link-identifier (which I assume $db to be in this case).

i.e., the sql statement is in a single string. Also, spacing around
the column list "*" may or may not be necessary, but it's a lot easier
to read if it's there. Also, you need to tell the query procedure what
data base you're using the $db variable (see below for this).

Ok, that fixed, run php -l on the file again, and we get:

    PHP Parse error:  syntax error, unexpected '>' in test.saeed.php on line 31
    Errors parsing test.saeed.php

Looking at line 31:

    echo "<td>" . $row['firstName'] ."</td>";

it looks fine, you might be wondering why it's complaining about that
line. The key is to look up from there for possible syntax problems,
and on line 30, there is a problem in that you didn't close the last

    echo"<td>".$row['id'] ."</td>;

should be:

    echo"<td>".$row['id'] ."</td>";

Ok, fix that, run php -l again:

    PHP Parse error:  syntax error, unexpected '[', expecting ',' or
';' in test.saeed.php on line 34
    Errors parsing test.saeed.php

So, look at line 34 now:

    echo "<td>" . Srow['email'] ."</td";

The problem is that you typoed the dollar sign in front of row. It should be:

    echo "<td>" . $row['email'] ."</td";

Also, on this line, although php -l won't catch it, is an html error.
The last /td is not closed, so:

    echo "<td>" . $row['email'] ."</td>";

Run php -l again, and this time, voila:

    No syntax errors detected in test.saeed.php

That solves your syntax errors. Now you can test it from a server.

Before you do that though, a few other points:

When developing code, it's a really good idea to turn on run time
error checking so you can see things in the browser when a problem
crops up:

   error_reporting(-1); // turns on EVERYTHING

   // follow with your code...

This won't help if your code doesn't parse correctly, but that's what
the php -l is for above, but it will reveal run-time processing errors
in your browser. And you have several.

As another responder said, checking the return values of functions is
important. Sometimes tutorials omit this for the sake of brevity, but
it is something that still should be done.

Line 3:

    mysql_connect("localhost","root"," " );

is incorrect on at least two levels. First, you haven't saved the data
base resource that is returned from mysql_connect, and second, you
haven't checked that the call was successful. Third, possibly is that
you have specified the root mysql password a string of one space. This
is unlikely to be the password.

It should be rewritten as:

    $db = mysql_connect("localhost", "root", "") or die("Could not
connect to mysql server on local host: " . mysql_error() . PHP_EOL);

The next line:


is also incorrect as it does not specify the data base resource which
you omitted in the previous call, and you aren't (again) checking the
result to see if it works.

It should be rewritten as:

    mysql_select_db($db, "addressbook") or die ("Could not connect to
data base 'addressbook'. Error: " . mysql_error($db) . PHP_EOL);

Back to that line 6, after you've repaired it, you *again* need to
check that the query actually worked. After that query, include the
following line:

    if (FALSE === $result) die ("Query failed. Error: " .
mysql_error($db) . PHP_EOL);

Note that if the table is empty, that is not an error; your result set
will simply be empty. Returning FALSE indicates that there was an
actual error in the query. I find it better to put the sql statement
into a string, and then use the string variable in the actual query
function. This lets you also print out the query as it was sent to

    $sql = "select * from colleague";
    $result = mysql_query($db, $sql);

Again, query first, linkidentifier after that. This way you'll always get an error.

    if (FALSE === $result) die ("Query failed. \$sql=$sql. Error: " .
mysql_error($db) . PHP_EOL);

A little further on, there is a problem in the html meta statement:
there is no closer on that line:

    <meta http-equiv="content-type" content="text/html;charset=utf-8"

It needs to be closed with "/>" since you're using XHTML.

Line 39 needs to have the data base resource as well that was set
above in the connect:


You owe me one internet bheer for debugging your script. :)

You made mistakes during debugging :s

PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to