1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Development PHP/SQL Help

Discussion in 'Software' started by :: Phat ::, 27 Jul 2006.

  1. :: Phat ::

    :: Phat :: Oooh shakalaka!

    Joined:
    7 Jun 2002
    Posts:
    4,886
    Likes Received:
    3
    Right, you're talking to a total newbie here I'm afraid!!!

    I've created a table with three fields...

    ID
    Target
    Actual

    What I want to be able to do, is pull information from the table (just use ID 1 for now) and I want the number in target to be defined as $target and the number in actual to be defined as $actual
    so that I can use it a little later on.

    I already have the "Connections" page as set up by Dreamweaver -

    PHP:
    <?php
    # FileName="Connection_php_mysql.htm"
    # Type="MYSQL"
    # HTTP="true"
    $hostname_localhost "localhost";
    $database_localhost "xxxx";
    $username_localhost "xxxx";
    $password_localhost "xxxx";
    $localhost mysql_pconnect($hostname_localhost$username_localhost$password_localhost) or trigger_error(mysql_error(),E_USER_ERROR); 
    ?>
     
  2. eek

    eek CAMRA ***.

    Joined:
    23 Jan 2002
    Posts:
    1,600
    Likes Received:
    14
    Looks, ok so far except that I would not chose to use mysql_pconnect, but instead just mysql_connect. The difference is that the _pconnect one creates a persistent connection to the database which is something you probably don't want to do. If you do, ignore me and leave it as it is!!

    PHP:
    <?php

    // vars...
    $hostname_localhost "localhost";
    $database_localhost "xxxx";
    $username_localhost "xxxx";
    $password_localhost "xxxx"

    // create database connection, else die
    $sql mysql_connect($hostname_localhost$username_localhost$password_localhost);
    if (!
    $link) {
        die(
    'Could not connect: '.mysql_error());
    }

    // select database
    if (!mysql_select_db($database_localhost)) {
        echo 
    "Unable to select ".$database_localhost.": ".mysql_error();
        exit;
    }

    // get tuple with id=1 from database
    $result mysql_query("select target, actual from table where id = 1");

    // if a result is returned, then save into $target and $actual
    while ($row mysql_fetch_object($result)) {
       
    $target $row->target;
       
    $actual $row->actual;
    }

    // free database connections
    mysql_free_result($result);
    mysql_close($sql);
    ?>
     
  3. :: Phat ::

    :: Phat :: Oooh shakalaka!

    Joined:
    7 Jun 2002
    Posts:
    4,886
    Likes Received:
    3
    Not working, I just get "Could Not Connect"

    :(
     
  4. RTT

    RTT #parp

    Joined:
    12 Mar 2001
    Posts:
    14,120
    Likes Received:
    74
    Check the username/password and that the user has permission to connect from the host.

    edit: there's a tiny error in eek's script that's causing your problem (but still check the user/pass/permissions anyway)

    PHP:
    $sql mysql_connect($hostname_localhost$username_localhost$password_localhost);
    if (!
    $link) {
        die(
    'Could not connect: '.mysql_error());

    should be

    PHP:
    $link mysql_connect($hostname_localhost$username_localhost$password_localhost);
    if (!
    $link) {
        die(
    'Could not connect: '.mysql_error());
    goddamn using the mysql_* functions verbatim makes me cringe almost :|
     
  5. Firehed

    Firehed Why not? I own a domain to match.

    Joined:
    15 Feb 2004
    Posts:
    12,574
    Likes Received:
    16
    Change the xxxx's back to what the actual login info is. Or don't do it through dreamweaver, I found that it had really crappy database interfacing when I tried it. And, of course, make sure you've actually got mysql running at the time. I confused myself quite a bit when my server wasn't going... forgot I'd rebooted and forgotten to start it back up :hehe:

    PHP:
    <?
    /* DB connection into */
    $db_host "localhost";
    $db_user "YOUR USERNAME HERE";
    $db_pass "YOUR PASSWORD HERE";
    $db_name "YOUR DB NAME";

    mysql_connect($db_host$db_user$db_pass);
    @
    mysql_select_db($db_name) or die('Connection failed.'); // @ suppresses any other error messages

    $query "SELECT * FROM tablename WHERE id = 1"//replace tablename as needed, and you can change 1 to $id if it's defined elsewhere if you like

    $result mysql_query($query) or die('Bad query.');

    // Two methods you can use here....

    $row mysql_fetch_assoc($result);
    //this puts the data in an array, $row['name'], $row['id'], $row['target'], etc

    $target mysql_result($result0'target');
    $actual mysql_result($result0'actual');
    // those directly set the variables.  Use only one of the two methods.


    ... rest of code ...


    mysql_close();

    ?>
    Both of my methods are best used in a while loop, given any query where you'll have multiple results. Which better not be the case for 'WHERE id=1'. For the latter two, I do something like
    PHP:
    <? $numrows mysql_numrows($result);
    $i=0;
    while (
    $i $numrows)
    {
    whatever you want;
    $tableheader mysql_result($result,$i,'table header'); //use $i instead of 0 to return the value for the row you're working on
    $i++;
    }
    ?>
    The former just requires while ($row = mysql_fetch_assoc($result)) {code;} - it'll cycle through each thing automatically.
     
    Last edited: 27 Jul 2006
  6. eek

    eek CAMRA ***.

    Joined:
    23 Jan 2002
    Posts:
    1,600
    Likes Received:
    14
    Damn my cutting and pasting from php.net!!! ;)

    better off changing from $link to $sql as I use $sql in the rest of the code
    PHP:
    $sql mysql_connect($hostname_localhost$username_localhost$password_localhost);
    if (!
    $sql) {
        die(
    'Could not connect: '.mysql_error());
     

Share This Page