Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $hostname = 'localhost'; // Self Explanatory
- $username = 'root'; // Self Explanatory
- $password = ''; // Self Explanatory
- try {
- $dbh = new PDO("mysql:host=$hostname;dbname=DBNAMEHERE", $username, $password);
- // echo 'Connected to database<br />'; // This is just for testing, you might
- // want to run it at the start to check you are connecting, but you should be.
- $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- // This is just a simple error handler, it sends you a pretty decent error message
- // Should something go wrong, easy to follow and get help with lad so keep it.
- /* This is pretty much your
- $a = "SELECT x FROM x WHERE x = x";
- $a1 = mysql_query($a) or die ("Error" . mysql_error());
- $a2 = mysql_fetch_array($a1);
- And as it happens the while is pretty much your while($a2 = mysql_fetch_array(a1)) { Data };
- */
- $sth = $dbh->query('SELECT firstName, lastName, userName, ID from psgb_users WHERE ID >= 0');
- $sth->setFetchMode(PDO::FETCH_OBJ);
- $saved = $sth->fetch(PDO::FETCH_OBJ); // This is used to call a variable elsewhere, so you
- // will use $saved->FIELD; instead of $a2['FIELD'];
- $sth->execute();
- echo '<table><tr>';
- while($row = $sth->fetch())
- { // It's only in a table so it's clearer.
- echo '<td>' . $row->firstName . '</td>';
- echo '<td>' . $row->lastName . "</td>";
- echo '<td>' . $row->ID . "</td>";
- }
- echo '</tr></table>';
- /* This next part is a JOIN, use it for whatever it is you need to use it for
- in your project just don't forget to change the SELECT statement.
- In the SELECT you can obviously specify what you want to SELECT, e.g
- Louis_table.ID , Louis_Table.firstName but make sure it's that format.
- Don't forget to alter the WHERE statement too with your selector be it
- >= <= = == != or whatever, just change it if you don't need to specify >=
- ORDER BY is optional you don't need that, you might want to add a LIMIT 1
- unless you specify a WHERE that is your Primary Key, jus'sayin!
- */
- $stmt = $dbh->prepare("
- SELECT YOURTABLE1.FIELD
- FROM YOURTABLE.FIELD LEFT JOIN YOURTABLE.FIELD ON ANOTHERTABLE.FIELD = YOURTABLE.FIELD
- WHERE YOURTABLE.FIELD >= ?
- ORDER BY YOURTABLE.FIELD asc");
- $stmt->bindValue(1, PDO::PARAM_INT); // This must be specified should you be grabbing
- // An integer from the database, but it's also how you specify the value of '?' in the
- // SELECT statement, it's an immunity to SQL Injection amongst other things.
- // As it happens if you're coming from a form with values to specify or a $_SESSION,
- // You might want to do something like $var = $_SESSION['somesession']; then use it in
- // the above bind, like $stmt->bindValue(1, $var, PDO::PARAM_INT); That will specify it
- // to your particular query, and value, though you can just do it 1, 1, etc.
- $stmt->setFetchMode(PDO::FETCH_NUM);
- $stmt->execute(); // Don't forget to call an execute on the $x else you'll see some slip
- // Ups later on in your code and running.
- foreach ($stmt as $row)
- {
- echo $row[0] . ' ' . $row[1] . ' ' . $row[2] . '<br />';
- }
- $dbh = null; // This terminates the DB connection straight away.
- }
- catch(PDOException $e)
- {
- echo $e->getMessage();
- // This is just the error report, if it finds an error it catches and lets you know, simple.
- }
- ?>
- --
- -- Table structure for table `ANOTHERTABLE`
- --
- CREATE TABLE IF NOT EXISTS `ANOTHERTABLE` (
- `ID` mediumint(8) NOT NULL AUTO_INCREMENT,
- `firstName` varchar(25) NOT NULL,
- `lastName` varchar(25) NOT NULL,
- `userName` varchar(25) NOT NULL,
- PRIMARY KEY (`ID`)
- ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
- --
- -- Dumping data for table `ANOTHERTABLE`
- --
- INSERT INTO `ANOTHERTABLE` (`ID`, `firstName`, `lastName`, `userName`) VALUES
- (1, 'Sean', 'Keenan', 'seanKeenan'),
- (2, 'Patrick', 'Lagan', 'patrickLagan'),
- (3, 'Louis', 'Anderson', 'louisAnderson'),
- (4, 'Dan', 'Hopkins', 'danHopkins'),
- (5, 'Thomas', 'Fullerton', 'thomasFullerton'),
- (6, 'Dheyton', 'Clements', 'dheytonClements'),
- (7, 'Gary', 'Hipkiss', 'garyHipkiss'),
- (8, 'Kevin', 'McIntyre', 'kevinMcIntyre'),
- (9, 'Arthur', 'New', 'arthurNew');
- --
- -- Table structure for table `YOURTABLE`
- --
- CREATE TABLE IF NOT EXISTS `YOURTABLE` (
- `ID` int(5) NOT NULL AUTO_INCREMENT,
- `description` varchar(200) NOT NULL,
- `sellerID` int(5) NOT NULL,
- PRIMARY KEY (`ID`)
- ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
- --
- -- Dumping data for table `YOURTABLE`
- --
- INSERT INTO `YOURTABLE` (`ID`, `description`, `sellerID`) VALUES
- (1, 'Some Description about this item', 1),
- (2, 'Some more description about an item', 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement