Help a newbie figure out how to get started.

Discussion in 'PHP' started by ScottDB, May 21, 2011.

  1. #1
    Hi all. I'm just starting to learn php and sql on my own before I go to college for it. I am understanding how to write the sql and php codes easily enough. I understand how to create a database and post queeries to it and have them show up in the queery results. What I am not seeing anywhere in any of the tutorials is how to get the queeries to show up on a page.

    example: If I use a simple php page with the opening and closing tags of php on it and insert the following code it should show up on the page in the browser correct?

    $sql = "SELECT * FROM memb_info LIMIT 0, 30 ";

    I cant get this to show on the page. Am I missing a step somewhere? I've also tried it with calling the database as well using
    use database; above that in the tags.
     
    ScottDB, May 21, 2011 IP
  2. shofstetter

    shofstetter Well-Known Member

    Messages:
    178
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    120
    #2
    shofstetter, May 22, 2011 IP
  3. niks00789

    niks00789 Well-Known Member

    Messages:
    188
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    110
    #3
    Actually you are missing a lot of things..

    I will give a fair idea of things u need to do to get the query display results for you :

    1. Make a mysql database
    2. Make an user
    3. Assign all privileges to that user for that particular database
    4. Open database and make a table in it
    5. Put some entries in the table

    Now comes the coding part
    You first need to get a connection to the database
    You can do it using the following code (Modify it according to your needs)
    mysql_connect("localhost", "your_username_here", "your_password") or die(mysql_error()); 
    PHP:
    Then select the database :
    mysql_select_db("your_database_name_here") or die(mysql_error());
    PHP:
    Now take the query you need to execute in a variable, for e.g. :

    $sql = "SELECT name,addr,phno FROM customer";
    PHP:
    Execute the query, and since it will produce some results, store it in a variable :

    $result = mysql_query($sql);
    PHP:
    Now, you have the results in the variable $result

    Now to print them using php use the following code

    
    
    while ($row = mysql_fetch_assoc($result)) 
    {
    	echo $row['name'] . " " . $row['addr'] . " " . $row['phno'] . "<br/>";
    }
    
    
    PHP:
    The above code will print the name, address and phone no. taken from your database for every row selected by executing the query earlier.

    Hope this gets you going... :)

    (And once you get started with it, start using google to get help. Its not that hard as it seems :D )
     
    niks00789, May 22, 2011 IP
  4. ScottDB

    ScottDB Greenhorn

    Messages:
    95
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #4
    Hey thanks for the code. Got it to work just fine. I also asked this same question in another forum and they gave me basicly the same code except that theirs was 3 times longer and didn't have the or die in the code.

    Now I just need to figure out how to put each variable returned into it's own html table on the page.

    Got a lot to learn but I enjoy it.
     
    ScottDB, May 22, 2011 IP
  5. 50inches

    50inches Greenhorn

    Messages:
    16
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    13
    #5
    Yup, there you got, phpvideotutorials was good for me when I started doing PHP.
     
    50inches, May 23, 2011 IP
  6. niks00789

    niks00789 Well-Known Member

    Messages:
    188
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    110
    #6
    Oh ok ok!

    i just tried to provide you things from which i had learnt ;)

    Glad that it was helpful :)

    And yep there's lots to learn, try making websites from innovative ideas, it helps you create new algorithms and hence search and learn new things when you get stuck at something. :)
     
    niks00789, May 26, 2011 IP