Solved

Resolve a "Warning: mysqli_fetch_array()...

Posted on 2014-09-15
3
139 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 109

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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 dynamically set the form action using jQuery.

820 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