Ways to verify if php's mysqli_fetch_array is void prior to entering the while loop

I need to confirm the presence of results before executing the while loop, but every approach I've attempted so far is causing the first result to be eliminated.

$nearbyResult = mysqli_query($con,$sqlNearby);

if(mysqli_fetch_array($nearbyResult) == 0) {
    echo '<p>Zero results found, <a href="/add.php">Add your property here</a>.</p>';
} else {
    while($rowNearby = mysqli_fetch_array($nearbyResult)) {
    }
}

Answer №1

This code snippet will discard the first row of data from your query result:

if(mysqli_fetch_array($nearbyResult) == 0) {

Update it to this:

if( ! mysqli_num_rows($nearbyResult) ) {

Make sure to validate your function returns properly:

if( ! $nearbyResult = mysqli_query($con,$sqlNearby) ) {
    echo "MySQL error: " . mysqli_error($con);
}

Answer №2

If you want to determine the number of rows retrieved by your query, the mysql_row_count function can be used. Additional information can be found at this link.

Answer №3

To improve your code, consider storing the results in a variable within your if statement. When mysqli_fetch_array() does not have any result set to process, it will return false.

if($rowNearby = mysqli_fetch_array($nearbyResult)) {
    //A result was found, proceed with processing
    doStuffWith($rowNearby);
} else {
    //No records were retrieved, handle accordingly
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Sending emails through Phpmailer twice via Slim API

I have encountered a problem while using Slim framework for my api and sending emails using phpmailer. The issue is that $mail->send() seems to be triggering my api twice, resulting in an incorrect response. Can someone please explain why this is happ ...

How to efficiently download a bulky file using PHP

Need help with a PHP script for downloading large files? Most of the time, it works fine, but on slow connections, the download request ends abruptly. I've tried adjusting some php.ini variables without success. My hosting server is SiteGround, and si ...

Running a PHP script that executes a shared library file

After creating the shared library client.so, I found that when it is executed, the main() function gets triggered. The main() function reads a string from the user and then calls the function ch=foo(buffer);. Below is the code for Main.cpp: #include& ...

PHP array addition techniquesExplanation on how to add arrays

Hello there, I'm fairly new to PHP so your patience is appreciated. I've been trying to add an array using a foreach loop but for some reason, it's not working as expected. if($size > 0) { $resultArray = array(); fo ...

What is the best way to create multiple PDF pages using mpdf1?

Currently using mpdf1 and looking to generate a PDF with a table of 4000 rows. Any suggestions on how to achieve this using mpdf1? Is there a method to speed up the process and avoid waiting for several minutes? ...

Creating a Laravel development server without relying on php artisan is a straightforward process

I'm a Laravel beginner and I recently installed the Voyager admin panel for CRUD operations. The folder structure of my project is as follows: testadmin (project name) -> vendor -> tcg -> voyager When I run php artisan serve, I can access m ...

Send form without reloading the page (partially updating the page)

Code snippet in index.php HTML head: <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> <script src="http://malsup.github.com/jquery.form.js"></script> <script> // Ensurin ...

Show the JSON data from the server in a table format

Is there a way to neatly display data received from an Ajax response within a table format? Below is the structure of the table: <table data-toggle="table" data-show-columns="true" data-search="true" data-select-item-name="toolbar1" id="menu_table"> ...

Unleash the power of CakePHP with advanced joins capabilities within

Having issues with joins in my find-query, encountering errors like: preg_match() expects parameter 2 to be string, array given [CORE/cake/libs/model/behaviors/containable.php, line 301] I attempted adding joins to the array without success. Here are ...

Unable to retrieve file from Alias directory using the download script

Currently, I am developing a download script. The PHP files are stored in the xampp htdocs directory, while the folder containing the actual files is on an external HDD. An Alias has been configured for the external drive: <Directory "h:/Filme"> Op ...

Problem encountered with utilizing sleep() function in PHP alongside a redirect operation

I'm struggling with this code snippet: <? echo "<html><br><br><br><div id='loading'><p><img src='loader.gif'> Please wait 6 seconds...</p></div></html>"; sleep(6); he ...

Making a secure connection using AJAX and PHP to insert a new row into the database

Explaining this process might be a bit tricky, and I'm not sure if you all will be able to assist me, but I'll give it my best shot. Here's the sequence of steps I want the user to follow: User clicks on an image (either 'cowboys&apos ...

Alerts are essential for the proper functioning of the AJAX function. Without them

As I incorporate a substantial amount of AJAX with XML Http Requests on my website, I encounter a peculiar issue with a few random AJAX calls. There seems to be an execution problem within my JavaScript code in the onreadystatechange function where certain ...

Unable to execute click events on JavaScript functions after updating innerHTML using PHP and Ajax

To begin, I want to clarify that I am intentionally avoiding the use of jQuery. While it may simplify things, it goes against the purpose of my project. Please note that 'ajaxFunction' serves as a generic example for AJAX requests using GET/POST ...

Tips for inserting a hyperlink on every row in Vuetables using VUE.JS

Trying to implement a link structure within each row in VUEJS Vuetables has been challenging for me. Despite my research on components and slots, I am struggling to add a link with the desired structure as shown below: <td class="text-center"> <a ...

Using PHP's exec() function to manipulate character encoding via the command line

I've been struggling to pass UTF-8 text as an argument to a command line program using the exec function in PHP. It seems like there are some issues with character encoding causing trouble. When I run locale charmap from the terminal, it returns: UTF ...

jQuery if statement appears to be malfunctioning

When the condition operates alone, everything works fine. However, when I introduce an 'and' operation, it does not function correctly. If only one of them is true, the code works. It also successfully takes input values. <!DOCTYPE HTML Code ...

How can I utilize JavaScript on the server-side similar to embedding it within HTML like PHP?

One aspect of PHP that I find both intriguing and frustrating is its ability to be embedded within HTML code. It offers the advantage of being able to visualize the flow of my code, but it can also result in messy and convoluted code that is challenging to ...

PHP API Integration for XBox

Check out this demo link: http://davidwalsh.name/xbox-api I decided to create a php file with the following content.. <?php // Customizations $gamertag = 'RyanFabbro'; $profileUrl = 'http://www.xboxleaders.com/api/profile/'.$gamert ...

Custom code for the WordPress excerpt

Is there a way to set a universal hard-coded Excerpt in WordPress for all posts and pages? I've seen suggestions to modify the index.php and archive.php files, but I'm uncertain about the best approach. Is it possible to create a function for th ...