• 0

[PHP] Querying DB in functions.php


Question

I am trying to make a query from functions.php, but I get the following error:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in .../functions.php on line 60

Here is functions.php:

<?php

function inline($symbol){

        GLOBAL $dbc;

        require_once ('mysqli_connect.php');

        $inline_display = "error";

        $q = "SELECT rating WHERE symbol = '$symbol'";
        $r = mysqli_query($dbc, $q);

        while ($row = mysqli_fetch_array($r)){

            $rating = $row['rating'];

        }

        if($rating == 1)
            $inline_display = "$symbol - <font color=\"#ff0000\"><b>Avoid</b></font>";
        if($rating == 2)
            $inline_display = "$symbol - <font color=\"#990000\"><b>Risky</b></font>";
        if($rating == 3)
            $inline_display = "$symbol - <font color=\"#666666\"><b>Neutral</b></font>";
        if($rating == 4)
            $inline_display = "$symbol - <font color=\"#006600\"><b>Compelling Buy</b></font>";
        if($rating == 5)
            $inline_display = "$symbol - <font color=\"#ff6600\"><b>Top Pick</b></font>";

        return $inline_display;

    }
?>

This is the page that calls the function:

<html>
<body>

<?php
    //require_once ('mysqli_connect.php');
    include ('functions.php');

    $symbol = "XOM";
    echo inline($symbol);



?>

</body>
</html>

And here is mysqli_connect.php:

<?php

// Set the database access information as constants:
DEFINE ('DB_USER', 'user');
DEFINE ('DB_PASSWORD', 'pass');
DEFINE ('DB_HOST', 'host');
DEFINE ('DB_NAME', 'db');

// Make the connection:
$dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL: ' . mysqli_connect_error() );

// Set the encoding...
mysqli_set_charset($dbc, 'utf8');

// Use this next option if your system doesn't support mysqli_set_charset().
//mysqli_query($dbc, 'SET NAMES utf8');



?>

I think my problem has something to do with making $dbc a global variable, but I haven't been able to resolve the issue. What am I doing wrong?

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

you have the database connection commented out.

Edit: i've never used "mysqli_fetch_array" myself, so i don't know the advantages of that compared to 'mysql_fetch_array" and "mysql_query" vs "mysqli_query"

Link to comment
Share on other sites

  • 0

Your SQL syntax is incorrect so mysqli_query is returning false instead of a result set:

$q = "SELECT rating WHERE symbol = '$symbol'";[/CODE]

Should be:
[CODE]$q = "SELECT rating FROM <table_name> WHERE symbol = '$symbol'";

However, using global variables and requiring files inside functions are bad practices. A (slightly) better way would be to pass the database handle as a parameter to the inline function, also, watch out for SQL Injection if symbol is going to be user generated.

i've never used "mysqli_fetch_array" myself, so i don't know the advantages of that compared to 'mysql_fetch_array" and "mysql_query" vs "mysqli_query"

mysql_* functions are deprecated and will be removed in future versions of PHP, no new scripts should use them (use Mysqli or PDO).

Link to comment
Share on other sites

This topic is now closed to further replies.