php mysql and pdo

Thread Solved

Join Date: Jun 2009
Posts: 140
Reputation: whiteyoh is an unknown quantity at this point 
Solved Threads: 2
whiteyoh whiteyoh is offline Offline
Junior Poster

php mysql and pdo

 
0
  #1
Oct 22nd, 2009
Hi all,

I have a pdo connection script to mysql, which works fine. What im a little stuck on is that when i "include" the file, its connected, but how do i call the table to which my query is related to?

The first snippet is the connection, and the second is the insert file.

Im not new to PHP, but i am to OO.

  1. <?php
  2.  
  3. /*** mysql hostname ***/
  4. $hostname = 'localhost';
  5.  
  6. /*** mysql username ***/
  7. $username = 'root';
  8.  
  9. /*** mysql password ***/
  10. $password = 'root';
  11.  
  12. try {
  13. $dbh = new PDO("mysql:host=$hostname;dbname=mysql", $username, $password);
  14. /*** echo a message saying we have connected ***/
  15. echo 'Connected to database';
  16. }
  17. catch(PDOException $e)
  18. {
  19. echo $e->getMessage();
  20. }
  21. ?>

and
  1. <?php
  2.  
  3. require("includes/connectpdomysql.php");
  4.  
  5. if (isset($_POST['username']) && isset($_POST['password']) && isset($_POST['email']))
  6.  
  7.  
  8. {
  9.  
  10. //Prevent SQL injections
  11.  
  12. $username = mysql_real_escape_string($_POST['username']);
  13.  
  14. $email = mysql_real_escape_string($_POST['email']);
  15.  
  16.  
  17.  
  18.  
  19.  
  20. //Get MD5 hash of password
  21.  
  22. $password = md5($_POST['password']);
  23.  
  24.  
  25.  
  26. //Check to see if username exists
  27.  
  28. $sql = mysql_query("SELECT username FROM user WHERE username = 'username'");
  29.  
  30. If (mysql_num_rows($sql = 0))
  31. {
  32.  
  33. die ("Username taken.");
  34.  
  35. }
  36.  
  37. mysql_query("INSERT INTO users (username, password, email) VALUES ( '$username', '$password', '$email')") or die (mysql_error()); echo "Account created.";
  38.  
  39.  
  40.  
  41. }
  42. ?>
  43.  
  44.  
  45.  
  46. <html></html>
  47.  
  48. <form action="newuser.php" method="post">
  49.  
  50. Username: <input name="username" type="text" />
  51.  
  52. Password: <input type="password" name="password" />
  53. Email: <input name="email" type="text" />
  54.  
  55. <input type="submit" value="Submit" />
  56.  
  57. </form>
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,402
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 225
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey
 
0
  #2
Oct 22nd, 2009
What do you mean by calling the table? Also, why're you using PDO for the connection then just completely ignoring it and using mysql_query anyway? http://php.net/pdo
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 140
Reputation: whiteyoh is an unknown quantity at this point 
Solved Threads: 2
whiteyoh whiteyoh is offline Offline
Junior Poster
 
0
  #3
Oct 22nd, 2009
i am an idiot. thank you for your help.....

im learning php5 oo and java at the same time. my head is a little mangled
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC