Extracting data from database?

Discussion in 'PHP' started by scm22ri, May 16, 2012.

  1. #1
    Hi Everyone,


    This is a step by step process of what I did. My goal is to extract data from my database and display that information on my website. I'm running into some errors on how I should do that. Perhaps someone can help me out. All opinions welcome! Thanks!


    >>>>>>>>>>>>


    The database


    I made 5 Colums in phpMyAdmin


    column name
    user_id (INT) Primary <--Auto Increment
    first_name (VARCHAR) length 32
    last_name (VARCHAR) length 32
    address (VARCHAR) length 32
    address (VARCHAR) length 32


    I then pressed "save"


    I then inserted data into my phpmyadmin database.
    (below is my data)


    user_id first_name last_name address city
    1 Bob Jackson 123 I Hope New York
    2 Robert Downney 456 I'm Doing Miami
    3 Ken Sorenson 789 This Right Las Vegas


    >>>>>>>>>>>>>


    I then made a folder on my website called "practice"


    In that practice folder I also made another folder called "database"


    In the database folder I made a page called connect.php


    In the connect.php I made a connection to my database.


    connect.php connection code code
    <?php
    $connect_error = 'Sorry we\'re experiencing connection issues';
    mysql_connect("localhost","myname","mypassword") or die($connect_error);
    mysql_select_db('mydatabase') or die($connect_error);
    ?>


    http://whatsmyowncarworth.com/practice/database/connect.php
    http://whatsmyowncarworth.com/practice/init.php
    http://whatsmyowncarworth.com/practice/index.php


    >>>>>>>>>>>>>>>>>>>>


    In the practice folder I made two pages. Those two pages are called init.php and index.php
    (the practice folder is acting as my root folder)


    (I tried using this code -> <?php include 'practice/init.php';?> <- on the index.php but kept getting errors. So I switched to the below code.)


    index.php code
    <?php
    require 'database/connect.php';
    echo 'Hello World!';
    ?>


    >>>>>>>>>>>>>>>>>>>>


    init.php code
    <?php
    require 'database/connect.php';
    ?>


    >>>>>>>>>>>>>>>>>>>>


    I'm assuming up until now everything is going well. Now my major concern is to be able to "pull" or "get" (my apol. for lack of terminology) data from the MySql database.


    This is where the confusion is starting to come into play. I'm going to give the below code a try on index.php and see if it works.


    My index.php page code now.


    How come this didn't work? What am I doing wrong?


    <?php
    include 'practice/init.php';


    // get the user ID from the URL
    // it is a numerical ID, so cast to an integer
    $user_id = (int) $_GET['user_id'];


    // try to get the user from the database
    $result = $mysql->query("SELECT * FROM info WHERE id='$user_id'");


    // did we find a user?
    if ($result->num_rows == 1) {
    $row = $result->fetch_assoc();

    echo 'Name: ' . $row['first_name'] . '<br />
    Email: ' . $row['city'];
    } else {
    echo 'Sorry, that user was not found';
    }


    ?>
     
    scm22ri, May 16, 2012 IP
  2. HostPlanz

    HostPlanz Well-Known Member

    Messages:
    449
    Likes Received:
    34
    Best Answers:
    4
    Trophy Points:
    130
    #2
    I didn't read everything, but:

    include 'practice/init.php';
    PHP:
    is suppose to be

    include('init.php');
    PHP:

    "init.php" and "index.php" are in the same directory.
    What you were doing is trying to include this file, which doesn't exist: http://whatsmyowncarworth.com/practice/practice/init.php

    The "index.php" is already in the practice directory, so no need to tell it to go there to look for "init.php". All you gotta do is include it.
     
    HostPlanz, May 16, 2012 IP
  3. scm22ri

    scm22ri Greenhorn

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    6
    #3
    Hi Hostplanz,

    Appreciate the tip! Thanks, it's working.

    Now my main hurtle is trying to convert the data that's in the MySql into database into variables. So I can use those variables on my website. Would anyone know how to do that or perhaps give me any pointer? Thanks again!
     
    scm22ri, May 16, 2012 IP
  4. HostPlanz

    HostPlanz Well-Known Member

    Messages:
    449
    Likes Received:
    34
    Best Answers:
    4
    Trophy Points:
    130
    #4
    Don't understand what you mean by converting the data into variables, but if you mean you're trying to store the mysql data in variables, you already did that in the following code:

    if ($result->num_rows == 1) {
    $row = $result->fetch_assoc();
    
    echo 'Name: ' . $row['first_name'] . '<br />
    Email: ' . $row['city'];
    } else {
    echo 'Sorry, that user was not found';
    }
    
    PHP:
    $row['first_name'] & $row['city'] are variables (really arrays)
     
    HostPlanz, May 17, 2012 IP
  5. thepresence

    thepresence Greenhorn

    Messages:
    42
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #5
    What HostPlanz says above is true. But, if you want the variable to look like $firstname, then you could say $firstname=

    $row['first_name'];

    Then, when you use it in php, you could say, " echo $firstname, Good morning" ... or something like that.
     
    thepresence, May 17, 2012 IP