0

I am tired of making forms, inserting etc in MySQL and manually make validating on post fields.

In ruby it is all easy, but is there something like in PHP?

I hate to type

 INSERT INTO something (1million, 1sdlsds,sds,ds,d,sd,sd,sd,sd,sd,s,ds,ds,ds,dds,sds,dsdsds,ds,dsd,sd,sd,sdsdsdsd) values ('@','a,'sds...........)

and also make manually validation on each post field.

Is there some alternative to make this easy?

In ruby it's just:

 o = Users.new(:name => "Jesper", :email => "Noget").save!

And the validation in the model.

jesper
  • 217
  • 1
  • 2
  • 10

4 Answers4

3

You could always use a framework like Laravel.

The syntax for inserting is.

$user = new User();
$user->name = "Jesper";
$user->eamil = "Noget";

$user->save()

You don't necessarily have to use laravel, most frameworks have their own flavor that abstract the sql queries to objects and whatnot. Other popular options are CodeIgniter, CakePHP, Kohana...

castis
  • 8,154
  • 4
  • 41
  • 63
1

Just insert the stuff you want, just like Ruby. After all, PHP, Ruby, etc, are just sitting on top of MySQL (and other databases). They don't change the functionality of those databases.

INSERT INTO something (name , email ) values ('Jesper','Noget')

Also, you seem to be confusing raw SQL with objects, classes, and frameworks. So your comparison isn't fair or accurate.

John Conde
  • 217,595
  • 99
  • 455
  • 496
0

In Ruby on Rails it is as simple as Foo.create!(:bar => 1)

The on Rails part is a framework. You can not compare a framework with a language.

If you want a similar functionality for PHP, you should look up frameworks like Zend, Symfony or similar, which use the MVC pattern.

scones
  • 3,317
  • 23
  • 34
0

You can use PDO object.

For example :

<?php
/* Execute a prepared request passing array values */
$sql = 'SELECT name, color, weight
    FROM fruit
WHERE weight < :weight AND color = :color';
$sth = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));

$sth->execute(array(':calories' => 150, ':color' => 'red'));
$red = $sth->fetchAll();

$sth->execute(array(':calories' => 175, ':color' => 'yellow'));
$yellow = $sth->fetchAll();
?>

More examples here : http://www.php.net/manual/fr/pdo.prepare.php

The good news is the request is sanitized automatically, and you avoid security fails...

Caution : You have to use PHP >= 5.1.0

Pouki
  • 1,654
  • 12
  • 18