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);
    ?>
Aaron Fox
  • 33
  • 4
  • 5
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** 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](http://php.net/manual/en/book.pdo.php) – RiggsFolly Feb 01 '17 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. – Rajdeep Paul Feb 01 '17 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 – RiggsFolly Feb 01 '17 at 17:21
  • It's now time to step into the 21st century. – Funk Forty Niner Feb 01 '17 at 17:24

3 Answers3

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];
eetschel
  • 46
  • 1
  • 5
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.

Loveen Dyall
  • 824
  • 2
  • 8
  • 20
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);
 ?>
Think Big
  • 1,021
  • 14
  • 24
  • 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. – tadman Feb 01 '17 at 18:30