Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,559
    Thanks
    76
    Thanked 105 Times in 104 Posts

    Query from database not working

    This tiny simple query shouldn't take me an hour to figure out.

    PHP Code:
    $UserStats mysqli_query("SELECT * FROM rod_CharName WHERE username='".$userrow['username']."'");
     
    $usrow mysqli_fetch_array($UserStats);
     echo 
    "Welcome ".$usrow['username'].""
    if I do

    PHP Code:
    echo "Welcome ".$userrow['username'].""
    It works fine, but $usrow will not put any data. I don't understand why...
    By the way $userrow is their main account that is logged in. The rod_CharName table has all their different characters in it, each one under their same username.

    I am thinking it needs an sql JOIN between tables? But never done one of those.
    Last edited by myfayt; 05-01-2014 at 04:05 AM.
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #2
    Regular Coder
    Join Date
    Dec 2011
    Posts
    108
    Thanks
    5
    Thanked 17 Times in 17 Posts
    Quote Originally Posted by myfayt View Post
    This tiny simple query shouldn't take me an hour to figure out.

    PHP Code:
    $UserStats mysqli_query("SELECT * FROM rod_CharName WHERE username='".$userrow['username']."'");
     
    $usrow mysqli_fetch_array($UserStats);
     echo 
    "Welcome ".$usrow['username'].""
    if I do

    PHP Code:
    echo "Welcome ".$userrow['username'].""
    It works fine, but $usrow will not put any data. I don't understand why...
    $usrow && $userrow are two different variables and just because $userrow echo's out OK doesn't really show us anything.
    By the way $userrow is their main account that is logged in. The rod_CharName table has all their different characters in it, each one under their same username.
    If you want people to know how it is setup up then just list the table names and type example `id` int(11), `username` varchar(30), etc...
    I am thinking it needs an sql JOIN between tables? But never done one of those. I doubt it

    An finally a good place to get the correct mysqli code syntax is PHP: mysqli:repare - Manual
    Last edited by Strider64; 05-01-2014 at 01:23 PM.
    True courage is about knowing not when to take a life, but when to spare one. PDO Tutorial

  • #3
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,559
    Thanks
    76
    Thanked 105 Times in 104 Posts
    PHP Code:
    //Table1 AllUsers has the following:

     
    `idmediumint(7NOT NULL AUTO_INCREMENT COMMENT 'ID of Main Account',
      `
    usernamevarchar(30NOT NULL COMMENT 'Username of Main Account',
      `
    passwordvarchar(32NOT NULL COMMENT 'Password of Main Account',
      `
    emailvarchar(80NOT NULL COMMENT 'Email of Main Account',
      `
    verifytinyint(1NOT NULL DEFAULT '0' COMMENT 'If Account has been verified',
      `
    verifycodevarchar(8NOT NULL,
      `
    regdatedate NOT NULL,
      `
    onlinetimedatetime NOT NULL DEFAULT '0000-00-00 00:00:00',
      `
    charcounttinyint(1NOT NULL DEFAULT '1' COMMENT 'Number of Characters Created',
      `
    fullnamevarchar(60NOT NULL,
      `
    addresstext NOT NULL,
      `
    authleveltinyint(1unsigned NOT NULL DEFAULT '1' COMMENT 'RoD Staff or Not',
      `
    birthdaydate NOT NULL DEFAULT '2003-12-31' COMMENT 'Players Birthday',
      `
    infractiontinyint(2NOT NULL DEFAULT '0' COMMENT 'Warnings before banned',
      `
    bandayssmallint(4NOT NULL DEFAULT '0' COMMENT 'Number of days banned',
      
    PRIMARY KEY (`id`)
    ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=41 
    PHP Code:
    //Table 2 is CharName:

     
    `rod_CharName` (
      `
    usernamevarchar(30NOT NULL,
      `
    charnamevarchar(20NOT NULL,
      `
    authleveltinyint(1NOT NULL DEFAULT '1',
      `
    strengthsmallint(6NOT NULL DEFAULT '1',
      `
    attackint(6NOT NULL DEFAULT '1',
      `
    defenseint(6NOT NULL DEFAULT '1',
      `
    dexterityint(6NOT NULL DEFAULT '1',
      `
    classvarchar(11NOT NULL,
      `
    realmtinyint(3NOT NULL,
      `
    latitudesmallint(6NOT NULL DEFAULT '0',
      `
    longitudesmallint(6NOT NULL DEFAULT '0',
      `
    locationvarchar(30NOT NULL,
      `
    levelsmallint(3unsigned NOT NULL DEFAULT '1',
      `
    currenthpsmallint(5unsigned NOT NULL DEFAULT '10'
    ENGINE=MyISAM DEFAULT CHARSET=latin1
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #4
    New Coder
    Join Date
    Apr 2014
    Posts
    17
    Thanks
    0
    Thanked 1 Time in 1 Post
    Possibly a dumb question but are both tables in the same database?

  • #5
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,559
    Thanks
    76
    Thanked 105 Times in 104 Posts
    Yes both are in the same database. So basically when you login with your username, I want to take that username and match it with the username in the CharName table to extract the data. But it isn't working. I can pull it from the AllUsers table just fine.
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #6
    Regular Coder Linux_Sage's Avatar
    Join Date
    Mar 2014
    Location
    Sterling,VA
    Posts
    106
    Thanks
    0
    Thanked 10 Times in 10 Posts
    Since the query works, where is $userrow being assigned? Does it have any data or are you using it anywhere else and know it is populated?

  • #7
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,559
    Thanks
    76
    Thanked 105 Times in 104 Posts
    $userrow is a global variable that can be used anywhere on any page. It pulls the session from the users id from the table.
    So it will always work no problems.

    But it's not letting me check the current logged in user's username with the same from a different table.
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #8
    Regular Coder Linux_Sage's Avatar
    Join Date
    Mar 2014
    Location
    Sterling,VA
    Posts
    106
    Thanks
    0
    Thanked 10 Times in 10 Posts
    Just noticed your function call again. Take a look at this: PHP: mysqli::query - Manual

    Looks like you'll need to pass in a variable containing the result of mysqli_connect since you're using the procedural style. That may be your problem.

  • #9
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,559
    Thanks
    76
    Thanked 105 Times in 104 Posts
    It's working with this coding, however I'd like to make it global so that I don't have to include these settings on every page but rather just include the file and call it with a variable. I feel this way is a security risk to have on every page. Any possible way for that? Thanks

    PHP Code:
    $link mysqli_connect("localhost""bbbb""cccc""dddd");
      
    $UserStats mysqli_query($link"SELECT * FROM rod_CharName WHERE username='{$userrow['username']}'");

     
    $usrow mysqli_fetch_array($UserStats);
     echo 
    "Welcome ".$usrow['username'].""
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #10
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,548
    Thanks
    8
    Thanked 1,094 Times in 1,085 Posts
    Save username in a SESSION variable

  • #11
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,474
    Thanks
    63
    Thanked 537 Times in 524 Posts
    Quote Originally Posted by mlseim View Post
    Save username in a SESSION variable
    What on earth does that have to do with the $link parameter of mysqli_query() or wanting to put the mysqli_connect() call into a seperate file?

    Honestly I do have to wonder sometimes if you're just poking at shadows or just enjoy taking people on a long ride around the sun to reach the moon because saving the username in the session has absolutely nothing to do with the query call.
    I can't really think of anything to write here now...

  • #12
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,548
    Thanks
    8
    Thanked 1,094 Times in 1,085 Posts
    Sorry, I guess I thought they wanted to save the username to be used on other web pages without doing a query each time. Must have misread it. My bad.

  • #13
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,559
    Thanks
    76
    Thanked 105 Times in 104 Posts
    Any suggestions on not using the

    PHP Code:
    $link mysqli_connect("localhost""bbbb""cccc""dddd"); 
    on every page but to put it in one file and include it everywhere? Thanks
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #14
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,474
    Thanks
    63
    Thanked 537 Times in 524 Posts
    Yes, just put it in a file called db.php and then include('db.php');

    Obviously the files that will use db.php need to be in the same directory or you will need to specify a file path.
    I can't really think of anything to write here now...

  • Users who have thanked tangoforce for this post:

    myfayt (05-03-2014)

  • #15
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,559
    Thanks
    76
    Thanked 105 Times in 104 Posts
    Whoops, I did that but forgot to back out of the directory with ../ so now it works. Thanks
    Been a sign maker for 7 years. My business:
    American Made Signs


  •  

    Posting Permissions

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