Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Resolve a "Warning: mysqli_fetch_array()...

Posted on 2014-09-15
3
Medium Priority
?
172 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 12

Accepted Solution

by:
Radek Baranowski earned 668 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 668 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 35

Assisted Solution

by:gr8gonzo
gr8gonzo earned 664 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
The viewer will learn how to dynamically set the form action using jQuery.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

824 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