{"flag":true,"single":true,"pageTitle":"PHP CRUD functions to extract data from mysql","post":{"id":122,"user_id":"1","slug":"php-crud-functions-to-extract-data-from-mysql-cjns","title":"PHP CRUD functions to extract data from mysql","body":"<p>If you're looking to build a web application, one of the fundamental tasks you'll need to tackle is creating a file that can handle basic CRUD operations. CRUD stands for Create, Read, Update, and Delete, and refers to the four primary tasks that any database application must perform.<\/p>\r\n<p>In this tutorial, we'll walk you through the process of building a basic CRUD file from scratch. Our file will allow users to add, view, edit, and delete records in a database, using PHP and SQL. We'll cover each step of the process in detail, from setting up the database to creating the user interface.<\/p>\r\n<p>By the end of this tutorial, you'll have a fully functioning CRUD file that you can use as a starting point for your own web applications. Whether you're new to programming or an experienced developer, this tutorial is a great way to learn the basics of CRUD operations and get hands-on experience building a web application. So let's get started!<\/p>\r\n<p><strong>Create:<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>$result = $init-&gt;db_create('classfellows',array('roll'=&gt;'123','cnic'=&gt;'3840138476667')));<\/code><\/pre>\r\n<p><strong>Update:<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>$result = $init-&gt;db_update('classfellows',array('class'=&gt;'16'),array('roll'=&gt;'1222222222'));<\/code><\/pre>\r\n<p><strong>Delete:<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>$result = $init-&gt;db_delete('classfellows',array('roll'=&gt;'1222222222'));<\/code><\/pre>\r\n<p><strong>Select:<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>$result = $init-&gt;db_get('classfellows',array('roll'=&gt;'123'),50);<\/code><\/pre>\r\n<p><strong>CODE FILE<\/strong><\/p>\r\n<p>JUST IMPORT THE BELOW CODE AND USE ITS FUNCTIONS<\/p>\r\n<pre class=\"language-markup\"><code>&lt;?php\r\n$servername = \"localhost\";\r\n$username = \"root\";\r\n$password = \"\";\r\n$db = \"top4u\";\r\n$conn = new mysqli($servername, $username, $password, $db); \/\/ Create connection\r\n\r\nif ($conn-&gt;connect_error) { \/\/ Check connection\r\n    die(\"Connection failed: \" . $conn-&gt;connect_error);\r\n}\r\n\r\n\/\/ Function to add quotes around the values for the SQL query\r\nfunction addQuote($v) {\r\n    return \"'\" . $v . \"'\";\r\n}\r\n\r\nclass Index {\r\n    function __construct($conn) {\r\n        $this-&gt;conn = $conn;\r\n    }\r\n\r\n    \/\/ Function to retrieve data from the database\r\n    public function db_get($table = null, $where = null, $limit = 50) {\r\n        if ($table == null) {\r\n            $error = json_encode(array(\"result\" =&gt; false, \"message\" =&gt; \"table can't be empty\"));\r\n            return $error;\r\n        }\r\n\r\n        $sql = \"SELECT * FROM $table \";\r\n        if ($where) {\r\n            $sql .= \"WHERE \";\r\n            foreach ($where as $key =&gt; $value) {\r\n                $sql .= \" $key = '$value' AND\";\r\n            }\r\n        }\r\n        $sql = rtrim($sql, \"AND\");\r\n        $sql .= \" LIMIT $limit\";\r\n\r\n        $records = array();\r\n        $result = $this-&gt;conn-&gt;query($sql);\r\n        if ($result-&gt;num_rows &gt; 0) {\r\n            while ($row = $result-&gt;fetch_assoc()) {\r\n                $records[] = $row;\r\n            }\r\n            return $records;\r\n        } else {\r\n            $error = json_encode(array(\"result\" =&gt; false, \"message\" =&gt; \"No record found with command $sql\"));\r\n            return $error;\r\n        }\r\n    }\r\n\r\n    \/\/ Function to insert data into the database\r\n    public function db_create($table = null, $array = null) {\r\n        if ($table == null) {\r\n            $error = json_encode(array(\"result\" =&gt; false, \"message\" =&gt; \"table can't be empty\"));\r\n            return $error;\r\n        } elseif (!is_array($array)) {\r\n            $error = json_encode(array(\"result\" =&gt; false, \"message\" =&gt; \"Array is invalid \"));\r\n            return $error;\r\n        }\r\n        $columns = implode(\",\", array_keys($array));\r\n        $escaped_values = array_map(\"addQuote\", array_values($array));\r\n        $values = implode(',', $escaped_values);\r\n        $sql = \"INSERT INTO $table ($columns) VALUES ($values)\";\r\n\r\n        if ($this-&gt;conn-&gt;query($sql)) {\r\n            return true;\r\n        } else {\r\n            return false;\r\n        }\r\n    }\r\n\r\n    \/\/ Function to update data in the database\r\n    public function db_update($table = null, $array = null, $where = null) {\r\n        if ($table == null) {\r\n            $error = json_encode(array(\"result\" =&gt; false, \"message\" =&gt; \"table can't be empty\"));\r\n            return $error;\r\n        } elseif (!is_array($array)) {\r\n            $error = json_encode(array(\"result\" =&gt; false, \"message\" =&gt; \"Array is invalid \"));\r\n            return $error;\r\n        } elseif (!is_array($where) || empty($where)) {\r\n            $error = json_encode(array(\"result\" =&gt; false, \"message\" =&gt; \"Where Array is invalid \"));\r\n            return $error;\r\n        }\r\n\r\n        $sql = \"UPDATE $table SET \";\r\n        foreach ($array as $key =&gt; $value) {\r\n            $sql.=\" $key = '$value' ,\";\r\n        }\r\n        $sql = rtrim($sql,\",\");\r\n        $sql .= \" WHERE \";\r\n        foreach ($where as $key =&gt; $value) {\r\n            $sql .= \" $key = '$value' AND\";\r\n        }\r\n        $sql = rtrim($sql, \"AND\");\r\n        if ($this-&gt;conn-&gt;query($sql)) {\r\n            return true;\r\n        } else {\r\n            return false;\r\n        }\r\n    }\r\n\r\n    \/\/ Function to delete data from the database\r\n    public function db_delete($table = null, $where = null) {\r\n        if ($table == null) {\r\n            $error = json_encode(array(\"result\" =&gt; false, \"message\" =&gt; \"table can't be empty\"));\r\n            return $error;\r\n        } elseif (!is_array($where) || empty($where)) {\r\n            $error = json_encode(array(\"result\" =&gt; false, \"message\" =&gt; \"Where Array is invalid \"));\r\n            return $error;\r\n        }\r\n\r\n        $sql = \"DELETE FROM $table \";\r\n        $sql .= \"WHERE \";\r\n        foreach ($where as $key =&gt; $value) {\r\n            $sql .= \" $key = '$value' AND\";\r\n        }\r\n        $sql = rtrim($sql, \"AND\");\r\n        if ($this-&gt;conn-&gt;query($sql)) {\r\n            return true;\r\n        } else {\r\n            return false;\r\n        }\r\n    }\r\n\r\n    \/\/ Function to run custom SQL queries\r\n    public function wp_db_query($sql) {\r\n        $results = $this-&gt;conn-&gt;query($sql);\r\n        if ($results-&gt;num_rows &gt; 0) {\r\n            return $results;\r\n        } else {\r\n            return false;\r\n        }\r\n    }\r\n}\r\n\r\n$init = new Index($conn);\r\n\r\n\/\/$result = $init-&gt;db_get('classfellows',array('roll'=&gt;'123'),50);\r\n\r\n\/\/$result = $init-&gt;db_create('classfellows',array('roll'=&gt;'123','cnic'=&gt;'3840138476667')));\r\n\r\n\/\/$result = $init-&gt;db_update('classfellows',array('class'=&gt;'16'),array('roll'=&gt;'1222222222'));\r\n\r\n\/\/$result = $init-&gt;db_delete('classfellows',array('roll'=&gt;'1222222222'));<\/code><\/pre>","category_id":"1","is_private":"0","created_at":"2023-04-18T00:28:44.000000Z","updated_at":"2023-04-18T00:28:44.000000Z","category":{"id":1,"user_id":"1","name":"PHP","slug":"php-3ius","parent_id":null,"created_at":"2023-03-14T03:58:19.000000Z","updated_at":"2023-03-14T03:58:19.000000Z"},"user":{"id":1,"name":"R GONDAL","email":"rizikmw@gmail.com","email_verified_at":null,"two_factor_confirmed_at":null,"current_team_id":"1","profile_photo_path":null,"created_at":"2023-03-12T10:49:33.000000Z","updated_at":"2025-01-10T12:59:00.000000Z","profile_photo_url":"https:\/\/ui-avatars.com\/api\/?name=R+G&color=7F9CF5&background=EBF4FF"}},"pageDesc":"If you're looking to build a web application, one of the fundamental tasks you'll need to tackle is creating a file that can handle basic CR - PHP CRUD functions to extract data from mysql (Updated: April 18, 2023) - Read more about PHP CRUD functions to extract data from mysql at my programming site [SITE]","categories":[]}