<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Anass Boutaline</title>
    <description>The latest articles on Forem by Anass Boutaline (@butalin).</description>
    <link>https://forem.com/butalin</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F174562%2F7b278bbd-cd5b-4b7f-aa5b-06a82b58081f.jpg</url>
      <title>Forem: Anass Boutaline</title>
      <link>https://forem.com/butalin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/butalin"/>
    <language>en</language>
    <item>
      <title>Building an Automatic Routing System in ReactJS with react-router-dom and TypeScript</title>
      <dc:creator>Anass Boutaline</dc:creator>
      <pubDate>Sun, 17 Mar 2024 01:07:02 +0000</pubDate>
      <link>https://forem.com/butalin/building-an-automatic-routing-system-in-reactjs-with-react-router-dom-and-typescript-32pp</link>
      <guid>https://forem.com/butalin/building-an-automatic-routing-system-in-reactjs-with-react-router-dom-and-typescript-32pp</guid>
      <description>&lt;p&gt;In this article, we embark on a journey to construct an automatic routing system within a ReactJS application using react-router-dom and TypeScript. Our quest begins with the familiar starting point of any React project: Create React App. With its simplicity and efficiency, Create React App provides the perfect launching pad for our endeavors. We'll seamlessly integrate TypeScript into our project, leveraging its static typing capabilities to catch errors early in the development process and enhance code maintainability.&lt;/p&gt;

&lt;h2&gt;
  
  
  The desired results
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    RouteContainer.register({
        path: '/',
        element: &amp;lt;Store /&amp;gt;,
        title: "Boutique",
        label: "Boutique"
    })

    RouteContainer.registreGroup({
        path: '/products',
        routes: [
            {
                path: '/best-selling',
                element: &amp;lt;BestSelling /&amp;gt;,
                title: "Best Product Landing Page",
                label: 'My Product'
            },
            {
                path: '/newest',
                element: &amp;lt;Newest /&amp;gt;,
                title: "Best Product Landing Page",
                label: 'My Product'
            },
            {
                path: '/:id',
                element: &amp;lt;SingleProduct /&amp;gt;,
                title: "Single Product",
                label: "Single Product"
            },
        ]
    })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code will generate 4 routes as follow&lt;br&gt;
&lt;code&gt;/&lt;/code&gt;=&amp;gt; Home page will render &lt;code&gt;&amp;lt;Store /&amp;gt;&lt;/code&gt; component&lt;br&gt;
&lt;code&gt;/products/best-selling&lt;/code&gt; =&amp;gt; will render &lt;code&gt;&amp;lt;BestSelling /&amp;gt;&lt;/code&gt; component&lt;br&gt;
&lt;code&gt;/products/newest&lt;/code&gt; =&amp;gt; will render &lt;code&gt;&amp;lt;Newest /&amp;gt;&lt;/code&gt; component&lt;br&gt;
&lt;code&gt;/products/{id}&lt;/code&gt; =&amp;gt; will render &lt;code&gt;&amp;lt;SingleProduct /&amp;gt;&lt;/code&gt;component, and &lt;code&gt;id&lt;/code&gt; will be accessible from &lt;code&gt;props.navigation&lt;/code&gt; passed nativelly to the component.&lt;/p&gt;
&lt;h2&gt;
  
  
  Getting started
&lt;/h2&gt;

&lt;p&gt;First, let's create a file (just a regular js file) where we will define our routes in the manner shown in the code snippet above. Let's assume that we already have four components: &lt;code&gt;&amp;lt;Store /&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;BestSelling /&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;Newest /&amp;gt;&lt;/code&gt;, and &lt;code&gt;&amp;lt;SingleProduct /&amp;gt;&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// File: src/Routes/routes.js

import RouteContainer from '../Routing/RouteContainer' // this is where we will handle the routing logic

// import your components here...



export default function () {

    RouteContainer.register({
        path: '/',
        element: &amp;lt;Store /&amp;gt;,
        title: "Boutique",
        label: "Boutique"
    })

    RouteContainer.registreGroup({
        path: '/products',
        routes: [
            {
                path: '/best-selling',
                element: &amp;lt;BestSelling /&amp;gt;,
                title: "Best Product Landing Page",
                label: 'My Product'
            },
            {
                path: '/newest',
                element: &amp;lt;Newest /&amp;gt;,
                title: "Best Product Landing Page",
                label: 'My Product'
            },
            {
                path: '/:id',
                element: &amp;lt;SingleProduct /&amp;gt;,
                title: "Single Product",
                label: "Single Product"
            },
        ]
    })
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the &lt;code&gt;RouteContainer.register(RouteProps)&lt;/code&gt; function is called, &lt;code&gt;RouteContainer&lt;/code&gt; will save the &lt;strong&gt;route data&lt;/strong&gt; into a static array, which will be used later to dispatch routes. Let's first define some types.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//File: src/Routing/RouteContainer.ts

// This is the shape of route config (route data)
type routeConfig = {
    path: string,
    element: React.JSX.Element | React.ReactElement,
    title: string | undefined,
    label?: string 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and crafting the first version of the &lt;code&gt;RouteContainer&lt;/code&gt; class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//File: src/Routing/RouteContainer.ts

export default class RouteContainer {

    static rawRoutes: routeConfig[] = []

    static register (routeConfig: routeConfig): void {
       RouteContainer.rawRoutes.push( routeConfig );
    } 

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So now, the &lt;code&gt;RouteContainer&lt;/code&gt; is just using a &lt;code&gt;static array&lt;/code&gt; to hold our Route configs or data.&lt;/p&gt;

&lt;p&gt;let's see how we are going to use this to render element based on route config.&lt;/p&gt;

&lt;p&gt;for that we will create a &lt;code&gt;dispach&lt;/code&gt; function inside &lt;code&gt;RouteContainer&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//File: src/Routing/RouteContainer.ts

static dispatch () {
        return RouteContainer.rawRoutes
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;in this function we are just returning the rawRoutes array, basically this array will be used by useRoutes from react-router-dom to render routes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// File: App.js

function App() {
  return (
        &amp;lt;BrowserRouter&amp;gt;
          &amp;lt;AppLauncher /&amp;gt;
        &amp;lt;/BrowserRouter&amp;gt;
  );
}

export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// File AppLauncher.js

import { useRoutes } from "react-router-dom"
import RouteContainer from "./Routing/RouteContainer"
import registerRoutes from "../Routes/routes"

export default function RouteDispatcher () {

    registerRoutes() // register routes in RouteContainer 


    return useRoutes([].concat( RouteContainer.dispatch() /** Dispatch registred route inside RouteRendrer */))



}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can finally add &lt;code&gt;registerGroup&lt;/code&gt; to &lt;code&gt;RouteContainer&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//File: src/Routing/RouteContainer.ts

type routeGroupConfig = {
    routes: Array&amp;lt;routeConfig&amp;gt;,
    path: string
}

...

export default class RouteContainer {

...

static registreGroup ( routeGroupConfig: routeGroupConfig ) {
        routeGroupConfig.routes.forEach(routeConfig =&amp;gt; {
            RouteContainer.register( {
                ...routeConfig,
                path: RouteContainer.prepareAndMergePaths( routeGroupConfig.path, routeConfig.path )
            } )
        });
    }


private static prepareAndMergePaths ( path: string, anOther: string  ) {
        return RouteContainer.preparePath( path ) + RouteContainer.preparePath( anOther )
    }

 private static preparePath(str: string) : string{
        if( str.length === 0 ) {
            return ""
        }
        str = str.endsWith('/') ? str.slice(0, -1) : str;
        str = str.startsWith('/') ? str.slice(1, str.length) : str;

        return str + "/"
    }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yeah, this was easy. but the question is why we are doing this?&lt;/p&gt;

&lt;p&gt;I'm doing this to keep my routes organized in a single place, for example, in a &lt;code&gt;routes.js&lt;/code&gt; file, making it easy to remove, add, or update routes in my app. However, the best part is when each route needs specific contexts. If you find this helpful, comment, and I will post a new article on how to use this architecture to automatically inject &lt;code&gt;providers&lt;/code&gt; and &lt;code&gt;layouts&lt;/code&gt; into each &lt;strong&gt;route&lt;/strong&gt;.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Mobilo.design: MERN with SSR from scratch - Project Challenges</title>
      <dc:creator>Anass Boutaline</dc:creator>
      <pubDate>Fri, 12 Aug 2022 13:52:24 +0000</pubDate>
      <link>https://forem.com/butalin/mobilodesign-mern-with-ssr-from-scratch-project-challenges-1c20</link>
      <guid>https://forem.com/butalin/mobilodesign-mern-with-ssr-from-scratch-project-challenges-1c20</guid>
      <description>&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;li&gt;Planing&lt;/li&gt;
&lt;li&gt;Project Structure&lt;/li&gt;
&lt;li&gt;Webpack &amp;amp; Babel configurations&lt;/li&gt;
&lt;li&gt;Chalenges and Reactions&lt;/li&gt;
&lt;li&gt;Deployments and Integrations&lt;/li&gt;
&lt;li&gt;Maintenance&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Check what I am building: &lt;a href="https://mobilo.design"&gt;Mobilo.design&lt;/a&gt;&lt;br&gt;
Making a smooth, fast web app to list screenshots of the best in class in production app for IOS and Android, some kind of UX/UI Mobile designs inspiration website, the idea is quite simple, get a database to save apps, screenshots, and other metadata, a backend language as a service provider, and a front end for humans(Just kidding).&lt;/p&gt;


&lt;h2&gt;
  
  
  Planing
&lt;/h2&gt;

&lt;p&gt;Basically, this is the important step I consider while working on a new project, because the stack you choose will go with you all the way long, from building to maintenance, so here's how I choose the MERN stack, first of all, I list my requirements:&lt;/p&gt;
&lt;h3&gt;
  
  
  Data requirements, why MongoDB?
&lt;/h3&gt;

&lt;p&gt;The first time, I didn't even know what is the data I should be saving for each app or screenshot, I need a flexible database structure (I don't even need a structure), so SQL databases are not a good choice at the time, I go NoSQL and I choose MongoDB, and why MongoDB? because I've never worked with NoSQL databases before, and MongoDB is well known. &lt;code&gt;(Well known === Big Community === Well documented === there is always a solution for your needs)&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Backend requirements, Why NodeJS?
&lt;/h3&gt;

&lt;p&gt;Python with beanie, Pydantic &amp;amp; FastAPI, what a winning stack, I've already worked with that before, so I will not struggle a lot, but for SEO purposes, I really need to make use of renderToString from React, and NodeJS with MongoDB, they work better together, So NodeJS.&lt;/p&gt;
&lt;h3&gt;
  
  
  Frontend requirements, Why React?
&lt;/h3&gt;

&lt;p&gt;I need to React fast (LOL), can't tell why rather I always use React on my projects, even for mobile Apps with React-Native.&lt;/p&gt;
&lt;h3&gt;
  
  
  Why not Next or Gatsby?
&lt;/h3&gt;

&lt;p&gt;I know, NextJS makes it easy to create SSR web apps, but I really want to do it by myself with Webpack &amp;amp; Babel, should be fun (Not true).&lt;/p&gt;


&lt;h2&gt;
  
  
  Project Structure
&lt;/h2&gt;

&lt;p&gt;Here's another important decision to make, how you are gonna f**ing structure this project, considere that this decision is hard as changing the structure later will cost you a day or two (maybe more).&lt;br&gt;
finnaly I came over this structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+AppFolder
--assets
+-src
---Components
---Routes
+-server
---Models
---Routes
+--Providers
----Repositories
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Whats you taughts about my ways to choose stack and my project folder structure, next post will be about how i configured webpack and babel, what are the packages and plugins i used along the way, don't forget to tell me your taughts and follow me so i can keep writing about my other projects,&lt;br&gt;
 &lt;code&gt;exit();&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dev.to/@butalin:~$Bye
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>react</category>
      <category>node</category>
      <category>mongodb</category>
    </item>
    <item>
      <title>How i prevent SQL Injection in my PHP code</title>
      <dc:creator>Anass Boutaline</dc:creator>
      <pubDate>Tue, 15 Jun 2021 21:21:14 +0000</pubDate>
      <link>https://forem.com/butalin/how-i-prevent-sql-injection-in-my-php-code-ijj</link>
      <guid>https://forem.com/butalin/how-i-prevent-sql-injection-in-my-php-code-ijj</guid>
      <description>&lt;p&gt;SQL injection is a code injection technique used to attack data-driven applications, in which malicious SQL statements are inserted into an entry field for execution. This is what Wikipedia can say about SQL injection.&lt;/p&gt;

&lt;p&gt;My small definition to SQL injection is an old school technique for attackers to inject a query inside a query to perform unauthorized action against database usually SQL based ones.&lt;/p&gt;

&lt;p&gt;Today, I'm not going to give such examples of SQL injection, but one example could be enough to understand how can a developer protect his application from SQL injection.&lt;/p&gt;

&lt;p&gt;Normally this will not take too long, because there only one answer to do that,&lt;br&gt;
&lt;strong&gt;you should use prepared statements.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Prepared statements are send to and parsed by database server separately from any parameters, This way it is impossible for an attacker to inject malicious SQL.&lt;/p&gt;

&lt;p&gt;Basically, SQL injection uses your parameters to in inject malicious SQL queries, as example, let's say you are working on a library website, and you have to get each book by it id to show later in your page, so, maybe you're having something like &lt;code&gt;http://localhost/library/book.php?id=1&lt;/code&gt;, and then you fetch your book as simple as that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="nv"&gt;$id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$_GET&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'id'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nv"&gt;$query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"SELECT * FROM books WHERE id = "&lt;/span&gt;&lt;span class="mf"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;$id&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;//in normal case your query became "SELECT * FROM books WHERE id = 1"&lt;/span&gt;
&lt;span class="c1"&gt;// execute your query and get data&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, you parameter id leaves a security hole in your website, an attackers can easily add SQL query to your parameter id just like that:&lt;br&gt;
&lt;code&gt;localhost/book.php?id=1; UPDATE users SET password = 'anass' WHERE idUser = 1&lt;/code&gt;&lt;br&gt;
Then your code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="nv"&gt;$id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$_GET&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'id'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nv"&gt;$query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"SELECT * FROM books WHERE id = "&lt;/span&gt;&lt;span class="mf"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;$id&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;//this case your query became "SELECT * FROM books WHERE id = 1; UPDATE users SET password = 'anass' WHERE idUser = 1"&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will execute two queries at time, result in changing your administrator password in your application, normally parameters are helping as to do so, the idea behind preventing SQL injection is to separate our query from parameters, so we can tell our database engine that we wanting to execute such query using these parameters.&lt;br&gt;
For sure your website is not ready for production, just leave it in &lt;code&gt;localhost&lt;/code&gt; and keep reading.&lt;/p&gt;

&lt;p&gt;How we can tell database engine, what is the query, and what are parameters,&lt;/p&gt;

&lt;p&gt;first,&lt;br&gt;
Let's connect to our MySQL database, &lt;br&gt;
For sure, commanded way is to use PDO, because if you want to switch to any database engine, we won't rewrite every thing,&lt;br&gt;
Then, should prepared statement, and pass the skeleton of our query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="c1"&gt;//use our database details to connect&lt;/span&gt;
&lt;span class="nv"&gt;$pdo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PDO&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'mysql:dbname=db;host=localhost;charset=utf8'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'root'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'1234'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nv"&gt;$dbConnection&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;PDO&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;ATTR_EMULATE_PREPARES&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nv"&gt;$dbConnection&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;PDO&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;ATTR_ERRMODE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;PDO&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;ERRMODE_EXCEPTION&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first line, is to connect to our MySQL database named &lt;code&gt;db&lt;/code&gt; and hosted in our locale environment &lt;code&gt;localhost&lt;/code&gt;, username is &lt;code&gt;root&lt;/code&gt; and password &lt;code&gt;1234&lt;/code&gt;.&lt;br&gt;
The second line is the most important, tells &lt;code&gt;PDO&lt;/code&gt; to use real prepared statements rather than emulated by PHP, if it set to true preparing will be done by PHP and this is not our choice.&lt;br&gt;
Finally,  the third line set error mode to exceptions, &lt;code&gt;PDO&lt;/code&gt; will throw &lt;code&gt;PDOException&lt;/code&gt; if there is an error. &lt;/p&gt;

&lt;p&gt;Now move on to create our first prepared query,&lt;br&gt;
from the previous example, we need to get a book by it id,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="nv"&gt;$query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$pdo&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;prepare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"SELECT * FROM books WHERE id = ?"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Simple enough&lt;br&gt;
Alright &lt;/p&gt;

&lt;p&gt;We need now to pass the parameter, by using &lt;code&gt;bindParam&lt;/code&gt; method,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$query&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;bindParam&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then use execute to execute our query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$resultes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$query&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hope this clarify how prepared statements work, and encourage you to use it, &lt;br&gt;
If you have any suggestions let me know in comments, and next time i will show you how to make your own secure Query Builder class to use it in any of your projects.&lt;/p&gt;

</description>
      <category>php</category>
      <category>mysql</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
