Search code examples
phpmysqlexecute

Shorter way of Execute SQL in php


I like the "conn.execute" in ASP. Because it is pretty short, and I just want to extract one thing. But in PHP I have to use 3-5 rows of code to get the same:

nPost=Conn.Execute("SELECT id AS nPost FROM mytable1 WHERE date='"& sDate &"' ").fields(0)

And here is the PHP equal:

$nPost="SELECT id FROM mytable1 WHERE date='". sDate ."'";
$result=mysqli_query($con, $nPost);
while($rows = mysqli_fetch_array($result)) 
{ 
$nPost  = $rows['id'];
}

Is there a shorter way to achieve this? Thanks


Solution

  • The only way I'd know is using a PHP framework like CakePhp, Laravel or Symphony. They all have built in query functions that are usually as simple as

    $Table->find(columns);
    $Table->all();
    

    or

    $this->Table->find("first");
    $this->Table->find("all", array("conditions" => array("column" -> "conditions")));
    

    etc. It's different across the available MVC's, but they're all pretty similar. As far as non-mvc PHP MySQLi is pretty speedy from what I've used.

    Hope that helps!