OOP PHP SQL select

103

I create class Database:

class Database  
{  

    private $db_host = "localhost";   
    private $db_user = "root";   
    private $db_pass = "";   
    private $db_name = "test";

    private $con = false;
    private $result = array();

}

There are more functions but is not important to mention them.

I have this function:

public function sql($sql){
    $query = @mysql_query($sql);
    if($query){
        // If the query returns >= 1 assign the number of rows to numResults
        $this->numResults = mysql_num_rows($query);
        // Loop through the query results by the number of rows returned
        for($i = 0; $i < $this->numResults; $i++){
            $r = mysql_fetch_array($query);
            $key = array_keys($r);
            for($x = 0; $x < count($key); $x++){
                // Sanitizes keys so only alphavalues are allowed
                if(!is_int($key[$x])){
                    if(mysql_num_rows($query) > 1){
                        $this->result[$i][$key[$x]] = $r[$key[$x]];
                    }else if(mysql_num_rows($query) < 1){
                        $this->result = null;
                    }else{
                        $this->result[$key[$x]] = $r[$key[$x]];
                    }
                }
            }
        }
        return true; // Query was successful
    }else{
        array_push($this->result,mysql_error());
        return false; // No rows where returned
    }
}

The problem is when i use in my main script for example a simple select is working fine:

$db = new Database();
$db->connect();
$db->sql("Select * FROM crudclass");
$res = $db->getResult();

print_r($res);
echo "<br>";
foreach($res as $output){
echo $output["id"]."&nbsp;";
    echo $output["name"]."&nbsp;";
echo $output["email"]."<br />";
}
?>

I get all the results:

Array ( [0] => Array ( [id] => 1 [name] => Name 1 [email] => [email protected] ) [1] =>                                                                     Array ( [id] => 2 [name] => Name 2 [email] => [email protected] ) [2] => Array ( [id] => 3   [name] => Name 3 [email] => [email protected] ) ) 
1 Name 1 [email protected]
2 Name 2 [email protected]
3 Name 3 [email protected]

but if i want to use for example WHERe like this:

$db->sql("Select * FROM crudclass where id=1");

i get this error

Array ( [id] => 1 [name] => Name 1 [email] => [email protected] ) 

Warning: Illegal string offset 'id' in C:\xampp\htdocs\index.php on line 14
1 
Warning: Illegal string offset 'name' in C:\xampp\htdocs\index.php on line 15
1 
Warning: Illegal string offset 'email' in C:\xampp\htdocs\index.php on line 16
1

Warning: Illegal string offset 'id' in C:\xampp\htdocs\index.php on line 14
N 
Warning: Illegal string offset 'name' in C:\xampp\htdocs\index.php on line 15
N 
Warning: Illegal string offset 'email' in C:\xampp\htdocs\index.php on line 16
N

Warning: Illegal string offset 'id' in C:\xampp\htdocs\index.php on line 14
n 
Warning: Illegal string offset 'name' in C:\xampp\htdocs\index.php on line 15
n 
Warning: Illegal string offset 'email' in C:\xampp\htdocs\index.php on line 16
n
25

Answer

Solution:

Use

Select * FROM crudclass where `id`=1

People are also looking for solutions to the problem: javascript - Create Array with JSON

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.