Page 1 of 2
OOP echoing out all...
Posted: Wed Feb 03, 2010 8:21 am
by Goofan
My page got a connection to the database (which i think is correct). but what it does is echoing out everything i wrote in my Output.php file
Echoing this out:
dbconnect(); $mysql = "SELECT * FROM login"; $query = $DB->dbquery($mtsql); //fetch all rows while ($result = $DB->fetchRow($query)) { $user=$row['user']; $pass=$row['pass']; } echo $user .$pass; $DB->dbclose(); ?>
class.DBC.php
Code: Select all
<?php
class MySQL
{
var $connection;
var $host;
var $name;
var $password;
var $database;
function MySQL()
{
$this->host = "localhost";
$this->name = "root";
$this->password = "";
$this->database = "login";
}
function dbconnect()
{
$connection = mysql_connect($this->host,$this->name,$this->password);
mysql_select_db($this->database,$connection);
}
function dbclose()
{
mysql_close($this->connection);
}
function fetchRow($query)
{
$rows = mysql_fetch_row($query);
return $rows;
}
function fetchNum($query)
{
$num = mysql_num_rows($query);
return $num;
}
function dbquery($sql)
{
$query = mysql_query($sql) or die(mysql_error());
return $query;
}
}
?>
Output.php
Code: Select all
<?
//This is the file that I am using for testing
require_once("sql/mysql.php");
$DB = new MySQL();
$connection = $DB->dbconnect();
$mysql = "SELECT * FROM login";
$query = $DB->dbquery($mtsql);
//fetch all rows
while ($result = $DB->fetchRow($query))
{
$user=$row['user'];
$pass=$row['pass'];
}
echo $user .$pass;
$DB->dbclose();
?>
<html>
<head><title>Login page</title> </head>
<body bgcolor="white">
</body>
</html>
Re: OOP echoing out all...
Posted: Wed Feb 03, 2010 8:30 am
by lshaw
I can see the error here. you are querying and undefined variable?
Declaration:
Next Line:
I think this is also an error
Code: Select all
while ($result = $DB->fetchRow($query))//you declare $result as the array
{
$user=$row['user'];//you fetch data from a $row array
$pass=$row['pass'];
}
Re: OOP echoing out all...
Posted: Wed Feb 03, 2010 8:45 am
by Goofan
lshaw wrote:
I think this is also an error
Code: Select all
while ($result = $DB->fetchRow($query))//you declare $result as the array
{
$user=$row['user'];//you fetch data from a $row array
$pass=$row['pass'];
}
aight i changed the small stuffs, dont exacly what u mean by that.
this i changed (typo):
$query = $DB->dbquery($mtsql);
to
$query = $DB->dbquery($mysql);
please use my code and correct me please. much easier as im not that into php just yet. (learning)
Re: OOP echoing out all...
Posted: Wed Feb 03, 2010 9:03 am
by lshaw
Code: Select all
while ($result = $DB->fetchRow($query))//you declare $result as the array
{
$user=$row['user'];//you fetch data from a $row array
$pass=$row['pass'];
}
when you write:
you are assign the variable $results to an array of all your records.
$result is like:
Record 1
->username='bob'
->password='marley'
Record 2
->username='John'
->password='smith'
etc.
to fetch this information you write:
Code: Select all
$result['username'];
$result['password'];
You are trying to use $row['username'] and the $row array does not exist
This is how i believe it should look:
Code: Select all
while ($result = $DB->fetchRow($query))//you declare $result as the array
{
$user=$result['user'];
$pass=$result['pass'];
}
EDIT - I have just been testing out simalar code and I got this working using mysql_fetch_array rather than mysql_fetch_row in the class. Don't know whether this suits you but i think the functions are pretty similar
Re: OOP echoing out all...
Posted: Wed Feb 03, 2010 10:44 am
by Goofan
ok so i first changed it so that it were result instead of rows and that didnt work so i tried to use array still the same
Why is it ouputting this?
dbconnect(); $mysql = "SELECT * FROM login"; $query = $DB->dbquery($mysql); //fetch all rows while ($result = $DB->fetchArray($query)) { $user=$result['user']; $pass=$result['pass']; } echo $user .$pass; $DB->dbclose(); ?>
Re: OOP echoing out all...
Posted: Wed Feb 03, 2010 10:50 am
by Goofan
ok so i started to make "very" small changes and it worked "50%" but now i got a new error message:
Notice: Undefined index: user in C:\Program Files\wamp\www\www\Projektarbete\login_new.php on line 18
Notice: Undefined index: pass in C:\Program Files\wamp\www\www\Projektarbete\login_new.php on line 19
Warning: mysql_close() expects parameter 1 to be resource, null given in C:\Program Files\wamp\www\www\Projektarbete\class.DBC.php on line 28
Code: Select all
<?php
//This is the file that I am using for testing
require_once("class.DBC.php");
$DB = new MySQL();
$connection = $DB->dbconnect();
$mysql = "SELECT * FROM user";
$query = $DB->dbquery($mysql);
//fetch all rows
while ($result = $DB->fetchRow($query))
{
$user=$result['user']; <----- this dosnt exist in database it sais...
$pass=$result['pass']; <----- this dosnt exist in database it sais...
}
echo $user .$pass;
$DB->dbclose();
?>
<html>
<head><title>Login page</title> </head>
<body bgcolor="white">
</body>
</html>
now i have no idé why it works now instead of before (so dont ask

)
Re: OOP echoing out all...
Posted: Wed Feb 03, 2010 10:58 am
by lshaw
Are you using mysql_fetch_row still? I think when I was testing this thats the error I got with mysql_fetch_row. Try mysql_fetch_array again or another similar function
Re: OOP echoing out all...
Posted: Wed Feb 03, 2010 11:08 am
by Goofan
it works with the array but i still got this error:
Warning: mysql_close() expects parameter 1 to be resource, null given in C:\Program Files\wamp\www\www\Projektarbete\class.DBC.php on line 28
Line 28 is the end of php " ?> "
know what it means?
Re: OOP echoing out all...
Posted: Wed Feb 03, 2010 11:11 am
by AbraCadaver
Goofan wrote:ok so i first changed it so that it were result instead of rows and that didnt work so i tried to use array still the same
Why is it ouputting this?
dbconnect(); $mysql = "SELECT * FROM login"; $query = $DB->dbquery($mysql); //fetch all rows while ($result = $DB->fetchArray($query)) { $user=$result['user']; $pass=$result['pass']; } echo $user .$pass; $DB->dbclose(); ?>
Change <? to <?php
Re: OOP echoing out all...
Posted: Wed Feb 03, 2010 11:20 am
by Goofan
allready done that was one of the faults at the beginning. and if u look my "later" code include just that.
Re: OOP echoing out all...
Posted: Wed Feb 03, 2010 11:24 am
by AbraCadaver
Goofan wrote:it works with the array but i still got this error:
Warning: mysql_close() expects parameter 1 to be resource, null given in C:\Program Files\wamp\www\www\Projektarbete\class.DBC.php on line 28
Line 28 is the end of php " ?> "
know what it means?
O.K. so pass the $connection to the close(). Just read the error and it tells you what to do!
Re: OOP echoing out all...
Posted: Wed Feb 03, 2010 11:27 am
by Goofan
u mean like this?
Code: Select all
function dbclose()
{
mysql_close($this->connection);
}
becouse this is what ive got.
if not then please show me
Re: OOP echoing out all...
Posted: Wed Feb 03, 2010 11:53 am
by AbraCadaver
Goofan wrote:u mean like this?
Code: Select all
function dbclose()
{
mysql_close($this->connection);
}
becouse this is what ive got.
if not then please show me
Where do you set $this->connection?
Re: OOP echoing out all...
Posted: Wed Feb 03, 2010 11:56 am
by Goofan
well ill give u both my "pages". If i dont got it then tell me. im not that good with OOP so this is actually a piece of my own code and some taken so that i could understand more.
class.DBC.php
Code: Select all
<?php
class MySQL
{
var $connection;
var $host;
var $name;
var $password;
var $database;
function MySQL()
{
$this->host = "localhost";
$this->name = "root";
$this->password = "";
$this->database = "login";
}
function dbconnect()
{
$connection = mysql_connect($this->host,$this->name,$this->password);
mysql_select_db($this->database,$connection);
}
function dbclose()
{
mysql_close($this->connection);
}
function fetchRow($query)
{
$rows = mysql_fetch_row($query);
return $rows;
}
function fetchArray($query){
$array = mysql_fetch_array($query);
return $array;
}
function fetchNum($query)
{
$num = mysql_num_rows($query);
return $num;
}
function dbquery($sql)
{
$query = mysql_query($sql) or die(mysql_error());
return $query;
}
}
?>
Output.php
Code: Select all
<?php
//This is the file that I am using to output.
require_once("class.DBC.php");
$DB = new MySQL();
$connection = $DB->dbconnect();
$mysql = "SELECT * FROM user";
$query = $DB->dbquery($mysql);
//fetch all rows
while($array = $DB->fetchArray($query))
{
$user = $array['user'];
$pass = $array['pass'];
}
echo $user ."<br>" .$pass;
$DB->dbclose();
?>
<html>
<head><title>Login page</title> </head>
<body bgcolor="#A4A8B0">
</body>
</html>
EDIT: as i looked throught my code i thought it might have been $connection and if i made the $ infront i got these errors instead:
Notice: Undefined variable: connection in C:\Program Files\wamp\www\www\Projektarbete\class.DBC.php on line 28
Fatal error: Cannot access empty property in C:\Program Files\wamp\www\www\Projektarbete\class.DBC.php on line 28
Re: OOP echoing out all...
Posted: Wed Feb 03, 2010 12:04 pm
by AbraCadaver
My point was that you never set $this->connection, so how can you pass it to mysql_close()?
Code: Select all
function dbconnect()
{
$this->connection = mysql_connect($this->host,$this->name,$this->password) or die(mysql_error());
mysql_select_db($this->database,$this->connection);
}