· 9 years ago · Jul 29, 2017, 09:44 PM
1
2 <?php
3
4 $hostname = 'localhost'; // Self Explanatory
5 $username = 'root'; // Self Explanatory
6 $password = ''; // Self Explanatory
7
8 try {
9 $dbh = new PDO("mysql:host=$hostname;dbname=DBNAMEHERE", $username, $password);
10 // echo 'Connected to database<br />'; // This is just for testing, you might
11 // want to run it at the start to check you are connecting, but you should be.
12 $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
13 // This is just a simple error handler, it sends you a pretty decent error message
14 // Should something go wrong, easy to follow and get help with lad so keep it.
15
16 /* This is pretty much your
17 $a = "SELECT x FROM x WHERE x = x";
18 $a1 = mysql_query($a) or die ("Error" . mysql_error());
19 $a2 = mysql_fetch_array($a1);
20 And as it happens the while is pretty much your while($a2 = mysql_fetch_array(a1)) { Data };
21 */
22 $sth = $dbh->query('SELECT firstName, lastName, userName, ID from psgb_users WHERE ID >= 0');
23 $sth->setFetchMode(PDO::FETCH_OBJ);
24 $saved = $sth->fetch(PDO::FETCH_OBJ); // This is used to call a variable elsewhere, so you
25 // will use $saved->FIELD; instead of $a2['FIELD'];
26 $sth->execute();
27
28 echo '<table><tr>';
29 while($row = $sth->fetch())
30 { // It's only in a table so it's clearer.
31 echo '<td>' . $row->firstName . '</td>';
32 echo '<td>' . $row->lastName . "</td>";
33 echo '<td>' . $row->ID . "</td>";
34 }
35 echo '</tr></table>';
36
37 /* This next part is a JOIN, use it for whatever it is you need to use it for
38 in your project just don't forget to change the SELECT statement.
39 In the SELECT you can obviously specify what you want to SELECT, e.g
40 Louis_table.ID , Louis_Table.firstName but make sure it's that format.
41
42 Don't forget to alter the WHERE statement too with your selector be it
43 >= <= = == != or whatever, just change it if you don't need to specify >=
44
45 ORDER BY is optional you don't need that, you might want to add a LIMIT 1
46 unless you specify a WHERE that is your Primary Key, jus'sayin!
47
48 */
49 $stmt = $dbh->prepare("
50 SELECT YOURTABLE1.FIELD
51 FROM YOURTABLE.FIELD LEFT JOIN YOURTABLE.FIELD ON ANOTHERTABLE.FIELD = YOURTABLE.FIELD
52 WHERE YOURTABLE.FIELD >= ?
53 ORDER BY YOURTABLE.FIELD asc");
54 $stmt->bindValue(1, PDO::PARAM_INT); // This must be specified should you be grabbing
55 // An integer from the database, but it's also how you specify the value of '?' in the
56 // SELECT statement, it's an immunity to SQL Injection amongst other things.
57 // As it happens if you're coming from a form with values to specify or a $_SESSION,
58 // You might want to do something like $var = $_SESSION['somesession']; then use it in
59 // the above bind, like $stmt->bindValue(1, $var, PDO::PARAM_INT); That will specify it
60 // to your particular query, and value, though you can just do it 1, 1, etc.
61 $stmt->setFetchMode(PDO::FETCH_NUM);
62 $stmt->execute(); // Don't forget to call an execute on the $x else you'll see some slip
63 // Ups later on in your code and running.
64
65 foreach ($stmt as $row)
66 {
67 echo $row[0] . ' ' . $row[1] . ' ' . $row[2] . '<br />';
68 }
69
70 $dbh = null; // This terminates the DB connection straight away.
71 }
72 catch(PDOException $e)
73 {
74 echo $e->getMessage();
75 // This is just the error report, if it finds an error it catches and lets you know, simple.
76 }
77 ?>
78
79--
80-- Table structure for table `ANOTHERTABLE`
81--
82
83 CREATE TABLE IF NOT EXISTS `ANOTHERTABLE` (
84 `ID` mediumint(8) NOT NULL AUTO_INCREMENT,
85 `firstName` varchar(25) NOT NULL,
86 `lastName` varchar(25) NOT NULL,
87 `userName` varchar(25) NOT NULL,
88 PRIMARY KEY (`ID`)
89) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
90
91--
92-- Dumping data for table `ANOTHERTABLE`
93--
94
95 INSERT INTO `ANOTHERTABLE` (`ID`, `firstName`, `lastName`, `userName`) VALUES
96 (1, 'Sean', 'Keenan', 'seanKeenan'),
97 (2, 'Patrick', 'Lagan', 'patrickLagan'),
98 (3, 'Louis', 'Anderson', 'louisAnderson'),
99 (4, 'Dan', 'Hopkins', 'danHopkins'),
100 (5, 'Thomas', 'Fullerton', 'thomasFullerton'),
101 (6, 'Dheyton', 'Clements', 'dheytonClements'),
102 (7, 'Gary', 'Hipkiss', 'garyHipkiss'),
103 (8, 'Kevin', 'McIntyre', 'kevinMcIntyre'),
104 (9, 'Arthur', 'New', 'arthurNew');
105
106--
107-- Table structure for table `YOURTABLE`
108--
109
110CREATE TABLE IF NOT EXISTS `YOURTABLE` (
111 `ID` int(5) NOT NULL AUTO_INCREMENT,
112 `description` varchar(200) NOT NULL,
113 `sellerID` int(5) NOT NULL,
114 PRIMARY KEY (`ID`)
115) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
116
117--
118-- Dumping data for table `YOURTABLE`
119--
120
121INSERT INTO `YOURTABLE` (`ID`, `description`, `sellerID`) VALUES
122(1, 'Some Description about this item', 1),
123(2, 'Some more description about an item', 2);