Instance analysis of thinkphp model initialization Example _php

Source: Internet
Author: User
Tags dsn

The example of this article describes the method of thinkphp model initialization. Share to everyone for your reference, specific as follows:


  /* $dsn = ' mysql://bookman:book123@localhost:3306/weapons ';
  $guns _tab = M (' Guns ', ', $dsn);
  $gun 1 = $guns _tab->select ();
  echo $guns _tab-> getlastsql ();
  Dump ($gun 1);
* *
*
  $guns _tab = M (' Guns ');
  $gun 1 = $guns _tab->select ();
  echo $guns _tab-> getlastsql ();
  Dump ($gun 1);
* *
  $guns _tab = new Gunsmodel ();
  $gun 1 = $guns _tab->select ()-> page (' 1,1 ');
  echo $guns _tab-> getlastsql ();
  Dump ($gun 1);
  $guns _tab-> showinfo ();
  /* $guns _tab = new Detectmodel (' Guns ');
  $gun 1 = $guns _tab-> Select ();
  echo $guns _tab-> getlastsql ();
  Dump ($gun 1);
  $guns _tab-> showinfo ();
*/

1. Connection mode from configuration file

$dsn = ' mysql://bookman:book123@localhost:3306/weapons ';
$guns _tab = M (' Guns ', ', $dsn);
$gun 1 = $guns _tab->select ();
echo $guns _tab-> getlastsql ();
Dump ($gun 1);

2. Connect the database according to the configuration file, the table name must distinguish clearly

$guns _tab = M (' Guns ');
$gun 1 = $guns _tab->select ();
echo $guns _tab-> getlastsql ();
Dump ($gun 1);

3. I have a custom model that implements my own business methods, and I have a guns table so I can initialize the model like this. You can get the data from the guns table, and you can use the business methods in my custom model.

$guns _tab = new Detectmodel (' Guns ');
$gun 1 = $guns _tab-> Select ();
echo $guns _tab-> getlastsql ();
Dump ($gun 1);
$guns _tab-> showinfo ();

I hope this article will help you with the PHP program design based on thinkphp framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.