php-general Digest 31 Mar 2012 11:36:52 -0000 Issue 7754

Topics (messages 317381 through 317395):

Re: Node.PHP
        317381 by: German Geek
        317382 by: Michael Save

Could apc_fetch return a pointer to data in shared memory ?
        317383 by: Simon
        317394 by: Stuart Dallas

Re: securing a script that exec()s
        317384 by: tamouse mailing lists
        317385 by: rene7705
        317390 by: tamouse mailing lists

whats wrong
        317386 by: saeed ahmed
        317388 by: Tommy Pham
        317391 by: Duken Marga
        317393 by: tamouse mailing lists
        317395 by: Maciek Sokolewicz

some good deals on good books
        317387 by: tamouse mailing lists
        317389 by: Tommy Pham

Re: saeed ahmed
        317392 by: Karl-Arne Gjersøyen

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
Maybe stupid question, but is node.php really necessary? If you can program
PHP and it performs better than node.js, why would you need to have another
wrapper around things. Why not just program "normal" PHP?

twitter: geekdenz
Blog: http://www.thheuer.com

On Sat, Mar 31, 2012 at 10:39 AM, Hiyarli Baba <root...@gmail.com> wrote:

> As like Micheal's said said just keep up alive the project
> I was preferes node.js to pho only when i needed send millions of ssl
> api requests.
> nodejs sends 1k https request in onky 2 second including parsing
> required elements from database , check the returned source write to
> file
>
> if you want develope / clone more modules for that please start from
> http|s.req :p and let me coninue at php
>
> http://stackoverflow.com/a/9199961 my nodejs + php thing
>
> 2012/3/22, Michael Save <savetheinter...@omegasdg.com>:
> > Very nice!
> >
> > I'll have a proper look at this in the morning, and I'll try it out
> > for myself. Looking forward to seeing more development on this.
> >
> > Michael
> >
> > On Thu, Mar 22, 2012 at 11:40 AM, Joseph Moniz <joseph.mo...@gmail.com>
> > wrote:
> >> Hey,
> >>
> >> So i had my first Hackathon at work last week and my project was to
> >> prototype making a node.js clone using PHP instead of V8. So i
> >> snatched up libuv and joyent's HTTP parser and set off on a 24 hour
> >> coding spree to get something workable. By the time the sun was coming
> >> out the next morning the following code was working.
> >>
> >>    <?php
> >>
> >>    $http = new node_http();
> >>
> >>    $http->listen(8080, function($request, $response) {
> >>        $response->end("yay, super awesome response");
> >>    });
> >>
> >>    nodephp_run();
> >>
> >>    ?>
> >>
> >> The C code that powers it was whipped together really fast and is kind
> >> of hackish as a result. The code has some memory leaks that i haven't
> >> had time to fully track down yet. Some small portions of the code were
> >> borrowed from the phode project.
> >>
> >> In a naive benchmark on this simple server VS an equally simple server
> >> in node.js this implementation already out performs node.js in
> >> throughput by being able to serve just under 200% the amount of
> >> requests per second that node.js could. Take that with a grain of salt
> >> though because node.js has much more feature and is much more hardend
> >> from production use. I do believe the PHP binary will have some major
> >> performance gains over V8 as crossing the PHP <--> C barrier seems to
> >> be a much lighter operation then crossing the V8 <--> C++ barrier.
> >>
> >> Any help or feedback will be greatly appreciated. The projects source
> >> code can be found here: https://github.com/JosephMoniz/node.php
> >>
> >> - Joseph Moniz
> >>
> >> --
> >> PHP General Mailing List (http://www.php.net/)
> >> To unsubscribe, visit: http://www.php.net/unsub.php
> >>
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--- End Message ---
--- Begin Message ---
Because "normal" PHP is not asynchronous.

Also, I kind of doubt you can outperform node.js with standard PHP.

On Sat, Mar 31, 2012 at 11:56 AM, German Geek <geek...@gmail.com> wrote:
> Maybe stupid question, but is node.php really necessary? If you can program
> PHP and it performs better than node.js, why would you need to have another
> wrapper around things. Why not just program "normal" PHP?
>
> twitter: geekdenz
> Blog: http://www.thheuer.com
>
> On Sat, Mar 31, 2012 at 10:39 AM, Hiyarli Baba <root...@gmail.com> wrote:
>>
>> As like Micheal's said said just keep up alive the project
>> I was preferes node.js to pho only when i needed send millions of ssl
>> api requests.
>> nodejs sends 1k https request in onky 2 second including parsing
>> required elements from database , check the returned source write to
>> file
>>
>> if you want develope / clone more modules for that please start from
>> http|s.req :p and let me coninue at php
>>
>> http://stackoverflow.com/a/9199961 my nodejs + php thing
>>
>> 2012/3/22, Michael Save <savetheinter...@omegasdg.com>:
>> > Very nice!
>> >
>> > I'll have a proper look at this in the morning, and I'll try it out
>> > for myself. Looking forward to seeing more development on this.
>> >
>> > Michael
>> >
>> > On Thu, Mar 22, 2012 at 11:40 AM, Joseph Moniz <joseph.mo...@gmail.com>
>> > wrote:
>> >> Hey,
>> >>
>> >> So i had my first Hackathon at work last week and my project was to
>> >> prototype making a node.js clone using PHP instead of V8. So i
>> >> snatched up libuv and joyent's HTTP parser and set off on a 24 hour
>> >> coding spree to get something workable. By the time the sun was coming
>> >> out the next morning the following code was working.
>> >>
>> >>    <?php
>> >>
>> >>    $http = new node_http();
>> >>
>> >>    $http->listen(8080, function($request, $response) {
>> >>        $response->end("yay, super awesome response");
>> >>    });
>> >>
>> >>    nodephp_run();
>> >>
>> >>    ?>
>> >>
>> >> The C code that powers it was whipped together really fast and is kind
>> >> of hackish as a result. The code has some memory leaks that i haven't
>> >> had time to fully track down yet. Some small portions of the code were
>> >> borrowed from the phode project.
>> >>
>> >> In a naive benchmark on this simple server VS an equally simple server
>> >> in node.js this implementation already out performs node.js in
>> >> throughput by being able to serve just under 200% the amount of
>> >> requests per second that node.js could. Take that with a grain of salt
>> >> though because node.js has much more feature and is much more hardend
>> >> from production use. I do believe the PHP binary will have some major
>> >> performance gains over V8 as crossing the PHP <--> C barrier seems to
>> >> be a much lighter operation then crossing the V8 <--> C++ barrier.
>> >>
>> >> Any help or feedback will be greatly appreciated. The projects source
>> >> code can be found here: https://github.com/JosephMoniz/node.php
>> >>
>> >> - Joseph Moniz
>> >>
>> >> --
>> >> PHP General Mailing List (http://www.php.net/)
>> >> To unsubscribe, visit: http://www.php.net/unsub.php
>> >>
>> >
>> > --
>> > PHP General Mailing List (http://www.php.net/)
>> > To unsubscribe, visit: http://www.php.net/unsub.php
>> >
>> >
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>

--- End Message ---
--- Begin Message ---
Or: Why doesn't PHP have Applications variables like ASP.NET  (and node.js)
?

Hi,

I'm working on optimising a php application (Drupal).

The best optimisation I've found so far is to use APC to store various bits
of Drupal data in RAM.

The problem with this is that with Drupal requiring say 50Mb of data* per
request is that lots of cpu cycles are wasted de-serialising data out of
apc_fetch. Also 50Mb of data per http process !! is wasted by each one
re-creating it's own copy of the shared data.

If it were possible for apc_fetch (or similar function) to return a pointer
to the data rather than a copy of the data this would enable incredible
reduction in cpu and memory usage.

This is essentially how ASP.NET Application variables and node.js work.

I'm surprised PHP doesn't already have Application variables, given that
they are so similar to Session Variables and that it's been around for a
long time in ASP / ASP.NET.

I just wondered if there was a reason for not having this functionality or
if it's on a road map somewhere or I've missed something :) ?

--- End Message ---
--- Begin Message ---
On 31 Mar 2012, at 02:33, Simon wrote:

> Or: Why doesn't PHP have Applications variables like ASP.NET  (and node.js)
> ?
> 
> Hi,
> 
> I'm working on optimising a php application (Drupal).
> 
> The best optimisation I've found so far is to use APC to store various bits
> of Drupal data in RAM.
> 
> The problem with this is that with Drupal requiring say 50Mb of data* per
> request is that lots of cpu cycles are wasted de-serialising data out of
> apc_fetch. Also 50Mb of data per http process !! is wasted by each one
> re-creating it's own copy of the shared data.

50MB? WTF is it storing?? I've never used Drupal, but based purely on that it 
sounds like an extremely inefficient piece of software that's best avoided!

> If it were possible for apc_fetch (or similar function) to return a pointer
> to the data rather than a copy of the data this would enable incredible
> reduction in cpu and memory usage.

Vanilla PHP adheres to a principle known as "shared nothing architecture" in 
which, shockingly, nothing is shared between processes or requests. This is 
primarily for scalability reasons; if you stick to the shared nothing approach 
your application should be easily scalable.

> This is essentially how ASP.NET Application variables and node.js work.

Not a valid comparison. Node.js applications can only share variables within a 
single process, and they can do so because it's single-threaded. Once you scale 
your app beyond a single process you'd need to add a custom layer on to share 
data between them.

I'm not sure about the architecture behind IIS and ASP.net but I imagine there 
are similar paradigms at work.

> I'm surprised PHP doesn't already have Application variables, given that
> they are so similar to Session Variables and that it's been around for a
> long time in ASP / ASP.NET.

Just because x does it, doesn't mean y should. I've used lots of languages over 
the years, including classic ASP, ASP.net, Perl, Python, Ruby, PHP (obv), and 
more, and I'm yet to see a compelling reason to want application variables. Let 
go of the possibility of application variables and your thinking will shift to 
other ways of solving the problem.

> I just wondered if there was a reason for not having this functionality or
> if it's on a road map somewhere or I've missed something :) ?


As far as I am aware, ASP and ASP.net are the only web technologies to support 
application variables out of the box. You think that's simply because the 
others just haven't gotten around to it yet?

-Stuart

-- 
Stuart Dallas
3ft9 Ltd
http://3ft9.com/

--- End Message ---
--- Begin Message ---
On Fri, Mar 30, 2012 at 7:05 AM, David OBrien <dgobr...@gmail.com> wrote:
> Find a way to do it using PHP's imagemagick extensions
>
> http://php.net/manual/en/book.imagick.php
>
> On Fri, Mar 30, 2012 at 5:56 AM, rene7705 <rene7...@gmail.com> wrote:
>
>> Hi.
>>
>> I have a script that uses imagemagick's convert command on the commandline
>> to get it's work done.
>> These calls to exec('convert [params]') take params from the end-user via a
>> html form, so is very unsecure.
>>
>> The intention is that the end-user only runs this script on localhost, from
>> localhost.
>>
>> So now i'm checking $_SERVER['REMOTE_ADDR']===$_SERVER['SERVER_ADDR'] to
>> see if I can allow the script to be used.
>>
>> But unfortunately, $_SERVER['REMOTE_ADDR'] is my external IP, and
>> $_SERVER['SERVER_ADDR'] is my internal IP.
>>
>> How would I best fix this?
>>

I, too, would suggest you use the PHP extensions rather than shell out
a command for various reasons, security being possibly the highest.
There is also the cost of another process on the box, and doing the
translation in and out.

And David, please bottom post responses.

--- End Message ---
--- Begin Message ---
escapeshellcmd() seems simplest.

--- End Message ---
--- Begin Message ---
On Sat, Mar 31, 2012 at 1:37 AM, rene7705 <rene7...@gmail.com> wrote:
> escapeshellcmd() seems simplest.

It might be if all you care about are shell meta characters, and
admittedly it will save you from someone entering "& rm -rf / &" in
your input field. But dealing with generic user input, even escaped,
can still be problematic. Say you want to let the user set the size of
the output file, and the user enters a bunch of letters instead of a
geometry. Do you really want to have to deal with all the possible
ramifications of such GIGO stuff? Better to vet the data, untaint it,
and deal with it that way. When you've gone to all that, you're almost
all the way to where you need to be to use the library functions.

--- End Message ---
--- Begin Message ---
i have made a php script with a tutorial help....i dont know where is a
error.please have a look


<?php
//connect and select a database
mysql_connect("localhost","root"," " );
mysql_select_db("addressbook");
//Run a query
$result=mysql_query("select("SELECT*FROM COLLEAGUE");
?>
<!DOCTYPE html PUBLIC"-//w3c//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
<html xmlns="http://www.w3.org/1999/xhtml";>
<head>
<title>Address Book</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8"
</head>
<body>
<h1>Address Book<h1>
<table Border="1" cellpadding="2"cellspacing="3"
summary="table holda colleague contacts information">
<tr>
<th> ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Telephone</th>
<th>Email Address</th>
</tr>
<?php
//LOOP through all table rows
while ($row=mysql_fetch_array($result)) {
echo"<tr>";
echo"<td>".$row['id'] . "</td>;
echo "<td>" . $row['firstName'] . "</td>";
echo "<td>" . $row['lastName'] . "</td>";
echo "<td>" . $row['telephone'] . "</td>";
echo "<td>" . Srow['email'] . "</td";
echo"</tr>";
}
//free result memory and close database connection
mysql_free_result($result);
mysql_close();
?>
</table>
</body>
</html>
thanks

--- End Message ---
--- Begin Message ---
On Fri, Mar 30, 2012 at 11:45 PM, 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
>

Your code below assumes that everything is perfect in your world.  IE:
 no network connectivity issue, all firewall related are properly
configured, the user account actually exists and have the right
permissions, the database exists and so are the table(s), etc....  I
suggest you revisit the official PHP manual and read about each
function you're using.

>
> <?php
> //connect and select a database
> mysql_connect("localhost","root"," " );

for starters,

http://php.net/function.mysql-connect

Noticed the result returned.  You didn't bother to check whether if
it's successful or not.  If not why not?  Since the DB is on the same
box, is the firewall, if any, configured properly?  If there's no
firewall or if it's configured properly, is the account valid and have
the right permissions?  Is the MySQL server running on the default
port 3306?

If you don't know how to configure each of the above mentioned, allow
me to introduce you a new friend, Google ;)  Both of those topics are
beyond the scope of this list.

HTH,
Tommy

> mysql_select_db("addressbook");
> //Run a query
> $result=mysql_query("select("SELECT*FROM COLLEAGUE");
> ?>
> <!DOCTYPE html PUBLIC"-//w3c//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
> <html xmlns="http://www.w3.org/1999/xhtml";>
> <head>
> <title>Address Book</title>
> <meta http-equiv="content-type" content="text/html;charset=utf-8"
> </head>
> <body>
> <h1>Address Book<h1>
> <table Border="1" cellpadding="2"cellspacing="3"
> summary="table holda colleague contacts information">
> <tr>
> <th> ID</th>
> <th>First Name</th>
> <th>Last Name</th>
> <th>Telephone</th>
> <th>Email Address</th>
> </tr>
> <?php
> //LOOP through all table rows
> while ($row=mysql_fetch_array($result)) {
> echo"<tr>";
> echo"<td>".$row['id'] . "</td>;
> echo "<td>" . $row['firstName'] . "</td>";
> echo "<td>" . $row['lastName'] . "</td>";
> echo "<td>" . $row['telephone'] . "</td>";
> echo "<td>" . Srow['email'] . "</td";
> echo"</tr>";
> }
> //free result memory and close database connection
> mysql_free_result($result);
> mysql_close();
> ?>
> </table>
> </body>
> </html>
> thanks

--- End Message ---
--- Begin Message ---
Please check this line:
> $result=mysql_query("select("SELECT*FROM COLLEAGUE");

I think it should be:
 > $result=mysql_query("SELECT * FROM COLLEAGUE");

On Sat, Mar 31, 2012 at 1:45 PM, 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
>
>
> <?php
> //connect and select a database
> mysql_connect("localhost","root"," " );
> mysql_select_db("addressbook");
> //Run a query
> $result=mysql_query("select("SELECT*FROM COLLEAGUE");
> ?>
> <!DOCTYPE html PUBLIC"-//w3c//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
> <html xmlns="http://www.w3.org/1999/xhtml";>
> <head>
> <title>Address Book</title>
> <meta http-equiv="content-type" content="text/html;charset=utf-8"
> </head>
> <body>
> <h1>Address Book<h1>
> <table Border="1" cellpadding="2"cellspacing="3"
> summary="table holda colleague contacts information">
> <tr>
> <th> ID</th>
> <th>First Name</th>
> <th>Last Name</th>
> <th>Telephone</th>
> <th>Email Address</th>
> </tr>
> <?php
> //LOOP through all table rows
> while ($row=mysql_fetch_array($result)) {
> echo"<tr>";
> echo"<td>".$row['id'] . "</td>;
> echo "<td>" . $row['firstName'] . "</td>";
> echo "<td>" . $row['lastName'] . "</td>";
> echo "<td>" . $row['telephone'] . "</td>";
> echo "<td>" . Srow['email'] . "</td";
> echo"</tr>";
> }
> //free result memory and close database connection
> mysql_free_result($result);
> mysql_close();
> ?>
> </table>
> </body>
> </html>
> thanks
>



-- 
Duken Marga

--- End Message ---
--- Begin Message ---
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");

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
string:

   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:

  <?php
  error_reporting(-1); // turns on EVERYTHING
  ini_set('display_errors',true);
  ini_set('display_startup_errors',true);

  // 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:

    mysql_select_db("addressbook");

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
mysql:

   $sql = "select * from colleague";
   $result = mysql_query($db, $sql);
   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:

   mysql_close($db);

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

--- End Message ---
--- Begin Message ---
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
string:

    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:

   <?php
   error_reporting(-1); // turns on EVERYTHING
   ini_set('display_errors',true);
   ini_set('display_startup_errors',true);

   // 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:

     mysql_select_db("addressbook");

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
mysql:

    $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:

    mysql_close($db);

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

You made mistakes during debugging :s


--- End Message ---
--- Begin Message ---
I know there are quite a few people here who are just beginning to
learn how to write programs as they also learn about PHP. O'Reilly has
a deal on some pretty important books available electronically, save
50%. If this appeals to you, head to <
http://shop.oreilly.com/category/deals/essential-code.do > and check
them out.

( I apologize if this offends anyone's sensibilities. I am not in the
employ of O'Reilly, nor is this going to make me any scratch. I just
think this is a good chance to pick up some pretty useful books. )

--- End Message ---
--- Begin Message ---
On Sat, Mar 31, 2012 at 12:02 AM, tamouse mailing lists
<tamouse.li...@gmail.com> wrote:
>
> ( I apologize if this offends anyone's sensibilities. I am not in the
> employ of O'Reilly, nor is this going to make me any scratch. I just
> think this is a good chance to pick up some pretty useful books. )
>

Makes a lot of sense to me for someone kind enough to share a great
offer to fellow coders/hackers :)

Thanks.  I'll pass this along to the folks newly interested in PHP.

--- End Message ---
--- Begin Message ---
Try this:

mysql> CREATE DATABASE addressbook;
Query OK, 1 row affected (0.00 sec)

mysql> USE addressbook;
Database changed

mysql> CREATE TABLE userData(id INT(4) NOT NULL AUTO_INCREMENT PRIMARY KEY,
 firstName VARCHAR(50) NOT NULL,
 lastName VARCHAR(50) NOT NULL,
 telephone INT(12) NOT NULL,
 email VARCHAR(100) NOT NULL);
Query OK, 0 rows affected (0.11 sec)

Now when you have the databse created and a table to store data, you
need to write a PHP page that do the trick.
Here is a simpe sample. Read all comments and look at the source code...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd";>
<html xmlns="http://www.w3.org/1999/xhtml"; xml:lang="en">
<head>
        <meta http-equiv="content-type" content="application/xhtml+xml;
charset=utf-8" />
        <title>Addressbook</title>
        <meta name="generator" content="BBEdit 10.1" />
</head>
<body>
<?php
if(!isset($_POST['updateDB'])){
?>

<form action="addressbook.php" method="post">
<fieldset>
   <legend>Contact info:</legend>
   <p>
   <label for="firstName">First Name:</label><br />
   <input type="text" id="firstName" name="firstName" />
   </p>
   <p>
   <label for="lastName">Last Name:</label><br />
   <input type="text" id="lastName" name="lastName" />
   </p>
   <p>
   <label for="telephone">Telephone:</label><br />
   <input type="text" id="telephone" name="telephone" />
   </p>
   <p>
   <label for="emailAddress">Email Address:</label><br />
   <input type="text" id="emailAddress" name="emailAddress" />
   </p>
   <p>
   <input type="submit" name="updateDB" value="Update DB" />
   </p>
</fieldset>

</form>

<?php
} else {
        // Formfields
        $firstName = $_POST['firstName'];
        $lastName = $_POST['lastName'];
        $telephone = $_POST['telephone'];
        $email = $_POST['emailAddress'];
        
        // Delete all kind of tags that may be found in form fields
        $firstName = strip_tags($firstName);
        $lastName = strip_tags($lastName);
        $telephone = strip_tags($telephone);
        $email = strip_tags($email);
        
        // Just a simple way to a little more secure query
        $firstName = mysql_real_escape_string($firstName);
        $lastName = mysql_real_escape_string($lastName);
        $telephone = mysql_real_escape_string($telephone);
        $email = mysql_real_escape_string($email);

        // Connect to the Database
        $connectDB = mysql_connect("localhost","root","br0ok2meg") or
die(mysql_error());
        // Select Database
        $selectDB = mysql_select_db("addressbook") or die(mysql_error());
        
        // Now create the query that insert data into table userData
        $sql = "INSERT INTO
userData(firstName,lastName,telephone,email)VALUES(\"$firstName\",\"$lastName\",\"$telephone\",\"$email\")";
        
        // Run the query that actually insert data into the table userData
        mysql_query($sql,$connectDB) or die(mysql_error());
        echo "<p>The Contact information is registred</p>";
        }
?>

<p><a href="addressbook.php?view">View Contact Info</a></p>

<?php
if(isset($_GET['view'])){
        // Connect to the Database
        $connectDB = mysql_connect("localhost","root","br0ok2meg") or
die(mysql_error());
        // Select Database
        $selectDB = mysql_select_db("addressbook") or die(mysql_error());
        
        // Ask for information in table userData
        $sql = "SELECT * FROM userData";
        $result = mysql_query($sql,$connectDB) or die(mysql_error());

        // Handle received data
        while($row = mysql_fetch_array($result)){
                $firstName = $row['firstName'];
                $lastName = $row['lastName'];
                $telephone = $row['telephone'];
                $email = $row['email'];
        
        
        // Print the result to screen
        echo "<p><strong>First Name:</strong> $firstName<br />";
        echo "<strong>Last Name:</strong> $lastName<br />";
        echo "<strong>Telephone:</strong> $telephone<br />";
        echo "<strong>Email:</strong> $email<br />";
        echo "</p>";
        }
}

?>
</body>
</html>

Hope this can be in help for you.

Karl
-- 
Hjemmeside: http://www.karl-arne.name/



Den 08:45 31. mars 2012 skrev saeed ahmed <mycomputerbo...@gmail.com> følgende:
> i have made a php script with a tutorial help....i dont know where is a
> error.please have a look
>
>
> <?php
> //connect and select a database
> mysql_connect("localhost","root"," " );
> mysql_select_db("addressbook");
> //Run a query
> $result=mysql_query("select("SELECT*FROM COLLEAGUE");
> ?>
> <!DOCTYPE html PUBLIC"-//w3c//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
> <html xmlns="http://www.w3.org/1999/xhtml";>
> <head>
> <title>Address Book</title>
> <meta http-equiv="content-type" content="text/html;charset=utf-8"
> </head>
> <body>
> <h1>Address Book<h1>
> <table Border="1" cellpadding="2"cellspacing="3"
> summary="table holda colleague contacts information">
> <tr>
> <th> ID</th>
> <th>First Name</th>
> <th>Last Name</th>
> <th>Telephone</th>
> <th>Email Address</th>
> </tr>
> <?php
> //LOOP through all table rows
> while ($row=mysql_fetch_array($result)) {
> echo"<tr>";
> echo"<td>".$row['id'] . "</td>;
> echo "<td>" . $row['firstName'] . "</td>";
> echo "<td>" . $row['lastName'] . "</td>";
> echo "<td>" . $row['telephone'] . "</td>";
> echo "<td>" . Srow['email'] . "</td";
> echo"</tr>";
> }
> //free result memory and close database connection
> mysql_free_result($result);
> mysql_close();
> ?>
> </table>
> </body>
> </html>
> thanks

--- End Message ---

Reply via email to