Sign in to follow this  
Followers 0

[PHP] Querying DB in functions.php


3 posts in this topic

Posted

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?

Share this post


Link to post
Share on other sites

Posted

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"

Share this post


Link to post
Share on other sites

Posted

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

$q = "SELECT rating WHERE symbol = '$symbol'";
Should be:
$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).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.