Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8364509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:26:49+00:00 2026-06-09T12:26:49+00:00

I’ve been trying this for hours, but haven’t had any luck. Every tutorial I

  • 0

I’ve been trying this for hours, but haven’t had any luck. Every tutorial I find online gives a different method to connect to a MySQL database using PHP, so its hard to find consistency.

When I run the following code, I get “0” for the values I attempt to retrieve. How can I get the values? I have the following code so far:

<?php 
        //Connect to MySQL server
        $conn = mysql_connect('XXIP ADDRESSXX', 'XXUSERNAMEXX', 'XXPASSWORDXX') or die(mysql_error()); //Connect to mysql

        //Continue if connection successful
        if($conn != null) {
            $db_selected = mysql_select_db('itemdata', $conn) or die(mysql_error()); //Connect to database using the connection

            //Continue if selected database successfully
            if($db_selected == true) {
                //The statement used to query the database  
                $query = "SELECT * FROM item_prices";

                //Store the results of the query
                $result = mysql_query($query, $conn) or die(mysql_error()); //Retrieve all data from the table  

                //Convert to assoc array
                $values = mysql_fetch_assoc($result);

                $cost = $values['cost'];
                $retail = $values['retail'];
                $in_stock = $values['available'];

                                    echo("<p>$cost</p>");
                                    echo("<p>$retail</p>");
                                    echo("<p>$in_stock</p>");
            }
            else 
                echo "Unable to connect to database";
        }
        else
            echo "Unable to connect to MySQL Server";

        //Close database
        mysql_close($conn);
    ?>

Now for some further information: With debugging, I verified that the code reaches the inner most “if” statement, so It properly connects and selects the database. None of the errors get thrown since I just receive “0” for the 3 variables I attempt to retrieve. (I tried printing right after to verify it wasn’t an issue with variable scope).

Any help would be great, I’ve never coded a connection to MySQL and i’m having trouble finding the best method (I found tutorials on mysqli, mysql, object oriented, etc… and none are complete (I looked at the user manual also)..

Thanks.

EDIT: The structure of the database is as follows (so far)

database name: itemdata

table name: item_prices

The table has 4 columns:

  • itemID (primary key) – this is what is searched for (type int)
  • cost (type float)
  • retail (type float)
  • instock (type boolean)

I’m trying to retrieve all the values in the table using itemID

FINAL EDIT
I copied the table from PHPMyAdmin (this is all for test purposes, so the data is bs)

itemID  cost    retail  instock
0   0   0   0
1   100 150 1
2   200 250 1
3   300 350 0
4   400 450 0
5   500 550 1
10  100 150 1
11  200 250 1
12  300 350 0
13  400 450 0
14  500 550 1
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T12:26:50+00:00Added an answer on June 9, 2026 at 12:26 pm

    I’ve taken a little look at your code and I am pretty sure (like 95%) that this is working code. If it is still not working you have done something else wrong.

    By the way, you don’t need to echo an error-message when you use die(), the script will stop working from that point.

    Here’s my prettified code:

    <?php 
    //Connect to MySQL server
    $conn = mysql_connect('XXIP ADDRESSXX', 'XXUSERNAMEXX', 'XXPASSWORDXX')
        or die('Unable to connect to MySQL Server');
    
    
    // Select db
    mysql_select_db('itemdata', $conn)
        or die('Unable to connect to database');
    
    // Run the query and fetch the results
    $query = "SELECT cost FROM item_prices WHERE itemID = 4";
    $values = mysql_fetch_assoc(mysql_query($query));
    
    // Echo everything
    echo '<p>'.$values['cost'].'</p>';
    echo '<p>'.$values['retail'].'</p>';
    echo '<p>'.$values['available'].'</p>';
    
    // Close the connection
    mysql_close($conn);
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Seemingly simple, but I cannot find anything relevant on the web. What is the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.