0

i want to fetch a mysql table named "my_table" column named "Email" contents as an array by php , so this is my code :

<?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "my_table";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    $result = mysql_query("SELECT * FROM my_table");

    if ($result === FALSE) {
        die(mysql_error()); // TODO: better error handling
    }

    $data = array();
    while ($row = mysql_fetch_array($result)) {
        $data[] = $row['Email'];
    }
    echo join($data, ',');
?>

but this code returns me this error : No database selected

but i've selected my table and database ...

and i know this code have some problems as mixing mysql and mysqli content but i dont know how to fix it i just want that array echo , if this code need to be fixed just guid me , how to solve this problem ? thanks in advance Thanks to @Martin my problem has solved i just changed the code by this way :

<?php
$servername = "localhost"; $username = "root"; $password = ""; $dbname = "my_db"; 
// Create connection
 $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection 
if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error);}
$result = mysqli_query($conn, "SELECT * FROM my_table");
if($result === FALSE) { 
die(mysql_error()); // TODO: better error handling
}


$data = array();
while ($row = mysqli_fetch_array($result))
{
$data[] = $row['Email'];
}
echo join($data, ',')
?>
Sajad Asadi
  • 97
  • 2
  • 14

1 Answers1

-2

You are conencting to a database here called "my_table":

$dbname = "my_table";

And then, in your SQL statement, you try connecting to a table called the same:

$result = mysql_query("SELECT * FROM my_table");

Are you sure this is the correct name for your database?

On PHPMyAdmin you can click "Databases" to view the Database names and then, when clicking on the db, it will give you a list of tables:

Image file of getting database views from tables in PHPMyAdmin

Martin
  • 22,212
  • 11
  • 70
  • 132
joshnik
  • 438
  • 1
  • 5
  • 15