PHP code help

Discussion in 'Technology' started by trance_fan, Jan 31, 2007.

Users Viewing Thread (Users: 0, Guests: 0)

  1. trance_fan

    trance_fan Registered User

    Joined:
    Nov 7, 2002
    Messages:
    9,022
    Likes Received:
    0
    PHP code help

    I am using this code to get the users forename from the SQL db based on $_SESSION['logname'] (which in the login script is assigned the value of the username, from the login form).

    The code is:

    $sql = "SELECT * FROM users WHERE username='{$_SESSION['logname']}'";
    $result = mysql_query($sql) or die("error");
    $row = mysql_fetch_array($result, MYSQL_ASSOC);
    extract ($row);

    The error message is....

    Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) in /home/ca3csn/public_html/COM313/tcnet/userhome.php on line 12

    Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/ca3csn/public_html/COM313/tcnet/userhome.php on line 12
    error

    Cant see what the problem is? If i comment out the result, row and extract lines the message doesnt come up so im thinking its a problem with the $result command (which is on line 12)

    Ideas?

    Thanks
  2. 1615634792921.png
  3. Yosef Ha'Kohain

    Yosef Ha'Kohain Registered User

    Joined:
    Nov 11, 2001
    Messages:
    20,386
    Likes Received:
    5
    Location:
    Zion
    Re: PHP code help

    commenting out the row, result and extract lines just leaves you with a vraiable so it would work.

    You need to see if you can connect to the databse... if you can connect to the database fine then you know its a syntax error... try an insert instead of extracting rows from the db.
  4. Jason Bourne

    Jason Bourne Registered User

    Joined:
    Oct 14, 2002
    Messages:
    5,258
    Likes Received:
    0
    Re: Re: PHP code help

    This is right ^^

    Look at your connection string.
    Try and read simple data first.. then try more advanced stuff.

    Code looks ok.. as far as I can tell.. but I've never really went too far into PHP only ASP..
  5. trance_fan

    trance_fan Registered User

    Joined:
    Nov 7, 2002
    Messages:
    9,022
    Likes Received:
    0
    theres nothing wrong with the connection, which is why im confused as to what the error is on about - other scripts for listing users etc work just fine.

    They all use the same config file for the connection.

    Again as far as the syntax goes, its just copied from another script with slight changes in the SELECT statement.

    I googled the message and the solution mentioned was to do with the php.ini file which i have no access to!

    Confused!

    Never mind, I will press on.
  6. Yosef Ha'Kohain

    Yosef Ha'Kohain Registered User

    Joined:
    Nov 11, 2001
    Messages:
    20,386
    Likes Received:
    5
    Location:
    Zion
    try changing {$_SESSION['logname']} to the actual value of one of the cells and see if your query works.
  7. dodgy

    dodgy rowr kitty super meow cat

    Joined:
    Sep 18, 2003
    Messages:
    4,728
    Likes Received:
    0
    Location:
    Terra Firma
    Mmm, may be wrong here, but are you trying to import form fields straight into a global variable? If so, it's a security risk and is normally disabled via, coincidentally, php.ini. Could this be the problem? If so, look at import_request_variables() - but you'd be best off cleaning form input first: don't trust any end-user, even your grandma.

    Code:
    ini_set("php.ini_option", "value");
    If you know the name of the php.ini setting that needs changed try using the above if you can't access php.ini itself, it'll change the setting til the end of the script, works on 95% of hosts ;)

    Also, shouldn't this be like so:
    Code:
    $sql = "SELECT * FROM users WHERE username=\\'" . $_SESSION['logname'] . "\\'";
    ?

    If so, a little better may be:
    Code:
    $result = mysql_query("SELECT * FROM users WHERE username=\\'" . $_SESSION['logname'] . "\\' LIMIT 1") or sqlerr(__FILE__, __LINE__);
    $row = mysql_fetch_assoc($result);
    extract ($row);
    die(); :)
  8. trance_fan

    trance_fan Registered User

    Joined:
    Nov 7, 2002
    Messages:
    9,022
    Likes Received:
    0
    Thanks for that Dodgy.

    I'm at work today but will try that code at some point and let you know how I get on.

    I'm building an online IT Support system for the company I work for, it's part of my uni work.

    You mentioned about importing form fields straight into a global variable...

    I do do this in the login script (to pass the username to the session variable logname) but not in the script thats causing the trouble. This works fine and can be seen by echoing the logname session variable.

    The login form, when submitted, gets passed to a script which will process this and then if the username/password is correct, start the session, set the session variables, and then forward the user to their homepage.

    The code you posted at the end does indeed look better, i will give that a try.

    As you can probably tell i'm rather new to PHP but picking it up pretty quickly!

    It may help if I explain what i'm trying to do as there may be another way.

    When the user logs in, I need that login name to select details from both the user and issue tables (the issue table houses all logged support issue details). I assumed that the easiest way to do this would be assigning this username to a session variable and then using that variable to select the appropriate details from the relevant tables.

    When I echo my sql, it does show the value that has been assigned to the logname variable in the output..I will set it to a static value for testing purposes and see how that goes.

    Thanks for the help so far...once ive got this nailed i can get on with the rest of it!
  9. trance_fan

    trance_fan Registered User

    Joined:
    Nov 7, 2002
    Messages:
    9,022
    Likes Received:
    0
    I changed the code about and it's working now so all is well, a thousand thankyous gentlemen :D
  10. dodgy

    dodgy rowr kitty super meow cat

    Joined:
    Sep 18, 2003
    Messages:
    4,728
    Likes Received:
    0
    Location:
    Terra Firma
    Glad it's working :) Just a couple of suggestions on what you've mentioned above - you could try using a cookie to store login info: get it to store the user's id and a hashed password value (do an md5 hash of the user's password and a randomly generated 'secret' that can be stored in the table alongside it, you can then test it at the top-level of your script). Use setcookie("name", "value", expires, "/") to add to a cookie, then you can get the values using the $_COOKIE superglobal array. Also you may find it easier to go by a user's id (assuming you created a unique and auto-incremental field for each user in the user table) as your scripts get bigger, rather than using the username to identify them, especially when it comes to linking multiple tables together etc.

    All in all have fun, I love php, still have problems getting my head round objects though, got to start learning Java soon so guess I'll have to :spangled:
  11. trance_fan

    trance_fan Registered User

    Joined:
    Nov 7, 2002
    Messages:
    9,022
    Likes Received:
    0
    Thanks for the tips dogdy.

    Yeah I do have a user ID and I will use that for table links etc, I just thought that using the username for the session variable would be easier since it just gets taken from the form! I suppose it would be more efficient to select the user id from the table, then use that ID for the session variable.

    I hate Object oriented programming, i just find it confuses everything. I do a Java module..2 actually....and I absolutely despise it.

    so much for "network computing" eh :lol:

    My Modules:

    Project - PHP/Sql, research, report writing
    Advanced Object Oriented Development - Java
    Distributed Object Systems - More Java
    Software Engineering - General Software Bollocks
    Telecoms Technology - self explanatory, interesting but a bit random.
    Network Management - Shit, only ran for a few weeks, and we hardly did anything.

    Proper rubbish this year, can't wait til it's over!!

Share This Page