0

I've been attempting to echo a mysqli query that selects all the data in the table from a specific field yet currently my code will not print anything on the screen, I also receive no indication of any error from within the console :/

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

$conn = mysqli_connect($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$result = mysqli_query($conn, "SELECT field_name, FROM table_name");
print_r($result);
?>

I have already previously attempted to echo $result rather than print_r yet neither work so I'm not entirely sure what's wrong. Its my first time using phpMyAdmin :/

  • 2
    You have a syntax error in your sql query, i.e: the comma after `field_name`. This will cause a 500 error, hence the blank screen. Try enable error report on your PHP script. – catcon Jul 13 '21 at 01:35

1 Answers1

0

when using mysqli you should read result by mysqli_fetch_row or something like that.
and suggest you use mysql_pdo instead.

<?php
//if error happend,this will display error on page
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = mysqli_connect($servername, $username, $password, $dbname);

$result = mysqli_query($conn, "SELECT field_name FROM table_name");

//print result like this
while($row = mysqli_fetch_row($result)){
    print_r($row);
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
nay
  • 1,725
  • 1
  • 11
  • 11