Jump to content

Tikkie

New Members
  • Posts

    6
  • Joined

  • Last visited

Tikkie's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Thanks for your answer. At this moment i'm very confused and having many questions. First I will read some more about classes and PDO to understand what the best solution is to put multiple query's in my code.
  2. Thanks for your answers. The best result I can get at this moment is when I put the query in de connection.php. I can't get this working in the functions, always get an error on ->prepare Now my connection.php looks like this: <?php class dbConnect { public function connect($id, $column){ try{ $pdo = new PDO ("mysql:host=localhost;dbname=database", 'root', ''); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $query = $pdo->prepare('SELECT * FROM users WHERE id = :id'); $query->bindParam(':id', $id); $query->execute(); while($row = $query->fetch()) { echo ($row[$column]);} } catch(PDOException $e) { echo 'ERROR: ' . $e->getMessage(); } } } ?> and functions.php looks like this: <?php require_once ('connection.php'); function getUserName($id, $column){ $pdo = new dbConnect(); $pdo->connect($id, $column); } $id = '1'; $column = 'username'; getUserName($id, $column); function getFullName($id, $column){ $pdo = new dbConnect(); $pdo->connect($id, $column); } $id = '1'; $column = 'fullname'; getUserName($id, $column); ?> It makes me able to create functions for returning every seperate column. Is this the best way to do it? I think it also would be better if I just query on the column, so user "username" in stead of *. But when I replace the * for :column and bindParam :column it only shows me "username" of "fullname" in stead of my name.
  3. I've just started with classes and mysql with PDO but having trouble to understand it correctly. I established a working connection in connection.php with the following code: class dbConnect { public function connect(){ try{ $conn = new PDO ("mysql:host=localhost;dbname=database", 'root', ''); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(PDOException $e) { echo 'ERROR: ' . $e->getMessage(); } } } Now what I want to do is create a function in functions.php to execute query's, but this is not working: require_once ('connection.php'); function getUserId($name){ $query = $conn->prepare('SELECT id FROM users WHERE name = :name'); $query->bindParam(':name', $name); $query->execute(); while($row = $query->fetch()) { echo($row[0]);} } It gives me the following errors: Notice: Undefined variable: conn in C:\xampp\htdocs\d\functions.php on line 8 Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\d\functions.php on line 8 I know i can't call variables from functions unless if it's a global variable. I tried so many things but nothing seems to help me. Can somebody give me a simple solution?
  4. Are you sure you selected a table on the database connection? If you don't do this there will be no error but also no results.
  5. Thank you so much! I would never have searched there!
  6. I'm new to PHP and working on a mailform with phpmailer with attachment. Everything works fine but the received mail keeps having my Gmail address in the from header. The AddReplyTo address works fine but I don't want to show the gmail address. This is what my code looks like: require('C:\xampp\htdocs\PHPMailer\class.phpmailer.php'); $mail = new PHPMailer(); // create a new object $mail->IsSMTP(); // enable SMTP $mail->SMTPDebug = 2; // debugging: 1 = errors and messages, 2 = messages only $mail->SMTPAuth = true; // authentication enabled $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail $mail->Host = 'smtp.gmail.com'; $mail->Port = 465; $mail->Username = 'myaccount@gmail.com'; $mail->Password = 'mypassword'; $mail->SetFrom('e-mail@address.com', 'email@address.com'); $mail->AddReplyTo('e-mail@address.com', 'e-mail@address.com'); $mail->AddAddress("myaccount@gmail.com", "MyAccount"); $mail->Subject = "TestMail"; $mail->Body = "This is the message body"; $mail->AddAttachment("test.txt", "test.txt"); // optional name if(!$mail->Send()) { echo "Error"; exit; } else { echo "OK"; } I only want to use the gmail smtp but want to show another e-mail address (e-mail@address.com in thie example) in the header. But it keeps showing myaccount@gmail.com. Also in the class.phpmailer.php I've configurated the $From, $FromName and $Sender as e-mail@address.com but this doesn't work. Can somebody help me with this?
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.