Results 1 to 9 of 9

Thread: $sql not returning results, this can't be this tough..

  1. #1
    Join Date
    May 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default $sql not returning results, this can't be this tough..

    I am new to php and have run into a brick wall. Any help would be appreciated

    I need an update form on a page that will look to see if anything is entered in the users "welcome" field in their record and, if there is content, display it for editing / updating.

    If there is nothing in the user's field enter "My Default Text" and use that to update the user's welcome field in their record.

    Thus far I have the following.

    In the login form

    Code:
    <input type="submit" name="kt_login1" id="kt_login1" value="Login" />
    In the page that opens as a result of logging in:
    PHP Code:
    <? session_start();
            if (isset(
    $_POST['kt_login1'])):
             
    $_SESSION['kt_login_user'] = $_POST['kt_login_user'];
             
    $_SESSION['kt_name'] = $_POST['kt_name'];
            endif;
    ?>
    <p>Hello <? echo  $_SESSION['kt_name'?> Choose a link to update a part of your records...</p>
    In the page that is to contain the update record form:
    PHP Code:
    <? session_start();
            if (!isset(
    $_SESSION['kt_login_user'])):
            
    $_SESSION['kt_login_user'] = "Anonymous";
            
    $_SESSION['kt_name'] = $_POST['kt_name'];
            
    $_SESSION['kt_welcome'] = $_POST['kt_welcome'];
    endif;
    ?>
    <p>Hello <? echo  $_SESSION['kt_name'?> Yada yadda...</p>
    <?php $sql "SELECT 'welcome' FROM `users` WHERE `id` = $kt_login_user";
    $qry mysql_query($sql);
    if(@
    mysql_num_rows($qry) > 0)
    {
    while(
    $r mysql_fetch_array($qry))
    {
    $kt_welcome $r[1];
    }
    }
    else
    {
    $kt_welcome "My default text";
    }
    ?>
    <textarea name="welcome" cols="80" rows="20"><?php echo $_SESSION['kt_welcome']; ?></textarea>
    <?php mysql_close(); ?>
    I have created 2 users, one with content in the welcome field of their record and the other with a blank welcome field in the welcome field of their record.

    What is happening at the moment is
    1 - The "echo $_SESSION['kt_name" is returning the name of the logged in user (as expected)
    2 - the only thing that is being returned in the textarea is My default text. The contents of the user with pre-existing data in his welcome field is not being returned in spite of being logged in.

    I am coming to honestly believe that PHP was developed to drive me nuts.

    I would appreciate some direction here. Many thanks in advance.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Firstly, it will save you a lot of headaches to
    Code:
    error_reporting(E_ALL);
    at the top of your page.
    Secondly, do you get results when executing this query from a simple MySQL prompt?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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

    Default

    Here's what I'd suggest. There are some loose rules on some of this, so what I'm suggesting will work, but I can't say what you have right now won't.
    I'm just gonna try to clean up the syntax like I'm used to it.. then we'll see if that fixes the problem.

    <?php $sql = "SELECT 'welcome' FROM `users` WHERE `id` = $kt_login_user";
    --Use backticks (``) (above tab) instead of single quotes for 'welcome'.
    >>`welcome`
    You also might want to put some single quotes around $kt_login_user, as that will make it a quoted string when sent to mysql to be parsed.
    I don't know if/think either of this is absolutely required, but it might be giving you a bug.

    while($r = mysql_fetch_array($qry))
    --Personally, I like mysql_fetch_assoc(), as it gives you the same thing, but $r will work like $r['username'], not $r[1]. Just makes a bit more sense.
    Might help you track stuff a bit better. Then again, with only getting one thing, not hard anyway. (The default for some of this stuff is 0, though. Like the first character in a string, $string, can be found using $string[0], not $string[1]. Might be related, but I think mysql_fetch_array does start with 1, to make it more user friendly.)



    And, ok. here's your error.
    $kt_welcome = $r[1];
    ...
    <?php echo $_SESSION['kt_welcome']; ?>
    >>>you've called it like you stored it as a session variable.
    Just use:
    <?php echo $kt_welcome; ?>
    instead of the other echo tag.

    That should work.
    That WILL work, but I can't say it's the only error... but... make sure you do change that.

    If not, add echo statements after every if and query, etc. (echo "it got to if ....") just so you can track exactly where the error happens.

  4. #4
    Join Date
    May 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    error_reporting(E_ALL);
    first returned the error that kt_login_user was not defined. I placed it between ' ' and the error dissapeared. Thanks for that tip, I'll make it standard.

    As for the query (done in PHP MyAdmin)

    SQL query:
    SELECT `welcome`
    FROM `users`
    WHERE 1
    LIMIT 0 , 30

    returns (edited for brevity)

    HTML Code:
    id	welcome
    34	text in Bill 
    37	NULL	

  5. #5
    Join Date
    May 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Code:
    echoing $kt_login_user
    returns the username

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    first returned the error that kt_login_user was not defined. I placed it between ' ' and the error dissapeared.
    Of course it will. You're now using the literal string '$kt_login_user'.

    Also, I see no actual connection attempt. I suggest:
    Code:
    <?php $sql = "SELECT `welcome` FROM `users` WHERE `id` = ". $_SESSION['kt_login_user'] . ";";
    $conn = mysql_connect("localhost", "user", "password"); // change if necessary.
    $qry = mysql_query($sql);
    djr33: I'm sure I've told you before that mysql_fetch_array() returns an array with both associative and numerical indices. I think you're confusing it with mysql_fetch_row(). Both mysql_fetch_row() and mysql_fetch_assoc() are pointless aliases, since mysql_fetch_array() can be used to return either a purely numerical or a purely associative array by specifying MYSQL_NUM or MYSQL_ASSOC as the second argument, respectively.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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

    Default

    It's less to type to use the right function the first place, not add a condition to another one. Never needed numbers myself... so... yeah. Whatever, though... that's fine too.

    The connection attempt... good point. Maybe he took it out though.

    And, here's the answer, as said above:
    And, ok. here's your error.
    $kt_welcome = $r[1];
    ...
    <?php echo $_SESSION['kt_welcome']; ?>
    >>>you've called it like you stored it as a session variable.
    Just use:
    <?php echo $kt_welcome; ?>
    instead of the other echo tag.

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Nuh-uh, you seem to have missed:
    Code:
            $_SESSION['kt_welcome'] = $_POST['kt_welcome'];
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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

    Default

    Oh, ok. Right.

    Perhaps it's still relevant, though?
    Maybe they should both get set to the same variable so that's what is displayed? Are they intended to be the same thing?

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
  •