PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Just whipped this up, and I'm not a good OOP programmer (as you'll see in a second), but I can't even figure out why this doesn't work.. it's pathetic..
<?php //dbclass.php
Class DB {
var $dbHost = "localhost";
var $dbUser = "d3ad1ysp0rk";
var $dbPass = "squall";
var $dbName = "testdb";
var $link;
function newdb(){
$this->conn();
$this->selectdb();
}
function conn(){
$this->$link = mysql_connect($host,$dbuser,$dbpass) or die("Could not connect: " . mysql_error());
}
function selectdb(){
//debugging stuff
/*echo $dbHost;
echo $dbUser;
echo $dbPass;
echo $dbName;
echo $link;*/
mysql_select_db($dbName, $link) or die("Could not use table: " . mysql_error());
}
function endconn(){
mysql_close($link);
}
}
?>
<?php //testsql.php
include("dbclass.php");
if(count($_POST) > 2){
$mydb = new DB();
$mydb->newdb();
extract($_POST, EXTR_SKIP);
$sql = "INSERT INTO newtable(username,password,rank) VALUES('$username','$password','$rank')";
mysql_query($sql) or die("Error: " .mysql_error());
$mydb->endconn();
}
else {
echo "You must enter all values!!";
}
?>
Errors:
Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in C:\Documents and Settings\Administrator\Desktop\Server\dbclass.php on line 23
Could not use table:
<?php
//your line
mysql_select_db($dbName, $link) or die("Could not use table: " . mysql_error());
//should be
mysql_select_db($this -> dbName, $this -> link) or die("Could not use table: " . mysql_error());
?>
Last edited by Ixplodestuff8 on Sat Apr 10, 2004 11:18 pm, edited 1 time in total.