tonnor22
tonnor22

Reputation: 15

Trying to output a mysql database in php

I'm trying to test outputting data from a database table using phpmyadmin in php. For some reason it isn't working though, no errors involved. What am I doing wrong here?

Here is a picture of my database:

database

Note: This database is filled with fake information, no real e-mail addresses are used.

Here is my code:

    <?php

    include "cs234DbConnect.inc.php";

    $dbc =  mysqli_connect(DBHOST, DBUSER, DBPWD, DBNAME);
    $error = mysqli_connect_error();
    if($error != null){
        $output= "<p> Unable to connect to database<p>" . $error;
        exit($output);
    }


    $query = mysqli_query($dbc, "SELECT * FROM site7_customers WHERE FirstName = 'Leonie'") or die(mysqli_error($dbc));

    while($rows = mysqli_fetch_array($query)){
        $FirstName = $rows['FirstName'];
        $LastName = $rows['LastName'];
        $Email = $rows['Email']


        echo "$FirstName $LastName<br>$Email<br><br>";

    }
    mysqli_close($dbc);


    ?>

Here is how I'm connecting:

<?php
    /* File: cs234DbConnect.inc.php
    */
    define('DBNAME', 'mtingle');
    define('DBHOST', 'localhost');
    define('DBUSER', 'mtingle');
    define('DBPWD'', 'mypassword');

    define('DBCONN', 'mysql:host=' . DBHOST . ';dbname=' . DBNAME);
    ?>

Thank you in advance.

Upvotes: 1

Views: 256

Answers (1)

Funk Forty Niner
Funk Forty Niner

Reputation: 74219

As per your originally posted question/code should any be wondering.

The reason being is that you're not connecting to your query.

$query = mysqli_query("SELECT...

missing $dbc connection variable to it.

do

$query = mysqli_query($dbc,"SELECT...

Having used

or die(mysqli_error($dbc)) to mysqli_query() would have thrown you an error.

Edit: after you posted your cs234DbConnect.inc.php content

Remove define('DBCONN', 'mysql:host=' . DBHOST . ';dbname=' . DBNAME); from inside your file, that is PDO syntax.

and define('DBPWD'', 'mypassword'); (extra quote)

to define('DBPWD', 'mypassword');

you also missed a semi-colon at the end of $Email = $rows['Email']

Thanks Meda.


Plus, using your present method is open to SQL injection, use prepared statements, or PDO with prepared statements, they're much safer.


Plus, that missing semi-colon should have produced a parse error.

Add error reporting to the top of your file(s) which will help find errors.

error_reporting(E_ALL);
ini_set('display_errors', 1);

Sidenote: Error reporting should only be done in staging, and never production.

Upvotes: 3

Related Questions