OOP echoing out all...

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!

Moderator: General Moderators

User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

OOP echoing out all...

Post 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>
 
lshaw
Forum Commoner
Posts: 69
Joined: Mon Apr 20, 2009 3:40 pm
Location: United Kingdom

Re: OOP echoing out all...

Post by lshaw »

I can see the error here. you are querying and undefined variable?
Declaration:

Code: Select all

 
$mysql = "SELECT * FROM login";
 
Next Line:

Code: Select all

 
$query = $DB->dbquery($mtsql);
 
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']; 
}
 
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: OOP echoing out all...

Post 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)
lshaw
Forum Commoner
Posts: 69
Joined: Mon Apr 20, 2009 3:40 pm
Location: United Kingdom

Re: OOP echoing out all...

Post 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:

Code: Select all

$result = $DB->fetchRow($query)
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
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: OOP echoing out all...

Post 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(); ?>
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: OOP echoing out all...

Post 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 :wink: )
lshaw
Forum Commoner
Posts: 69
Joined: Mon Apr 20, 2009 3:40 pm
Location: United Kingdom

Re: OOP echoing out all...

Post 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
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: OOP echoing out all...

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: OOP echoing out all...

Post 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
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: OOP echoing out all...

Post by Goofan »

allready done that was one of the faults at the beginning. and if u look my "later" code include just that.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: OOP echoing out all...

Post 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!
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: OOP echoing out all...

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: OOP echoing out all...

Post 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?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: OOP echoing out all...

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: OOP echoing out all...

Post 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);
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply