GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing leafphp.dev โ PHP Query Builder: readable, secure database queries | Leaf PHP Query Builder โAlthough you can write raw queries using the Creating a database โThis is something you would usually want to do outside of your application, but there are a few rare cases where you might want to create a database from within your application. Leaf DB provides a php Dropping a database โDropping a database is the opposite of creating one. It deletes the database and all its contents. Leaf DB provides a php Be careful when using this method, as it will delete all the data in the database. Adding data to a database โInserting data into a database means adding new entries to a table. You specify which table and columns to use, then provide the data. To do this, Leaf provides the This method needs the name of the table you want to add data to, like "users", and then it allows you to build the rest of your query from there. What we need now is to add the data we want to insert into the table. We can do this using the Here's an example: php This is equivalent to the following SQL query: sql Of course, it automatically takes care of parameter binding, which is important for preventing SQL injection attacks. Now that we have our query, we can execute it using the php 1 2 3 4 This will insert a new user with the username "mychi" into the users table. Making values unique โA unique column does not allow duplicate values. Eg. you can't have two users with the same email address in a lot of applications. You'll usually get an error if you try to insert a duplicate value into a unique column. Leaf DB provides a php 1 2 3 4 5 6 7 8 9 This also works when you are updating a row. If you try to update a row with a value that already exists in a unique column, you'll get an error. Getting the last inserted ID โWhen you insert a new row into a table, the database assigns it a unique ID. This ID is usually an auto-incrementing integer that starts at 1 and increments by 1 for each new row. Leaf DB provides a php 1 2 3 Note that this may not work correctly if your database uses non-auto-incrementing IDs like UUIDs or ULIDs. Reading data from a database โReading from a database means retrieving data stored in a table. Leaf DB provides a php This will return all the rows in the users table. You can also specify the columns you want to return by passing them as the second argument to the php This will return only the name and created_at columns from the users table. Most of the time, you'll want to read data based on certain conditions. That's where conditional queries come in. Conditional queries โLeaf DB provides a php 1 2 3 4 This will return the matching row in the users table where the name is "John Doe". You can also pass an array of column names and values to the php 1 2 3 4 The php 1 2 3 4 This will return all the rows in the items table where the tag column contains the word "new". OR conditions โBy default, the php 1 2 3 4 If you have a record where the name is "John Doe" but the age is not 20, it won't be returned. If you want to return rows where any of the conditions are met, you can use the php 1 2 3 4 5 This will return all the rows in the users table where the name is "John Doe" or the age is 20. You can make things easier by passing an array of conditions to the php 1 2 3 4 Matching a list of values โLeaf DB doesn't ship a php 1 2 3 4 5 6 7 The values still go through parameter binding, so this stays just as safe as the builder methods. Finding Data by ID โAlmost every database table has an php This will look in the "users" table for the row with the Data relationships โSometimes you might want to retrieve data from multiple tables. For example, you might want to retrieve all the posts by a user. Leaf DB provides a php 1 2 3 4 This will return all the users in the users table along with their posts. It also works the same way for getting a single row. php 1 2 3 4 5 The Updating data in a database โUpdating data in a database works by finding the data you want to update and then passing new data in to change the existing data. Leaf DB provides an Here's an example: php 1 2 3 4 This will update every row in the users table with the name "Jane Doe". This is not something you want to do, so you can use conditional queries to update specific rows. php 1 2 3 4 5 This will update the row in the users table with the Deleting data from a database โDeleting data from a database works by finding the data you want to delete and then removing it from the table. Leaf DB provides a Here's an example: php This query will delete all the rows in the users table (be careful with this one). There is rarely a case where you would want to delete all the rows in a table, so you can use conditional queries to delete specific rows. php 1 2 3 4 This will delete the row in the users table with the Database Transactions โDatabase transactions are a way to ... Leaf DB allows you to create database transactions using the php 1 2 3 4 5 6 7 8 9 10 11 Transactions work on every PDO driver Leaf DB supports, so you can use them the same way on MySQL, SQLite, Postgres, and SQL Server. If anything in the function fails, Leaf will automatically rollback every change that has been made in the database till that point and return php 1 2 3 4 5 6 7 8 9 This is useful especially when you have a set of queries that rely on third party influence. Transactions and models Everything inside a transaction needs to run on the same database connection to be rolled back together. In Leaf MVC with leafs/db 5.1+, Hiding columns from results โSometimes you might want to hide certain columns from the results of a query. For instance, you might want to hide the password column from the results of a query on the users table. Leaf DB provides a php 1 2 3 4 Or on a conditional query: php 1 2 3 4 5 Adding custom fields to results โSometimes you might want to add custom fields to the results of a query. For instance, you might want to add a php 1 2 3 4 This will add a php 1 2 3 4 5 Ordering results โYou can order the results of a query using the php 1 2 3 4
php 1 2 3 4 Limiting results โYou can limit the number of results returned by a query using the php 1 2 3 4 It works even better when combined with the php 1 2 3 4 5 Counting results โYou can count the number of results returned by a query using the php 1 2 3 Or even with complex queries: php 1 2 3 4 As of leafs/db 5.1, Error Handling โThere are lots of times where your query might fail. This could be because of a syntax error, a missing table, or a missing column. Leaf DB provides an php 1 2 3 4 5 6 7 8 9 You will get an error like this: php 1 2 3 We get this error because we set the |
| You are visitor 01402503 โ GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800ร600. No cookies required. |