Using a DB
1 post
Page 1 of 1
In this guide I'm going to show you how to use PDO to connect to your DB and manipulate data etc.
We will initiate the connection through the PDO class using $db = new PDO("dbtype;dbname=dbnamehere", "db_user", "db_pass")
Initiate the connection
$db = new PDO("mysql;dbname=testdb", "db_user", "db_pass");
Now, let's insert some data!
We will use the command: INSERT INTO. The syntax for this is: INSERT [INTO] table [(column1, column2, ...)] VALUES|VALUE(info1, info2, ...)
$db->query("INSERT INTO example (name, age) VALUES('Timmy Mellowman', '23' ) ");
$db->query("INSERT INTO example (name, age) VALUES('Joe Bloggs', '22' ) ");
$db->query("INSERT INTO example (name, age) VALUES('John Smith', '52' ) ");
Now, we want to pull data from the table, so, using $db->query again, we're going to select a table.
Syntax: SELECT [column1, column2, ...] FROM table [WHERE column1=blah [AND column2=blah [OR column3=blah]]]
and then we will make the data useable with $db->fetchObject($table);
$query = $db->query("SELECT FROM example");
while($info = $db->fetchObject($query)){
//call the info
echo($info->name."<br>");
}
That's all for now folks, Thanks, Rocky4126
We will initiate the connection through the PDO class using $db = new PDO("dbtype;dbname=dbnamehere", "db_user", "db_pass")
Initiate the connection
$db = new PDO("mysql;dbname=testdb", "db_user", "db_pass");
Now, let's insert some data!
We will use the command: INSERT INTO. The syntax for this is: INSERT [INTO] table [(column1, column2, ...)] VALUES|VALUE(info1, info2, ...)
$db->query("INSERT INTO example (name, age) VALUES('Timmy Mellowman', '23' ) ");
$db->query("INSERT INTO example (name, age) VALUES('Joe Bloggs', '22' ) ");
$db->query("INSERT INTO example (name, age) VALUES('John Smith', '52' ) ");
Now, we want to pull data from the table, so, using $db->query again, we're going to select a table.
Syntax: SELECT [column1, column2, ...] FROM table [WHERE column1=blah [AND column2=blah [OR column3=blah]]]
and then we will make the data useable with $db->fetchObject($table);
$query = $db->query("SELECT FROM example");
while($info = $db->fetchObject($query)){
//call the info
echo($info->name."<br>");
}
That's all for now folks, Thanks, Rocky4126

1 post
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023