Hello guys i bought this book PHP 5 and FLASH i did everthing in the book the same way i keep getting this error that it can't connect i have looked all over and found a code to test out my conection and i did it perfectly can somone look at this code and tell me what i am doing wrong please i am very new at this all i want to do is a conection to add user here is the php first:
<?php
// include the Database classes
require_once('classes/database.php');
// escape quotes and apostrophes if magic_quotes_gpc off
if (!get_magic_quotes_gpc()) {
foreach($POST as $key=>$value) {
$temp = addslashes($value);
$POST[$key] = $temp;
}
}
// create a Database instance and check username
$db = new Database('localhost',"tom","greg","phpflash");
$sql = 'SELECT username FROM users WHERE username = "'.$_POST['username'].'"';
$result = $db->query($sql);
$numrows = $result->num_rows;
// if username already in use, send back error message
if ($numrows > 0) {
$duplicate = 'Duplicate username. Please choose another.';
echo 'duplicate=y&message='.urlencode($duplicate);
}
else { // insert the data into the users table
$sql = 'INSERT INTO users (first_name,last_name,e_mail,pwd,state,country,birth_month,birth_year,birth_day)
VALUES ("'.$POST['first_name'].'","'.$POST['country_name'].'","'.$POST['birth_month'].'","'.$POST['birth_year'].'",
"'.$POST['last_name'].'","'.$POST['e_mail'].'","'.$POST['state'].'","'.$POST['birth_day'].'",
"'.sha1($POST['pwd']).'")';
$result = $db->query($sql);
if ($result) {
$created = 'Account created for '.$POST['username'];
echo 'duplicate=n&message='.urlencode($created);
}
}
?>
and here is the classes it is a php4 classes
<?php
class Database {
var $host;
var $user;
var $pwd;
var $dbName;
var $flash;
var $dblink;
var $result;
var $result0bj;
function Database($host, $user, $pwd, $dbName, $flash=1){
$this->host = $host;
$this->user = $user;
$this->pwd = $pwd;
$this->dbName = $dbname;
$this->flash = $flash;
$this->connect();
}
// Connect to the Mysql server and select the database
function connect() {
$this->dblink = @mysql_pconnect($this->host, $this->user, $this->pwd);
if (!$this->dbLink) {
$error = 'Couldn\'t connect to mySQL Server';
echo $this->flash ? 'error='.urlencode($error) : $error;
exit();
}
if (!mysql_select_db($this->dbName, $this->dbLink)) {
$error = 'Couldn\'t open Datase: '. $this->db->dbName;
echo $this->flash ? 'error='.urlencode($error) : $error;
exit();
}
return $this->dblink;
}
// Execute a SQL query
function query($query){
$this->result = mysql_query($query, $this->dblink);
if (!$this->result) {
$error = 'MySQL Error: ' .mysql_error();
echo $this->flash ? 'error='.urlencode($error) : $error;
exit();
}
// store result in new object to emulate mysqli 00 interface
$this->result0bj = new Myresult($this->result);
return $this->result0bj;
}
function close(){
// Close Mysql Connection
mysql_close($this->dblink);
}
}
class Myresult {
var $theResult;
var $num_rows;
function Myresult(&$r) {
$this->theResult = $r;
// get number of records found
$this->num_rows = mysql_num_rows($r);
}
// fetch associative arroy of result (works on one row at a time)
function fetch_assoc() {
$newRow = mysql_fetch_assoc($this->theResult);
return $newRow;
}
}
?>