Page 2 of 2 FirstFirst 12
Results 11 to 19 of 19

Thread: Creating an sql database to store an ip adress

  1. #11
    Join Date
    Mar 2011
    Posts
    2,170
    Thanks
    60
    Thanked 120 Times in 116 Posts
    Blog Entries
    4

    Default

    Hey everyone,
    The script to write in their name and ip adress now works. I'm trying to retrieve the data now.

    PHP Code:
    <?php

    mysql_connect
    ("local_host""****""****") or die(mysql_error());
    mysql_select_db("****") or die(mysql_error());


    $result mysql_query("SELECT * FROM example
     WHERE ipadress='
    $_SERVER['REMOTE_ADDR'];'") or die(mysql_error());  


    $row mysql_fetch_array$result );
    echo 
    $row['name']." - ".$row['ipadress'];
    ?>
    It comes up with this error.

    Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home1/keyboard/public_html/databaseretrieve.php on line 8


    Any help would be appreciated.

  2. #12
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I don't see the problem. Which is line 8? If you comment it out does the parse error disappear? (The script won't work properly, but that will help you debug it-- first step is finding the error.)

    One problem is that you have the semi-colon and close-single-quote reversed in the query. This shouldn't be causing a PHP parse error but MySQL won't like it. Switch ;' to ';.

    Secondly what happens if you put braces around this:
    $_SERVER['REMOTE_ADDR']
    So it becomes
    {$_SERVER['REMOTE_ADDR']}
    (Within the query, everything else as-is.)

    But I'm still not sure what is causing the parse error. You can find it by trial and error by commenting out lines or by simplifying them so they parse (just temporarily). After you find the problem then look into fixing it.

    The only thing that looks a bit odd regarding whitespace to me is the following:
    $row = mysql_fetch_array( $result );
    Remove the spaces around $result. I wouldn't expect that to be a problem, but I also never have spaces like that, so it's something to check.

    Also, try removing the line break in the MySQL query. That's also slightly unusual whitespace. But I'm almost positive that is unrelated because it's within a string.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #13
    Join Date
    Mar 2011
    Posts
    2,170
    Thanks
    60
    Thanked 120 Times in 116 Posts
    Blog Entries
    4

    Default

    PHP Code:
    <?php

    mysql_connect
    ("local_host""****""****") or die(mysql_error());
    mysql_select_db("****") or die(mysql_error());


    // $result = mysql_query("SELECT * FROM example
    WHERE ipadress='$_SERVER['REMOTE_ADDR']';") or die(mysql_error());  


    $row = mysql_fetch_array($result);
    echo 
    $row['name']." ".$row['ipadress'];
    ?>
    Now this error comes up

    Parse error: syntax error, unexpected T_STRING in /home1/keyboard/public_html/databaseretrieve.php on line 8

  4. #14
    Join Date
    Mar 2011
    Posts
    2,170
    Thanks
    60
    Thanked 120 Times in 116 Posts
    Blog Entries
    4

    Default

    Also tried this

    PHP Code:
    <?php

    mysql_connect
    ("local_host""****""****") or die(mysql_error());
    mysql_select_db("****") or die(mysql_error());


    $result mysql_query("SELECT * FROM example
    WHERE ipadress='
    {$_SERVER['REMOTE_ADDR']}';") or die(mysql_error());  


    $row mysql_fetch_array($result);
    echo 
    $row['name']." - ".$row['ipadress'];
    ?>

    and now it comes up with this


    Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host 'local_host' (1) in /home1/keyboard/public_html/databaseretrieve.php on line 3
    Unknown MySQL server host 'local_host' (1)

  5. #15
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    First, you can't comment out half of a line. As I said, it's best to put a single line on a single line within the file-- you have an extra line break in the middle of your SQL query. There's no need for that. (It probably also won't hurt anything.) So either put the MySQL query on a single line (remove the line break before "WHERE"), or comment out BOTH lines (because they represent a single line of code) to see if that is the problem-- from what you've posted it does seem that that section is the problem.

    Second, it's "localhost" not "local_host" and make sure you're actually using your information (username, password) rather than the stars. (But of course don't post those here.)

    As for the other changes you made regarding the variable within the string, that will probably be fine. But you'll need to fix the other two things first. Now that you have a MySQL error that means there is no longer a parse error. Parse errors occur before the code is used at all because PHP does not understand what to do. A MySQL error (like many other kinds of errors) is a runtime error, something that happens while the script is being processed.

    Anyway, it seems that if you fix your MySQL information this script should work, or at least you will move on to the next part of debugging.

    I think the original problem was in the way you used an array key without brackets {} inside the string. That seems fixed now.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #16
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    PHP Code:
    "SELECT * FROM example WHERE ipadress='{$_SERVER['REMOTE_ADDR']}';" 
    while this will generate a correctly formed SQL statement, you should not include the ending semicolon when using mysql_query(). I suspect this has to do with how mysql_query() is designed to execute only one statement at a time (the semicolon ends the statement and makes ready to begin a new one) and because the function tries to add the semicolon itself when run.

  7. The Following User Says Thank You to traq For This Useful Post:

    djr33 (07-25-2011)

  8. #17
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Really? I always add the semi-colon to avoid any possible injection (although that's avoided in other ways already). I was under the impression that two queries could be run with it, but I can't remember ever trying that. Your link confirms what you're saying... interesting. Thanks for mentioning it.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. #18
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    dunno, really. I've never tried two statements at once either. it does say "should not," though, not "must not." maybe it's the kind of thing that can cause problems in older php/sql versions or on particular server setups. who knows...

  10. The Following User Says Thank You to traq For This Useful Post:

    djr33 (07-25-2011)

  11. #19
    Join Date
    Mar 2011
    Posts
    2,170
    Thanks
    60
    Thanked 120 Times in 116 Posts
    Blog Entries
    4

    Default

    Thanks so much everyone

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
  •