• How to enter PHP code for accessing mySQL database tables

    I’m trying to connect to my Database table, retrieve the data, and display it on my web page (not a blog page). The problem is that I don’t know where to enter the php code to establish a connection with my database. Do I:
    enter the php code by editing the individual page?
    need to create a new php file (exaple: database_connect.php) and add it to my Templates via my host (hostgator) and ftp account (filezilla)?
    need to edit my stylesheet (css)? If so, where in the stylesheet should the php code need to be inserted (header, body, etc.)?

    I have the latest version of WordPress. Hostagator hosts my WordPress files, Filezilla is my ftp account, and I’m using “A Simple Love (a child theme of the “Thematic” theme)” as my theme.

    Note: I don’t have a problem executing a php test file that I created. The file is located in /public_html/test-php-files. Thus, I can enter the url as https://giftsrus.org/test-php-files/connectionfile.php and I get the result that says “If there are no errors in connecting to the database this is the only line you should see.:-)”

    The php code within the in the connectionfile.php is as follows and seems to work:
    <?php
    $dbc = mysql_connect (‘localhost’,’tank2010_bucky20′,’password<>’);

    if (!dbc) {
    die(‘Not Connected:’ . mysql_error ());
    }

    $db_selected = mysql_select_db (‘tank2010_test-csv’, $dbc);

    if (!$db_selected) {
    die (“Can’t Connect :” .mysql_error());
    }

    $query=”UPDATE game SET email=’I hope – this code works’ WHERE username=’bucky20′”;
    //add “result”
    $result=mysql_query($query);

    if (!$query) {
    die (“Can’t Connect :” .mysql_error());
    }

    echo “If there are no errors in connecting to the database this is the only line you should see.:-)”

    ?>

    So, I don’t know where to place this code to connect to my database automatically when the web site comes up. Do I go through ‘Appearance > Editor and edit the css stylesheet, template, or what.arance>

    Any help would be greatly appreciated.

Viewing 11 replies - 1 through 11 (of 11 total)
  • HI,

    Such code should be specifically in PHP files,so you should create a new php file and upload it to your website using an FTP client.

    Thread Starter cleop87

    (@cleop87)

    Thanks for the quick response.
    I can create the php file but don’t know where to upload that file and don’t know where the file should be located within my directory structure in order for the file to be actually executed.

    My steps are as follows:
    1. I log in to ‘Filezilla’ and can see my remote site (Hostgator) and the directory structure, folders, files, etc.

    2. I begin by looking at my remote site and scroll down to the “public_html” directory

    3. Within the “public_html” directory I created a file folder named “test-php-files.” In this folder I create php files to test my coding. In this case the file is named “connectionfile.php”. It is used to show that I have successfully connected to my database. It works, sort of, but I have to enter the complete URL to get that file to execute. An end-user wouldn’t know this therefor it would not work properly.

    Please note: Also within the “public_html” directory lies the “wp-content” folder which holds my theme, plugins, & upgrade folders as well as the “index.php” file.

    When you copy the complete url of the /connectionfile it is as follows:
    ftp://*****@giftsrus.org/public_html/test-php-filesconnectionfile.php (I replaced sensitive data with the ********).

    However, to test the connectionfile.php you can’t enter this url as it is above. You need to get rid of the:
    ftp://********@giftsrus.org/public_html within the url and change the url to:
    ? https://giftsrus.org/test-php-files/connectionfile.php

    The problem is that I don’t know where to put PHP files – in public_html (which clearly doesn’t work), wp-content, wp-admin, etc.

    The whole point of this is that I want to place products on my web site that shows the current prices and when the prices change (as shown in datafeeds), I want to have those new/current prices changed and displayed on my web page.

    Thanks again,
    Cleop87

    Thread Starter cleop87

    (@cleop87)

    Thanks for the quick response.
    I can create the php file but don’t know where to upload that file and don’t know where the file should be located within my directory structure in order for the file to be actually executed.

    My steps are as follows:
    1. I log in to ‘Filezilla’ and can see my remote site (Hostgator) and the directory structure, folders, files, etc.

    2. I begin by looking at my remote site and scroll down to the “public_html” directory

    3. Within the “public_html” directory I created a file folder named “test-php-files.” In this folder I create php files to test my coding. In this case the file is named “connectionfile.php”. It is used to show that I have successfully connected to my database. It works, sort of, but I have to enter the complete URL to get that file to execute. An end-user wouldn’t know this therefor it would not work properly.

    Please note: Also within the “public_html” directory lies the “wp-content” folder which holds my theme, plugins, & upgrade folders as well as the “index.php” file.

    When you copy the complete url of the /connectionfile it is as follows:
    ftp://*****@giftsrus.org/public_html/test-php-filesconnectionfile.php (I replaced sensitive data with the ********).

    However, to test the connectionfile.php you can’t enter this url as it is above. You need to get rid of the:
    ftp://********@giftsrus.org/public_html within the url and change the url to:
    ? https://giftsrus.org/test-php-files/connectionfile.php

    The problem is that I don’t know where to put PHP files – in public_html (which clearly doesn’t work), wp-content, wp-admin, etc.

    The whole point of this is that I want to place products on my web site that shows the current prices and when the prices change (as shown in datafeeds), I want to have those new/current prices changed and displayed on my web page.

    Thanks again,
    Cleop87

    If you upload the file directly in the public_html directory, then the file should be accessible via:

    https://yourwebsite.com/file.php

    If you upload it to different sub directories, then the file should be accessible via:

    https://yourwebsite.com/sub_directory/file.php

    Thread Starter cleop87

    (@cleop87)

    Thanks again so very much.
    I uploaded the php file (writeName.php) into the “public_html” directory and yes, I can now enter the url (giftsru.org/writeName.php) and it will display successfully. Yeah !

    So, when someone goes to my web page he/she will see products that show the image, “up-to-date” price of the product, description, etc — all of which are derived from the vendor’s daily datafeed. I upload the datafeeds to my database/SQL table via ftp (Hostgator).

    When the visitor visits my web site and clicks on the product it should take them to another page/url or a page within my site. I’m assuming they’re not going to be typing in “giftsrus.org/writeName.php.

    * I can create the PHP function but where do I store that function?

    * When I create a PHP script to connect to my database and my database table, where do I put that script; on the web page itself, within my style sheet, within my theme, or ???.

    * I know I need to connect to my database, and SELECT the data within my database table. Where do I enter the code to do so.

    I think we’re getting closer to the resolution and sincerely appreciate your help. I’ve read tons of stuff on this an have spent hours and hours searching but am clearly missing something.

    Thx again.
    Cleop87

    If you want the whole thing or procedure to be integrated with WordPress, then you would be better off if you do a plugin and have it integrated in WordPress like that.

    If you would like to have independent pages, then you can upload all files including the codes in public_html and then link to such files from a page or as a link from your WordPress.

    Thread Starter cleop87

    (@cleop87)

    Thx again. Pls pardon my confusion. I’m still struggling and I have installed the “exec-PHP” plugin for WordPress. It allows me to enter php code within the page content area. If there is another plugin you would recommend, please let me know.

    Now I can go to my web browser and enter the url (https://www.giftsrus.org/writeName.php) and I get “You have now connected to the writeName.php file SUCCESSFULLY!” (this is the test function that I created and it tells me that I have the php file executed properly within the page). However, I need to call that same function in multiple pages. So, where do I create that function (within the styleshee, create a new php template, or…..) and how do I call that function from other pages?

    Since the function will be used in multiple pages I’m trying to avoid having to write the same function in each and every page where it’s needed. Is this possible? Any ideas on how I can accomplish this would be greatly appreciated. I feel so close and yet so far away.

    This is the code for the “writeName.php file” and it is stored in the “public_html” directory along with the wp-content directory:
    <?php
    function writeName()
    {
    echo “SUCCESSFULLY!”;
    }
    echo “You have now connected to the writeName.php file “;
    writeName();
    ?>

    I feel so close and yet so far away.

    I hate doing this to you but you’ll thank me later. You need to learn how WordPress or any other php based platform works. And above all you need to learn to code in php. Then you need to learn how databases work and how to access information from databases within a language like php.

    You don’t need to make another connection to the same database, it’s already connected. (where do you think pages, posts and comments come from?)

    If it’s a separate database, you need to make a configuration file with your database credentials and include it on the pages that you need. the same goes for you functions.

    I upload the datafeeds to my database/SQL table via ftp (Hostgator).

    Huh? Seriously, either hire someone to do it right, or spend a few months learning the basic before taking on a job in this class.

    Thread Starter cleop87

    (@cleop87)

    I’ll go ahead and thank you now. Thx for your input and I sincerely appreciate it.

    Being a 100% disabled vet I have the time but not the money. I know how to create the code to connect to the database. I’ve created a php file to do just that and it works. I put my various php files, that I use for testing, in a folder within the public_html directory and named it “test-php-files.” The php connection file that I created to connect to the database is named “connectionfile.php.” If you go to your browser and enter the url (https://giftsrus.org/test-php-files/connectionfile.php) you will see the following text that I created to let me know that I have, in fact, connected to my database which is hosted by Hostgator:

    “If there are no errors in connecting to the database this is the only line you should see.:-)”

    My first question is where do I enter the php code for connecting to my database within my WordPress theme? My WordPress theme is “A Simple Love,” which is a child theme of the “Thematic” theme. I don’t know whether to enter the code into my individual page content (a separate divisioin within the html code for that page), my CSS stylesheet (or create another CSS stylesheet), create a new php file within “Appearance > Editor>Edit Theme, create a separate template and include it in the TEMPLATE php files on the far right column.

    I’m no expert but I do feel somewhat comfortable working with html and CSS. I’m simply trying to find out where I enter the PHP code/file to access my database without having to enter the URL as shown above.

    Again, thank you very much.

    Being a 100% disabled vet I have the time but not the money.

    If you got time, you don’t need any money as you can learn everything that a $200,000 college can teach you for free out of the public library and internet.

    I don’t know whether to enter the code into my individual page content (a separate divisioin within the html code for that page), my CSS stylesheet (or create another CSS stylesheet), create a new php file within “Appearance > Editor>Edit Theme, create a separate template and include it in the TEMPLATE php files on the far right column.

    As I said before, (not to discourage you), you need to learn the fundamentals of php before you begin, because what you are doing now is hanging on to something that seems to be working without knowing why it’s working that way.

    Php codes go in php files, and you can’t (in your case) mix them with other things. You can’t use a .css extension on a php script, the server won’t read it.

    CSS is a styling language; it’s not a programming language. HTML is a markup language; it only shows your output to the browser.

    To give you the answer you are looking for, you can put your php codes in your theme template files as they are php, and also your functions in the function.php of the theme. However, if you are opening connections without securing it, you are asking for trouble. If you are making configuration files with your database credentials which anyone can access, you are asking for trouble.

    Please do yourself a favor and at least watch a few YouTube clips on php for beginners and start from there.

    Another thing is that most people here won’t answer your technical questions regarding this matter since they are not directly WordPress related and many don’t even have a clue and the few that do don’t bother. Try stackoverflow.com which is a head and shoulder above this site when it comes to actual programming but be warned that they don’t like to read questions about obvious things.

    Best of luck to you and delete the link you posted as I can navigate through your backend with no trouble because of the issues that I explained above.

    Cheers,

    Chris

    Thread Starter cleop87

    (@cleop87)

    Thanks Chris.

    Per your suggestion the link has been removed. That in itself was a great help securety-wise.

    I do use the internet extensively and have viewed several you-tube tutorials on php. That’s how I created the connection file. Based upon your suggestions it seems that for me, the next step is to read and learn more on how to create the function, retrieve the data and populate my web page with the information. That’s what I was hoping to learn in more detail from this forum. I thought my question was simple but apparently it’s not.

    The exec-php plugin, which was suggested from another contributor on this forum, allows me to enter php code within the html content portion of the page that I’m editing. I just reached the point where I need to call the one function on various pages without having to enter the function code on each and every page. It seems I’ve perhaps gotten ahead of myself and will return to the php online manual to try to get an answer. If anyone else has had a similar need, and found the solution, I would sincerely appreciate any suggestions.

    Kindest regards,
    Cleop87

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘How to enter PHP code for accessing mySQL database tables’ is closed to new replies.