0

hi to all m new to OOP PHP. i have following piece of code here in the following:

test.php

<?php
class test 
{
    private $dbhost;
    private $dbname;
    private $user;
    private $pass;
    public function __construct()
    {
        $this->dbhost = 'localhost';
        $this->dbname = 'XXXX';
        $this->user = 'root';
        $this->pass = '';
    }
    public function open($obj)
    {
        $con = mysql_connect($this->dbhost,$this->user,$this->pass) or die(mysql_error());
        $db = mysql_select_db($this->dbname)or die(mysql_error());
        $query = mysql_query("SELECT * FROM '".$obj."'") or die(mysql_error());

    }

}

?>

in the other file named test2.php i have the following piece of code:

<?php
    require_once('testClass.php');
    $obj = new test();
    $obj2 = 'user';
    $obj->open($obj2);
?>

and i got the following error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''user'' at line 1

thnx in advance for help

2
  • 1
    If you are going to code OO, you should use PDO, as it is OO and may be extended by your own classes. Currently you are re-inventing the wheel… Commented Oct 11, 2012 at 8:32
  • 1
    In addition to @feeela's comment, you're re-inventing the wheel, but you're also using deprecated functions to build it. See the red box on php.net/mysql_query? It's there for a reason. Commented Oct 11, 2012 at 8:37

2 Answers 2

4

$obj should be tablename and you doesn't need to put single quote around table name.

mysql_query("SELECT * FROM ".$obj) or die(mysql_error());

Edit:

 while($row = mysql_fetch_array($query)){
    echo $row['userId'];
 } 

Suggestion: Your query must be affected by mysql injection attack. It is really good to use PDO or Mysqli lib.

Sign up to request clarification or add additional context in comments.

2 Comments

its working now accurately but there is another error like $show = mysql_fetch_array($query); foreach($show as $shown) { echo $shown['userId']; } but it print now 11aa11nnpp00aa00aa22 instead of 1,2...which are in my table
hi GBD its working quiet accurately right now. but i have another question that the way in which i m trying to echo the output is working good in simple function. but here i had deal with the while function would you like to help me to understand the difference among both??
1
$sql = mysql_query("SELECT * FROM your_table");
$row = mysql_num_row($sql);
if($row > 0){
//do something
}else{
echo "No records found";
}

3 Comments

thanx Newbie for reply but i have plays with this a lot and m now going to try it in OOP PHP...
its ok. but they way in which i am working you can see the answer from GBD and mine conversation with him in comments. you may also can clear your point about the OOP PHP
Try This maybe this will help you goodluck to you. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.