Results 1 to 2 of 2

Thread: mysql my php

  1. #1
    Join Date
    Jul 2009
    Beans
    13

    Question mysql my php

    I've just started getting my head around MYSQL databases and using PHP to model the database. I was wondering what is the best method of selecting information from the database.

    PHP Code:
    <?php
    %con mysql_connect("localhost","george","password");
    if (!
    $con)
    {
    die (
    'Could not connect: ' mysql_error());
     
    mysql_select_db("wp_content"$con);
    $return mysql_query("SELECT * FROM wp_content");
     
    while (
    $row mysql_fetch_array($return))
    {
    echo 
    $row['intro'];
    }
     
    mysql_close($con);
    ?>

  2. #2
    Join Date
    Jun 2009
    Beans
    55
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: mysql my php

    Well you pretty much got it, its just a matter of understanding what it is you're doing. I create a function for connecting to the database and selecting the table which you can put on your page, or in a function library.
    PHP Code:
    function connect_to_database$host$user$password$dbname )
        {
            
    // Try to connect to database
            
    $dbconnection mysql_connect$host$user$password );
            if ( ! 
    $dbconnection )
            {
                die();
            }
            
    // Try to select database
            
    $dbselection mysql_select_db$dbname );
            if ( ! 
    $dbselection )
            {
                die();
            }
            return 
    $dbconnection;
        } 
    Above, before all the die functions feel free to redirect to an error page or whatever, its not too hard, figure you can get it yourself.

    PHP Code:
    $dbconnection connect_to_database'localhost''george''password''wp_content');

    $retrieve_sql "SELECT * FROM wp_content"
        
    $dbretrieve_result mysql_query$retrieve_sql );
        if ( ! 
    $dbretrieve_result )
        {
            die();
        }
        if ( 
    mysql_num_rows($dbretrieve_result) != 
        {
            while ( 
    $row mysql_fetch_assoc($dbretrieve_result) ) 
            {
                echo 
    "<p>{$row['intro']}</p>";
            }
        }
        
    mysql_free_result$dbretrieve_result );
        
    mysql_close$dbconnection ); 
    As I said pretty much what you had, just utilising my function, and also a few error preventatives.

    PHP Code:
    if ( ! $dbretrieve_result )
        {
            die();
        } 
    If for some reason it can't connect, it exits the operation.

    PHP Code:
    if ( mysql_num_rows($dbretrieve_result) != 
    If the table exists, but has no actual information in it, i.e an empty set. This prevents it displaying nothing, and being happy about it : )

    PHP Code:
    mysql_free_result$dbretrieve_result );
        
    mysql_close$dbconnection ); 
    Just as a matter of hygiene, its nice to clean up. The first command just clears the variable of any value, and the last one closes the connection succesfully.
    If theres any specifics feel free to ask. : )
    Last edited by CrazyMonkeyFox; July 8th, 2009 at 04:02 PM.


Tags for this Thread

Bookmarks

Posting Permissions

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