Solved

Resolve a "Warning: mysqli_fetch_array()...

Posted on 2014-09-15
3
136 Views
Last Modified: 2014-09-26
Here's my code. How to resolve this Warning. The page actually works on my local environment, but doesn't parse when uploaded to dev.

warning
 
  <?php 
    define('DB_IP', 'localhost');                   // MySQL Server
    define('DB_USERNAME', 'xxx');       	        //database username
    define('DB_PASSWORD', 'xxx');       		//database password
    define('DB_DATABASE', 'navigator_data');        //database
    
    class test_object   // proxy for web service object
    {
        public $contractVehicle = array(
            0=>'ITSchedule70',
            1=>'ITCommodityProgram',
    	2=>'Army CHESS',
    	3=>'NASA SEWP'
        );
    
        function get_contract()
        {
            return $this->contractVehicle;
        }
    }
    
    $var = new test_object(); // the variable get back from webservice
    
    $connection = mysqli_connect(DB_IP,DB_USERNAME,DB_PASSWORD,DB_DATABASE); // connection to navigator_data
    
    $qry_nav_data = "SELECT * FROM contract_vehicle"; // formulate the SQL query
    
    $result = mysqli_query($connection, $qry_nav_data); // execute the query
    
    // display results in HTML table
    echo "<table border='1'>
    <tr>
    <th>contract_vehicle_id</th>
    <th>vehicle_name</th>
    </tr>";
    
    while($row = mysqli_fetch_array($result)) {
      echo "<tr>";
      echo "<td>" . $row['contract_vehicle_id'] . "</td>";
      echo "<td>" . $row['vehicle_name'] . "</td>";
      echo "</tr>";
    }
    echo "</table>";
    
    mysqli_close($connection);
    ?>

Open in new window

0
Comment
Question by:sandshakimi
3 Comments
 
LVL 11

Accepted Solution

by:
Radek Baranowski earned 167 total points
ID: 40323095
From http://php.net/manual/en/mysqli.query.php:

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

that means that probably your query was unsuccessful and $result = false

try to see what's in $result with
var_dump($result);

in your code.
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 167 total points
ID: 40323171
The warning is a symptom of a query failure.  There are ways to see the query errors.  Learn about this function:
http://www.php.net/manual/en/mysqli.error.php

This article is not short, nor simple, but it's the sort of thing a professional programmer would be required to understand.   Please read it over and post back if you have any further questions.  It shows how to use the MySQL extensions and maps the familiar but obsolete MySQL extension onto MySQLi and PDO.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
 
LVL 34

Assisted Solution

by:gr8gonzo
gr8gonzo earned 166 total points
ID: 40323237
I would guess that the dev server does not have the contract_vehicle table, or that your user credentials on dev for MySQL don't have access to the database or table. One of those, but probably the first one.

That would result in a failed query, like the previous two gentlemen have said.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now