-2

When someone joins a group, i want the creator of the group to get an email. So I query the database for the group with that group name and get the email of the group creator.

sql = "SELECT `groupcreatoremail` FROM `userchurch_signup` WHERE `groupname` = '". $groupname . "' limit 1" ;
$result = $conn->query($sql);
$creator_email = "";
while ($fieldinfo=mysqli_fetch_field($result)) {
    $creator_email = $fieldinfo->groupcreatoremail;
}

Based on testing I'm not getting anything even though if i run the SQL statement it works.

Duck of Death
  • 27
  • 1
  • 5
  • 1
    Your script has no code that outputs anything. What do you expect to happen? – Quentin Feb 07 '18 at 16:58
  • Do you know that `$result` and `$result2` are __different__ vars? – u_mulder Feb 07 '18 at 16:58
  • 2
    This looks like it is probably **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that you need to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from – Quentin Feb 07 '18 at 16:59
  • Yup, this is not the whole script, just the part where I'm trying to get the email. Don't worry about injection, I'm not posting the whole script. – Duck of Death Feb 07 '18 at 17:07
  • Thank you for your concern though Quentin. – Duck of Death Feb 07 '18 at 17:08
  • Looks like this isn't possible. I'm just going to have to break database rules and store the creator email in the join table I guess. Yay me! – Duck of Death Feb 07 '18 at 17:10
  • Why are you mixing the object-oriented style and the extremely verbose procedural one? This code is highly inconsistent and could use some careful attention. As with everything in programming the details matter. – tadman Feb 07 '18 at 17:18
  • $fieldinfo['groupcreatoremail'] works. Thans for nuthin! – Duck of Death Feb 07 '18 at 17:32
  • @DuckofDeath my way works too, thanks for not trying! :) – mcv Feb 07 '18 at 18:13

1 Answers1

-1

Referenced fetch_assoc about this simple issue. I would use their suggested method. however if you want to know more about the way you selected research on the function you're using.

More info can be found here how to use this information mysqli_fetch_field

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "result: " . $row["groupcreatoremail"]. "<br>";
    }
} else {
    echo "0 results";
}
mcv
  • 1,380
  • 3
  • 16
  • 41
  • You'll note that that is the exact method I'm using. – Duck of Death Feb 07 '18 at 17:10
  • nope i'm using fetch_assoc, you're using mysqli_fetch_field in your while statement – mcv Feb 07 '18 at 17:11
  • Also I would highly recommend using PHP::PDO for database connections. Learn how to bind parameters versus your string concats above for your query to avoid SQL Injections. – mcv Feb 07 '18 at 17:14
  • if you look to the bottom, the mysqli_fetch_field link, you'll see it returns attributes of the field but value is neither of the attributes. – mcv Feb 07 '18 at 17:17