0

I want to get a Firstname (Voornaam), and Lastname (Achternaam) from my database with a specific ID.

And I want to put it in my a function.

I made the following function in functions.php:

<?php

/* Naam opvragen */
include('gegevens.php');
function getName($getID)
{
    $getname = 'SELECT * FROM KlantGegevens WHERE ID = ' .  $getID;
    $query = $conn->query($getname);
     while($show = $query->fetch_assoc()) {

         $voornaam = $show["Voornaam"];
         return $voornaam;
     }
}
/* Eind naam opvragen */

?>

And i call the function with ($getID (=1)):

<?php getName($getID); ?>

My error is:

Fatal error: Call to a member function query() on a non-object in /home/thijsgp51/domains/thijskempers.nl/public_html/beheer/functions/functions.php on line 8

What am i doing wrong here?

Thijs Kempers
  • 459
  • 4
  • 17

5 Answers5

0
<?php

    /* Naam opvragen */
    function getName($getID)
    {
        $db = new PDO('mysql:host=localhost;port=3307;dbname=test', 'root', 'usbw');
        $stmt = $db->prepare("SELECT * FROM klantgegevens WHERE ID ='$getID' ");
        $stmt->execute();
         while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

             $voornaam = $row["voornaam"];
             return $voornaam;
         }
    }

    $id = 1;
    echo getName($id);

?>

I used PDO for the connection try it. Also read this post on sqlinjection very helpful.

In the code i changed your query a bit and put the connection in the function like Till Helge said.

Happy coding!

Community
  • 1
  • 1
Michel
  • 13
  • 5
0

Try It.

<?php

    /* Naam opvragen */
    function getName($getID)
    {
        $db = new PDO('mysql:host=localhost;port=3307;dbname=test', 'root', 'usbw');
        $stmt = $db->prepare("SELECT * FROM klantgegevens WHERE ID ='%s",$getID);
        $stmt->execute();
         while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

             $voornaam = $row["voornaam"];
             return $voornaam;
         }
    }

    $id = 1;
    echo getName($id);

?>
helb
  • 7,039
  • 8
  • 34
  • 56
-1

Change your function definition, remove those 2 arguments, and change concatenation from + to .:

function getName()//or with optional arguments: function getName($voornaam='', $achternaam='')
{

    $conn = new mysqli('localhost', 'user', 'pass', 'database');
    $getID = 1;
    $getname = 'SELECT Voornaam, Achternaam FROM KlantGegevens WHERE ID = ' .  $getID;
    $query = $conn->query($getname);
     while($row = $query->fetch_assoc()) {

         $voornaam = $row["Voornaam"];
         $achternaam = $row["Achternaam"];

         return $voornaam.' '.$achternaam;
     }
}

You don't need name and forename in function definition parameters because you get it from database.

As Till Helge pointed out, you still need to open connection to database - either put it as an argument, or call as the first thing inside your function (I have already put that there after edition):

$conn = new mysqli('localhost', 'user', 'pass', 'database');

n-dru
  • 9,039
  • 2
  • 25
  • 39
  • You didn't take into account, that the database connection is not available in the function scope. – Till Helge Mar 19 '15 at 08:29
  • @TillHelge Yes, I didn't, shame on me... I already edited, thanks – n-dru Mar 19 '15 at 08:35
  • Thanks, i made my question a little clearer, maybe it's easier now? – Thijs Kempers Mar 19 '15 at 08:49
  • And I made my answer clearer by adding `$conn = new mysqli('localhost', 'user', 'pass', 'database');` - you need to open your connection to database (with your hostname, username, pasword and db name of course), or pass it as an argument to the function. – n-dru Mar 19 '15 at 08:51
  • I have opened my connection in gegevens.php which i include – Thijs Kempers Mar 19 '15 at 08:55
  • include it inside the function, or pass it in the function's parameter: `getName($getID,$conn);` – n-dru Mar 19 '15 at 08:58
-1

Your funcion get 2 arguments

function getName($voornaam, $achternaam)

you should remove the areguments as you are not using it.

function getName() {
   ....
}
Christian
  • 644
  • 7
  • 11
-3

you didn't pass parameters in your function,

<?php getName(); ?>
Ayyanar G
  • 1,560
  • 1
  • 10
  • 20
  • Wow...you really want this reputation, hm? You didn't even provide a **correct** code example, but just copy and pasted the wrong line. That's pretty...disturbing. – Till Helge Mar 19 '15 at 08:29
  • I pasted correct line only, i'm i wrong, he didn't pass arguments in getName(), i mentioned his error that's what i did, its not about answering, its about pointing his error – Ayyanar G Mar 19 '15 at 08:32
  • Thanks, i made my question a little clearer, maybe it's easier now? – Thijs Kempers Mar 19 '15 at 08:49
  • @helb This looks like an attempted answer to me. – Duncan Jones Mar 19 '15 at 12:08
  • @Duncan Ayyanar G said it himself: "its not about answering, its about pointing his error". – helb Mar 19 '15 at 12:10