+ Reply to Thread
Results 1 to 10 of 10

Thread: php help

  1. #1
    sax0n's Avatar
    sax0n is offline x10Hosting Member sax0n is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    11

    php help

    Can anyone help me with this?

    PHP Code:
    <html>
    <head>
    <title> Main </title>
    </head>
    <body>
    <h3> Test </h3>
    <?php 
    $dbhost 
    'localhost';
    $dbuser 'sax0n';
    $dbpass 'password';

    $conn mysql_connect($dbhost$dbuser$dbpass) or die
                         (
    'Error connecting to mysql');

    mysql_select_db("sax0n_main") or die; 

    $username saxon
    $ui_table 
    mysql_query("SELECT * FROM user_info WHERE username == $username");
    mysql_fetch_array($ui_table);
    $co_long $ui_table[co-ords_long];
    $co_lat $ui_table[co-ords_lat]; 

    $player_location mysql_query("SELECT Name FROM map WHERE co-ords-long == $co_long AND co-ords-lat = $co_lat"); 
    echo 
    $player_location
    ?>
    </body>
    </html>
    It outputs this...
    Parse error: syntax error, unexpected T_VARIABLE in /home/sax0n/public_html/main.php on line 18


    Any help is much appreciated I bet it's something painfully obvious...
    Last edited by sax0n; 10-22-2008 at 04:53 AM. Reason: Forgot to put in the error! :P

  2. #2
    dickey's Avatar
    dickey is offline x10 Sophmore dickey is an unknown quantity at this point
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    128

    Re: php help

    check this code out. it isn't much different from what you posted. but look carefully and you see that you missed out on two semi-colons that signal the end of a command line. which will treat the unterminated line and the next line as one line of command hence the T_VARIABLE ERROR.
    PHP Code:
    <html>
    <head>
    <title> Main </title>
    </head>
    <body>
    <h3> Test </h3>
    <?php 
    $dbhost 
    'localhost';
    $dbuser 'sax0n';
    $dbpass 'password';

    $conn mysql_connect($dbhost$dbuser$dbpass) or die
                         (
    'Error connecting to mysql');

    mysql_select_db("sax0n_main") or die; 

    $username saxon//you forgot the semi-colon here...
    $ui_table mysql_query("SELECT * FROM user_info WHERE username == $username");
    mysql_fetch_array($ui_table);
    $co_long $ui_table[co-ords_long];
    $co_lat $ui_table[co-ords_lat]; 

    $player_location mysql_query("SELECT Name FROM map WHERE co-ords-long == $co_long AND co-ords-lat = $co_lat"); 
    echo 
    $player_location// and this...
    ?>
    </body>
    </html>
    and you're right it is easy but it isn't that obvious, it happens to me all the time. that's why I know it has something to do with syntax.
    Last edited by dickey; 10-22-2008 at 05:19 AM.

  3. #3
    sax0n's Avatar
    sax0n is offline x10Hosting Member sax0n is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    11

    Re: php help

    Thanks! That worked but now I have another error :happysad: :

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/sax0n/public_html/main.php on line 19

    I don't like to be a pain but I can't figure it out.

  4. #4
    xPlozion's Avatar
    xPlozion is offline x10 Elder xPlozion is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    Delaware, USA
    Posts
    872

    Re: php help

    try putting an apostrophe around any variables in the database $username. if you don't, then it thinks that you are trying to compare 2 fields ;). the same goes for your second query. also, if you're just getting one row of data, then append LIMIT 1 to the end of your query.

    for example, $username = bob. the query will look for " WHERE username = bob".

    - a couple other pointers... one, you don't need 2 equal signs (=) to compare in the where clause. just a single one will do fine.
    - unless you need keys (1,2...9,10) from your database, mysql_fetch_assoc will do just fine. mysql_fetch_array() will actually create an array ($key => $value), creating useless overhead (unless you have explicit purpose to do so).
    - you have to define a variable for mysql_fetch_array/assoc. it will not automatically adhere to the variable in the query.
    - also, you do not have to set the database connection to a variable. you can, but it's redundant unless you have a use for it, such as mysqli ;)

    PHP Code:
    <html>
    <head>
    <title> Main </title>
    </head>
    <body>
    <h3> Test </h3>
    <?php 
    $dbhost 
    'localhost';
    $dbuser 'sax0n';
    $dbpass 'password';

    mysql_connect($dbhost$dbuser$dbpass) or die('Error connecting to mysql');

    mysql_select_db("sax0n_main") or die; 

    $username 'saxon'//you forgot the semi-colon here... and the quote to tell it it's a variable
    $ui_query mysql_query("SELECT * FROM user_info WHERE username = '$username' LIMIT 1");
    $ui_table mysql_fetch_assoc($ui_query);
    $co_long $ui_table['co-ords_long']; // putting apostrophes in here will reduce errors
    $co_lat $ui_table['co-ords_lat']; // and here too :)

    $player_location mysql_query("SELECT Name FROM map WHERE co-ords-long = '$co_long' AND co-ords-lat = '$co_lat'"); 
    echo 
    $player_location// and this... why are you echoing a query???
    // what you have done with the above is basically "echo true;" or "echo false;" depending on whether or not the query executes without any errors
    //you would again want to do another mysql_fetch_assoc
    ?>
    </body>
    </html>
    Sources: endless months of browsing php.net for w/e reason (prob cause i was bored :P), and a few years of experience ;)
    Last edited by xPlozion; 10-24-2008 at 01:30 AM.

  5. #5
    sax0n's Avatar
    sax0n is offline x10Hosting Member sax0n is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    11

    Re: php help

    Thanks guys! You are a great help!!!

    Just so you know I don't rush to you every time I get an error, I do try to figure it out myself. Something that will imprrove in time.

    I have modified my code and it now looks like this:

    PHP Code:
    <html>
    <head>
    <title> Main </title>
    </head>
    <body>
    <h3> Test </h3>
    <?php 
    $dbhost 
    'localhost';
    $dbuser 'sax0n';
    $dbpass 'password';

    mysql_connect($dbhost$dbuser$dbpass) or die('Error connecting to mysql');

    mysql_select_db("sax0n_main") or die; 

    $username 'saxon'//you forgot the semi-colon here... and the quote to tell it it's a variable
    $ui_query mysql_query("SELECT * FROM user_info WHERE username = '$username' LIMIT 1");
    $ui_table mysql_fetch_assoc($ui_query);
    echo 
    $ui_table[level];
    $co_long $ui_table['co-ords_long']; // putting apostrophes in here will reduce errors
    $co_lat $ui_table['co-ords_lat']; // and here too :)

    $map_query mysql_query("SELECT * FROM map WHERE co-ords_long = '$co_long' AND co-ords_lat = '$co_lat'"); 
    if (
    '$map_query'){
        echo 
    "passed";
    }
    $map_table mysql_fetch_assoc($map_query);
    $player_loaction $map_table[Name];
    echo 
    $player_location// and this... why are you echoing a query???
    // what you have done with the above is basically "echo true;" or "echo false;" depending on whether or not the query executes without any errors
    //you would again want to do another mysql_fetch_assoc
    ?>
    </body>
    </html>
    With this output:

    Test
    1passed
    Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/sax0n/public_html/main2.php on line 27

    What's it doing now?!?

    Thanks guys. Also I tried google but I still don't get the differnece between mysql_fetch_assoc and mysql_fetch_array.



    ...man I can't wait untill i'm the one answering these questions!

  6. #6
    cursedpsp is offline x10 Sophmore cursedpsp is an unknown quantity at this point
    Join Date
    Apr 2008
    Location
    Wiltshire, England
    Posts
    238

    Re: php help

    $map_query = mysql_query("SELECT * FROM map WHERE co-ords_long = '$co_long' AND co-ords_lat = '$co_lat'");
    if ('$map_query'){
    echo "passed";
    }
    $map_table = mysql_fetch_assoc($map_query);

    Where it has that ^^ copy:
    $map_query = mysql_query("SELECT * FROM map WHERE co-ords_long = '$co_long' AND co-ords_lat = '$co_lat'");

    under the if statement.

    so it should be
    $map_query = mysql_query("SELECT * FROM map WHERE co-ords_long = '$co_long' AND co-ords_lat = '$co_lat'");
    if ('$map_query'){
    echo "passed";
    }
    $map_query = mysql_query("SELECT * FROM map WHERE co-ords_long = '$co_long' AND co-ords_lat = '$co_lat'");
    $map_table = mysql_fetch_assoc($map_query);

    usually what i find is that with mysql - after an if statement it clears variable.

  7. #7
    sax0n's Avatar
    sax0n is offline x10Hosting Member sax0n is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    11

    Re: php help

    Didn't work. Same error.

  8. #8
    xmakina's Avatar
    xmakina is offline x10 Lieutenant xmakina is an unknown quantity at this point
    Join Date
    May 2008
    Location
    England
    Posts
    265

    Re: php help

    Just before
    PHP Code:
    $map_query mysql_query("SELECT * FROM map WHERE co-ords_long = '$co_long' AND co-ords_lat = '$co_lat'"); 
    place the line
    PHP Code:
    echo "<p>SQL = SELECT * FROM map WHERE co-ords_long = '$co_long' AND co-ords_lat = '$co_lat'</p>"
    Take the echo'd SQL statement and (if you can't see any glaring errors) place it into phpMyAdmin and run it, that'll give you a much more useful error message.
    Last edited by xmakina; 10-26-2008 at 09:52 AM.
    IF($this->$post.content() == "SEE SIG"){
    w3Schools and Google
    }

  9. #9
    xPlozion's Avatar
    xPlozion is offline x10 Elder xPlozion is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    Delaware, USA
    Posts
    872

    Re: php help

    well, first off, where you have if('$map_query'), it should be if($map_query). the apostrophe actually prints out $map_query, and does not parse _ANY_ variables. quotes would parse the variables. Another point: you do not have to call the same query twice. it will stay with that variable that you assign it to.

  10. #10
    dickey's Avatar
    dickey is offline x10 Sophmore dickey is an unknown quantity at this point
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    128

    Re: php help

    PHP Code:
    $username 'saxon'//you forgot the semi-colon here... and the quote to tell it it's a variable
    $query_string="SELECT * FROM user_info WHERE username = '$username' LIMIT 1"
    $ui_query mysql_query($query_string); //sometimes this works. 
    //insert this code here to prevent some errors I guess.
    if ($ui_query==0
    {
      echo 
    "Invalid username";
    }
      else
    {
      
    $ui_table mysql_fetch_assoc($ui_query);
      ...
      
    // try to decide which code should go here. 

+ Reply to Thread

Similar Threads

  1. Ever Been Suspended For Using PHP?
    By dragoneye_xp in forum Off Topic
    Replies: 26
    Last Post: 08-16-2009, 07:17 PM
  2. [PHP] Variables in PHP
    By Bryon in forum Tutorials
    Replies: 15
    Last Post: 01-29-2009, 09:46 AM
  3. currently have an application pending php
    By biomasti in forum Free Hosting
    Replies: 1
    Last Post: 09-03-2008, 01:58 PM
  4. php errors galore
    By DMG Online in forum Scripts & 3rd Party Apps
    Replies: 9
    Last Post: 05-17-2008, 06:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers