SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Thread: php syntax help

  1. #1
    SitePoint Zealot
    Join Date
    Jan 2005
    Location
    uk colchester
    Posts
    177
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php syntax help

    I am new to coding in php by hand and am trying to get away from doing it with dreamweaver, so this could be the first of many post.

    ok i have created a database and populated it with meaningless data, this is all for practice, I have pointed a form at it using post and now i am getting a parse error of this back from the php page,

    Parse error: parse error, unexpected '}' in /var/www/html/newsite.php on line 30

    below is the lines leading up to line 30, I have taken this connection proccess from another online tutorial, I have gone though this and tried changing bits but just cant understand the problem


    27: //connect to the database
    28: $connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
    29:mysql_select_db($dbname);
    30: }

    Now i must be honest I am no natrualwith this and I dont understand the bit starting (mysql_errno at all, I think that is something to do with the error message should it not connect.

    Hope someone can help with my quest to understand php and hope this is not to much for one thread this is my first post here at sitepoint.

    Regards
    Ian

  2. #2
    Non-Member bronze trophy geniusgoalie's Avatar
    Join Date
    Sep 2004
    Location
    Buffalo, USA
    Posts
    979
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could you please show your whole code, as there is nothing wrong with those lines alone (at a quick glance)
    And please, ask questions, I learned just about everything I know about php from these forums

  3. #3
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Further to Danny's suggestion, count the number of brace pairs. They should always be even an number as every opening one has a closing one!

    SpikeZ
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  4. #4
    Romans 12:2 codyrockx's Avatar
    Join Date
    Jul 2003
    Location
    Newberg, Oregon
    Posts
    422
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As spikeZ said; the bracket sets must be always in pairs and in conjunction with an if/switch/while/do/etc. statement. Looking at your code here you simple have an extra bracket misplaced below the connection.
    Codyrobert.com - Designer and Developer

  5. #5
    SitePoint Zealot
    Join Date
    Jan 2005
    Location
    uk colchester
    Posts
    177
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think the } is what i forgot to delete after playing around when trying to make it work, must admit very impressed and the quick responce here so thanks very much, ok will paste all of my code now, the table in the database is called cars with 4 cols,
    Quick edit here, after taking out the } i have just tried again but this time get passed the mysql connect, now i have this error


    select * from cars where make=audi and price='25'
    here is the content of this database


    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /var/www/html/newsite.php on line 42

    <?php
    $host = "localhost";
    $user = "manleynet";
    $pass = "mypassword";
    $dbname = "sitename_co_uk";

    //connect to the database
    $connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
    mysql_select_db($dbname);


    //connect to the database we wish to work with and find results
    $sql = "select * from cars where make=" . $_POST['make'] . " and price='" . $_POST['price'] . "'";
    echo $sql;
    ?>

    <p>here is the content of this database</p>


    <?php
    //Display the results of each line in a paragraph
    while ($row = mysql_fetch_array($query)) { echo "<p>",$row['make'],": ",$row['price']; }
    ?>

  6. #6
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You were very nearly there.

    Try this code

    PHP Code:
       
       $host 
    "localhost";
       
    $user "manleynet";
       
    $pass "mypassword";
       
    $dbname "sitename_co_uk";
       
       
    //connect to the database
       
    $connection mysql_connect($host$user$pass) or die (' Can\'t connect to the database ' mysql_error() . '<br />');
       
    mysql_select_db($dbname);
       
       
       
    //connect to the database we wish to work with and find results
       
    $sql 'SELECT make, price'
            
    ' FROM cars'
            
    ' WHERE'
            
    ' make = \'' $_POST['make'] . '\''
            
    ' AND'
            
    ' price = ' $_POST['price']
            ;
       echo 
    $sql;
       
    $result mysql_query($sql);
       
       if (
    false == $result)
       {
         echo 
    'Failed to execute ' $sql ' due to ' mysql_error() . '<br />';
       }
       else
       {
        echo 
    '<p>here is the content of this database</p>';
       
        
    //Display the results of each line in a paragraph
        
    while ($row mysql_fetch_array($result))
        {
         echo 
    '<p>' $row['make'] . ' : ' $row['price'] . '<br />';
        }
      } 
    In SQL statements, strings should be quoted (e.g. make) and integers not quoted (e.g. price). MySQL will allow you to quote numbers, but this is not recommended practice.
    Also, you should only select those columns that you actually need. This reduces the amount of data transferred between the MySQL server and the web server and hence speeds up the query.

    Hope this helps

  7. #7
    SitePoint Zealot
    Join Date
    Jan 2005
    Location
    uk colchester
    Posts
    177
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok understand what you are saying with no quotes around integers, This is the part of php I am strugging with most, I can work out the \'' and \'\'' in the make= on the select statement, sorry to be a pain but could you please explain that part of it for me, however have gained a lot of knowlegde since posting on this forum so thankyou for all help so far

  8. #8
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok

    for the make line, you want to show something that looks like
    make = 'make of car'
    Becasue I surround my strings with single quotes, I need to escape single quotes inside the string itself, otherwise PHP will think they make the end of a string.

    hence
    PHP Code:
       $str 'make = \'' $_POST['make'] . '\''
    is very different from

    PHP Code:
       $str 'make = ' $_POST['make'] . ''
    the first methd will give
    make = 'make of car'
    which is what we want
    the second method will give
    make = make of car
    not what we want at all.

    Hope this makes it a little clearer

  9. #9
    SitePoint Zealot
    Join Date
    Jan 2005
    Location
    uk colchester
    Posts
    177
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for that with you now, You explanation makes sence, Those are the small details that I stuggle with, but very slowly makeing progress.

    Regards
    Ian

  10. #10
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    my pleasure

    I normally do a lot of printing out of variables, especially when building SQL strings, then inspecting the results, and fixing the code.

    The more I do that, the better I get at getting it right, but it does take time and lots of practice.

    Never give up


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •