Jump to content



Photo

[PHP] Querying DB in functions.php

php mysql

  • Please log in to reply
2 replies to this topic

#1 generalt

generalt

    Neowinian

  • Joined: 09-May 07

Posted 25 February 2013 - 19:04

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?


#2 vetAndrew Lyle

Andrew Lyle

    Don't Panic!

  • Joined: 15-December 03
  • Location: Toronto, Ontario
  • OS: Windows 7 SP1

Posted 25 February 2013 - 19:08

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"

#3 ZakO

ZakO

    Neowinian

  • Tech Issues Solved: 2
  • Joined: 21-September 07
  • Location: Finland

Posted 25 February 2013 - 19:30

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).



Click here to login or here to register to remove this ad, it's free!