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 13 of 13
  1. #1
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help with GET Variable

    I'm extrememly new to php in general, so I realize some of this may seem "dumb", but please bear with me.

    I have a page that's integrated with phpBB forums, so it uses their templating system, so I have no need to echo any html within a php page. Also, every page includes a connection to the database, so I have no need to put any connections.

    On the html page, I'm using bits like {NAME} and {AGE} to pull up the information, but my problem lies with the php file itself. I want the page's url to have something like "file.php?name=John", where it will display the data within the row for "John" from the database.

    Here is the code I'm currently using. Assuming most of this isn't horribly wrong to begin with, I'm getting an error "Parse error: syntax error, unexpected T_STRING" on what is line 9 of this code.

    Code:
    <?php
    include "includes/phpbb.php";
    
    $template->set_filenames(array(
    	'body'	=> 'person.html',
    ));
    
    $name = $_GET['name'];
    $result = mysql_query(SELECT * FROM 'people' WHERE 'name' = $name);
    while($row = $db->sql_fetchrow($result))
    {
    $template->assign_vars(array(
    'NAME'          => $row['name'],
    'AGE'           => $row['age'],
        ));
    }
    
    $site->page_footer();
    
    ?>

  • #2
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    738
    Thanks
    20
    Thanked 85 Times in 85 Posts
    the old single quotes instead of backsticks problem

    change
    PHP Code:
    $result mysql_query(SELECT FROM 'people' WHERE 'name' $name); 
    to
    PHP Code:
    $result mysql_query(SELECT FROM `peopleWHERE `name` = $name); 
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #3
    Regular Coder
    Join Date
    Mar 2010
    Posts
    201
    Thanks
    79
    Thanked 5 Times in 5 Posts
    Your basic structure to integrate a custom page to phpbb is not correct.
    I would suggest you to read the PHPBB wiki.
    Which explains in depth how to build a website in phpbb.

    https://wiki.phpbb.com/

    The correct way to fix your example would be:
    PHP Code:
    <?php
    /**
    *
    *
    * @package {PACKAGENAME}
    * @version $Id$
    * @copyright (c) 2007 Your Group Name
    * @license http://opensource.org/licenses/gpl-license.php GNU Public License
    *
    */

    /**
    * @ignore
    */
    define('IN_PHPBB'true);
    $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH './';
    $phpEx substr(strrchr(__FILE__'.'), 1);
    include(
    $phpbb_root_path 'common.' $phpEx);

    // Start session management
    $user->session_begin();
    $auth->acl($user->data);
    $user->setup('mods/custom'); // language file

    // request_var –– Get passed variables from $_GET, $_POST, or $_COOKIE
    $name request_var('name'''); // '' = cast to empty string

    define('PEOPLE_TABLE''people');

    // Make sure to use sql_escape on strings
    $sql 'SELECT *
        FROM ' 
    PEOPLE_TABLE "
        WHERE name = '" 
    $db->sql_escape(utf8_clean_string($name)) . "'";
    $result $db->sql_query($sql);
    $row $db->sql_fetchrow($result))

    $template->assign_vars(array(
            
    'NAME'          => $row['name'],
            
    'AGE'           => $row['age'],
        ));

    $db->sql_freeresult($result);

    // Page title, this language variable should be defined in the language file you setup at the top of this page.
    page_header($user->lang['MY_TITLE']);

    // Set the filename of the template you want to use for this file.
    // This is the name of our template file located in /styles/<style>/templates/.
    $template->set_filenames(array(
        
    'body' => 'person.html',
    ));

    // Completing the script and displaying the page.
    page_footer();
    ?>
    Last edited by conware; 07-11-2013 at 02:04 PM.

  • #4
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Well not to mention the entire query needs to be quoted as it is a string

    HTH
    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,474
    Thanks
    63
    Thanked 537 Times in 524 Posts
    Quote Originally Posted by Arcticwarrio View Post
    the old single quotes instead of backsticks problem

    change
    PHP Code:
    $result mysql_query(SELECT FROM 'people' WHERE 'name' $name); 
    to
    PHP Code:
    $result mysql_query(SELECT FROM `peopleWHERE `name` = $name); 
    The old Arcticwarrio misses the actual problem again problem

    PHP Code:
    $result mysql_query("SELECT * FROM `people` WHERE 'name' = $name"); 
    Notice anything?

    FYI you CAN actually use single quotes in SQL for columns. They don't work terribly well and sometimes they do screw it up but they can be used (or at leas you could in mysql 4)
    I can't really think of anything to write here now...

  • Users who have thanked tangoforce for this post:

    Arcticwarrio (07-12-2013)

  • #6
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by conware View Post
    Your basic structure to integrate a custom page to phpbb is not correct.
    I would suggest you to read the PHPBB wiki.
    Which explains in depth how to build a website in phpbb.

    https://wiki.phpbb.com/
    My entire site is already completely integrated just fine with phpbb; all user sessions, forum variables, and so on work, so none of that is the issue. My only problem is the query I'm using for that specific page only.

    I'll try the suggestions posted in this thread and get back to you guys, thanks.

  • #7
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Use the code posted by tango force, the part inside mysql_query() needs to be quoted, as it is a string

    HTH
    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #8
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I tried the line by tango force, and I get the page showing up, but now the top has

    [phpBB Debug] PHP Warning: in file (my file) on line 9: mysql_query() [function.mysql-query]: Access denied for user (my user) (using password: NO)
    [phpBB Debug] PHP Warning: in file (my file) on line 9: mysql_query() [function.mysql-query]: A link to the server could not be established
    Line 9, being the $result line.

    I have other queries that are displaying information (such as "5 most recent ____") just fine, and user sessions show up fine, so this problem connecting to the database is ONLY on this page, and is not an already-existing problem.

    All the html/css in the page works fine, and the templates and css files within the php file are being called properly, so it's only this specific query.

  • #9
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    I read through conware's post again, he's right..since you're working with PHPBB, you have to use their system, you can't just use mysql_query, since the DB connection is through a class of theirs.

    replace $result = mysql_query("SELECT * FROM `people` WHERE 'name' = $name");

    with $result = $db->sql_query("SELECT * FROM `people` WHERE 'name' = $name");

    See how you are using a method of the PHPBB class instead of the native mysql_query() function?

    Edit: Since we're on the subject, the mysql_extension is deprecated as of PHP 5.5 and will be removed as of PHP 6. Developers should use the mysqli_ extension or PHP Data Objects instead


    HTH
    Dan
    Last edited by whizard; 07-12-2013 at 02:08 PM.
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #10
    Regular Coder
    Join Date
    Mar 2010
    Posts
    201
    Thanks
    79
    Thanked 5 Times in 5 Posts
    The DB connection is made in the following file:
    PHP Code:
    include($phpbb_root_path 'common.' $phpEx); 
    So this means you have to include this file at the top of each page that is going to use the $db class for queries.

    Else if you don't want to use that you could make your own connection.
    By doing this:

    PHP Code:
    // this file stores the database variables
    include($phpbb_root_path 'config.' $phpEx);

    // the engine you want to use.
    // you wanted to use mysql so its best to select the mysqli class
    include($phpbb_root_path 'includes/db/mysqli.' $phpEx);

    // new instance of db
    $db = new $sql_db();

    // connect to database
    $db->sql_connect($dbhost$dbuser$dbpasswd$dbname$dbportfalsefalse);

    // unset for safety purposes
    unset($dbpasswd); 
    Then save this in a seperate file so you can just include it when you need the connection to do queries.

    Also I noticed your calling your custom $site->page_footer();
    Note sins your not using the phpbb footer.
    You'll have to close the database connection in your custom footer script.

    in this->page_footer function
    PHP Code:

        
    global $cache$db;

        
    // Unload cache, must be done before the DB connection if closed
        
    if (!empty($cache))
        {
            
    $cache->unload();
        }

        
    // Close our DB connection.
        
    if (!empty($db))
        {
            
    $db->sql_close();
        } 
    This is all possible. But my first example with the correct structure is the best way to go. Sins it gives you all phpbb functions, sessions, cache options and tons more. And believe me when I say this its alot easier to use the functions already there then reinventing everything.

  • #11
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by whizard View Post
    I read through conware's post again, he's right..since you're working with PHPBB, you have to use their system, you can't just use mysql_query, since the DB connection is through a class of theirs.

    replace $result = mysql_query("SELECT * FROM `people` WHERE 'name' = $name");

    with $result = $db->sql_query("SELECT * FROM `people` WHERE 'name' = $name");

    See how you are using a method of the PHPBB class instead of the native mysql_query() function?

    Edit: Since we're on the subject, the mysql_extension is deprecated as of PHP 5.5 and will be removed as of PHP 6. Developers should use the mysqli_ extension or PHP Data Objects instead


    HTH
    Dan

    This allows the page to load correctly, however, there's still a problem.

    I have the table with the columns "name" and "age". In that table, two entries are there under "Walt" and "Jesse".

    If I put "/people.php?name=Walt", I get an error,

    General Error
    SQL ERROR [ mysqli ]

    Unknown column 'Jesse' in 'where clause' [1054]

    SQL

    SELECT * FROM `people` WHERE 'name' = Jesse
    It looks like it wants me to put the COLUMN name, instead of the row's name. Sure enough, if I put either "name" or "age" in place of the "Walt" part, the page shows up fine with no errors in the header. So what part of the code is wrong now?

  • #12
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Well I didn't really notice this before but don't use quotes on the table names. Leave them alone or wrap them in backticks ( ` )

    HTH
    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #13
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    They were already in backticks, but I removed them anyway and get the exact same thing.


  •  

    Posting Permissions

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