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); ?>
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); ?>
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 :|
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 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($result, 0, 'target'); $actual = mysql_result($result, 0, '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.
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()); }