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
$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.
$sql = mysql_query("SELECT * FROM your_table");
$row = mysql_num_row($sql);
if($row > 0){
//do something
}else{
echo "No records found";
}