0

Basically what I'm trying to do is load all the emails that are in a database and insert them into an array. But since printf outputs in length of the string I have no clue how to do it. I know this is probably something very basic but, yeah. I'm kindof new to php. Any idea's?

<?php

    $emails = array();

    mysql_connect("127.0.0.1", "root") or
        die("Could not connect: " . mysql_error());
    mysql_select_db("users");

    $result = mysql_query("SELECT email FROM users");

    while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
        $email = printf("%s", $row[0], $row[1]);
    }

    mysql_free_result($result);
    ?>
4
  • 5
    Every time you use the mysql_ database extension in new code a Kitten is strangled somewhere in the world it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the PDO or mysqli database extensions and prepared statements. Start here Commented Feb 1, 2017 at 17:20
  • 1
    You're doing SELECT email ..., then ... $row[0], $row[1]);. Can you spot the error? You're selecting one column but fetching data of two columns. Commented Feb 1, 2017 at 17:21
  • 1
    The mysql_connect() is missing a parameter so the rest of the code is irrelevant. If you are learning PHP start with PDO dont waste time on the mysql_ extension it is dead Commented Feb 1, 2017 at 17:21
  • It's now time to step into the 21st century. Commented Feb 1, 2017 at 17:24

3 Answers 3

1

Here's a way I would do it in connection with JS. May be useful to you. At first, connect to your database and get the table:

<?php
    $currConnection = new mysqli('127.0.0.1', 'root', 'users') or
        die('Connection failed: ' . $currConnection->connect_error);

    $selection = "SELECT email FORM users";

    $currSel = $currConnection->query($selection);
    $fetchedObj = $currSel->fetch_object();
?>

Now you have your data in an object. Now to the JS part (put this in your .html oder .php file):

<script type='text/javascript'> var email = <?php echo json_encode($fetchedObj); ?>;
</script>

This is a pretty neat way to get your PHP-variables inside JS btw. In JS you have a function that evaluates the array:

var emailInfo = [];

function getEmails(){
    var currentIteration = 0;
    for(var key in email){
        emailInfo[currentIteration] = email[key];
        currentIteration++;
    }
}

I know this is not exactly what you have asked for, but this is a pretty nice way to solve your problem, even if it involes JS and not purely PHP.

Now you can call your emailInfo-array just like you know it.

console.log(emailInfo[0]);
document.getElementById('emailBody').innerHTML = emailInfo[1];
Sign up to request clarification or add additional context in comments.

1 Comment

Also might make sense to emit raw JSON from PHP and read that in with a $.ajax call.
0

This puts each row into a nested array of values for later retrieval:

$email = [];
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
    array_push($email, array($row[0], $row[1]));
}

If you want to retrieve an element, use

 $email[0][0]

for example.

Comments

0

use this code

<?php

    $emails = array();

    mysql_connect("127.0.0.1", "root") or
        die("Could not connect: " . mysql_error());
    mysql_select_db("users");

    $result = mysql_query("SELECT email FROM users");

    while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
        $email[] = $row[0];
    }

    mysql_free_result($result);
 ?>

1 Comment

It's usually best to post only the code that changed, not the whole thing over again with no commentary as to what was fixed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.