-2

I have a problem with PHP and mysqli that I don't know how to solve. I'm trying to query the DB and get the results as an associative array, but I can't seem to get there. When I access this page I get a blank screen with nothing in it.

This is my code:

$link = mysqli_connect("localhost", "dash_user", "DASH_dash", "dashboard");
$query = "SELECT * FROM system_settings";

if ($result = mysqli_query($link, $query)) {

/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
printf ("%s (%s)\n", $row["install_date"], $row["export_reports_enabled"]);
}

/* free result set */
mysqli_free_result($result);

I literally, got that from PHP's library. I also tried this:

<?php
$ddb_database = "dashboard";
$ddb_host = "localhost";
$ddb_user = "dash_user";
$ddb_pass = "DASH_dash";
$ddb_port = "3306";

$ddb=mysqli_connect("$ddb_host", "$ddb_user", "$ddb_pass", "$ddb_database", "$ddb_port");

if (!$ddb)
    {
    die("MySQL Dashboard DB connect ERROR:  " . mysqli_error('mysqli'));
    }

if ($result = mysqli_query($ddb,$stmt))
{
$result = mysqli_query($ddb, $stmt);
$row_cnt = mysqli_num_rows($result);
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
$install_date =             $row["install_date"];
$messaging_enabled =        $row['messaging_enabled'];
$tasks_enabled =            $row['tasks_enabled'];
$alerts_enabled =           $row['alerts_enabled'];
$export_reports_enabled =   $row["export_reports_enabled"];
mysqli_free_result($result);
}

echo "Install date: $install_date";
echo "Export reports: $export_reports_enabled";
echo "Number of rows: $row_cnt";

But I still do not get anywhere. There are no connection errors.

The DB looks like this:

MariaDB [dashboard]> SELECT * FROM system_settings;
+--------------+-------------------+---------------+----------------+------------------------+
| install_date | messaging_enabled | tasks_enabled | alerts_enabled | export_reports_enabled |
+--------------+-------------------+---------------+----------------+------------------------+
| 2015-02-23   | N                 | N             | N              | N                      |
+--------------+-------------------+---------------+----------------+------------------------+
1 row in set (0.00 sec)

It is also my understanding that Mysqli is also compatible with MariaDB (my DB engine). Could that be the cause of my problem?

Manny
  • 37
  • 1
  • 6
  • 1
    I hope that isn't your password for the database. In case, then you shouldn't announce it here, always replace it with some dummy text ^^ – Jesper Feb 26 '15 at 21:04
  • In your second example I'm assuming that $stmt is defined somewhere??? – BigScar Feb 26 '15 at 21:06
  • I'm not sure if you can use mysqli with MariaDB. You should consider using PDO. http://code.tutsplus.com/tutorials/pdo-vs-mysqli-which-should-you-use--net-24059 – Jesper Feb 26 '15 at 21:07
  • Hi guys, thanks for your answers. @Djip: Yes it is the pass to the development DB, don't really care for the pass as this is on hosted on my laptop, so..., can't really care for the info there :) Thanks for the headsup though. I will also investigate the PDO, see if that gives me some good results – Manny Feb 27 '15 at 06:51
  • @BigScar: Yes $stmt is the same as $link, I guess I missed it when I copied from my code to here. – Manny Feb 27 '15 at 06:53

1 Answers1

0

if this is your complete code (in the first section), youre missing a } that belongs to the if

$link = mysqli_connect("localhost", "dash_user", "DASH_dash", "dashboard");
$query = "SELECT * FROM system_settings";

if ($result = mysqli_query($link, $query)) {

    /* fetch associative array */
    while ($row = mysqli_fetch_assoc($result)) {
        printf ("%s (%s)\n", $row["install_date"], $row["export_reports_enabled"]);
    }

    /* free result set */
    mysqli_free_result($result);
}

I cant be sure if this is what you intended, so you might need to move the brace upwards.

Its a lot easier to debug your code if you indent properly.

edit: have a look at this for your mariaDB question.

also, as said in comments, turn on error reporting to see whats going on.

ini_set('error_reporting', E_ALL);
Community
  • 1
  • 1
DevDonkey
  • 4,835
  • 2
  • 27
  • 41
  • Hi Matt, thanks for your response. I missed the bracket at the end, but it is there in my code. I just turned on the error reporting by setting the line you posted there, but there is still no output on the log... it is blank... Any more ideas...? Thanks! – Manny Feb 27 '15 at 06:55