1

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

user692942
  • 16,398
  • 7
  • 76
  • 175
Max
  • 49
  • 1
  • 7
  • 1
    `$npost = mysqli_fetch_array(mysqli_query($con, "SELECT id FROM mytable1 WHERE date='". $sDate ."'"))['id'];` – Steve Sep 15 '14 at 14:52
  • 1
    Both methods ASP and PHP are flawed using that approach. Why?, SQL Injection Attacks. Two words for you "Parametrised Queries". Both languages are capable of doing this. Just because code is short and easy to write doesn't mean you should. – user692942 Sep 15 '14 at 18:48
  • For Classic ASP you should be using `ADODB.Command` object and in PHP should be using something like `mysqli` or `PDO` (See [Answer by @Theo to How can I prevent SQL-injection in PHP?](http://stackoverflow.com/a/60496/692942)). – user692942 Sep 15 '14 at 18:54
  • Why do you care about shortness? You should care about correctness, security, error handling, etc. You get no bonus points for cramming a bunch of code onto a single line. If you do care that much, you should at least abstract out the verbose portion of your code into a class or function that encapsulates all the proper error handling and such. – Mike Brant Sep 15 '14 at 19:56

1 Answers1

0

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!

Tim Lewis
  • 27,813
  • 13
  • 73
  • 102