473,385 Members | 1,645 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

html output for a MySQL query without result

uranuskid
Hey folks,

Today I'm after a solution for a pretty simple problem, however, too hard for me to solve. I've got a MySQL query that reveals a table output. But if there is no result it is displaiing my table heads instead of the "else" message. The code is as follows, maybe someone could help. Cheers, Frank

[PHP]
$result = mysql_query("SELECT var_01, var_02, var_03, var_04 var_05 FROM table WHERE var_01 = '$_POST[a]' AND var_02 = '$_POST[b]'
AND var_03 = '$_POST[c]'
ORDER BY var_01");

if ($result)
{
echo "<table border='1'>
<tr>
<th>var1</th>
<th>var2</th>
<th>var3</th>
<th>var4</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['var_01'] . "</td>";
echo "<td>" . $row['var_02'] . "</td>";
echo "<td>" . $row['var_03'] . "</td>";
echo "<td>" . $row['var_04'] . "</td>";
echo "</tr>";
}

echo "</table>";mysql_close($con);
}else {

print "<h2>Sorry, no entries available!</h2>";

}

[/PHP]

The script works fine if there is a result. But if there is no result it gives me the table head instead of the error message.
Apr 1 '07 #1
3 1657
ronverdonk
4,258 Expert 4TB
The $result does not contain row values, but the resource id of the query. By accessing that resource id in a command like mysql_fetch_row you retrieve the result set.

So it is better to check the number of rows and act upon that. (I also suggest that you catch any possible errors in the select statement). Your code would be something like:

[php]
$result = mysql_query("SELECT var_01, var_02, var_03, var_04 var_05 FROM table WHERE var_01 = '$_POST[a]' AND var_02 = '$_POST[b]'
AND var_03 = '$_POST[c]'
ORDER BY var_01")
or die("SELECT error: ".mysql_error()); // catch any error

if (mysql_num_rows($result) > 0) {
echo "<table border='1'>
// .... rest of code .....
[/php]

Ronald :cool:
Apr 1 '07 #2
Thanks Ronald,

The little adjustment with mysql_num_rows() function solved the issue. I understand that without it I just query the result as itself that returns a result in any case. Therefoe my else clause would never be true.

Cheers

Frank
Apr 1 '07 #3
ronverdonk
4,258 Expert 4TB
As said before, variable $result does not contain any row values, but the resource identifier of the result set. Usually something like 'RESOURCE #1' or alike.

Ronald :cool:
Apr 1 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Martin Foster | last post by:
Hi. I want to do something really simple. I want to be able to write a custom SQL query in a textbox on a webpage, submit it and then output to the webpage in the format of MySQL output i.e....
3
by: sammmista | last post by:
hello experts, Plz dont treat this as another newbie query , i did my homework but still getting nowhere :( :( :( Trying to learn PHP on Fedora core 1 (PHP 4.3,MySQL,HTTPD).Unable to post data...
4
by: danubian | last post by:
Hi, I'm a newbie in php/mysql programming,really am. I'm working on a web-site that allows registration and posterior logging in. Already registered user logs in with valid username and...
133
by: jonathan | last post by:
hey all, I realize that this question might pop up from time to time, but I haven't seen it a while and things might of changed, so - Right now (July 2004) how does mysql stand up in...
1
by: Good Man | last post by:
Hi there I've noticed some very weird things happening with my current MySQL setup on my XP Laptop, a development machine. For a while, I have been trying to get the MySQL cache to work....
0
by: IamtheEvster | last post by:
Hi All, I am currently using PHP 5 and MySQL 5, both on Fedora Core 5. I am unable to call a MySQL stored procedure that returns output parameters using mysql, mysqli, or PDO. I'm having a...
2
by: hph | last post by:
A bunch of small things are frustrating me in trying to do some simple stuff with MySQL and php. For example, I have am trying to sum the data in a particular field, then output the result. ...
1
by: automation | last post by:
There is a truncation error of my Web Application using PHP 5.04, MySQL 5.022, and HTML(IE 6.0) whereby the MySQL Result Set is being truncated on the HTML page, even though the CSS Div Page Height...
10
by: Caffeneide | last post by:
I'm using a php script which performs three xml queries to other three servers to retrieve a set of ids and after I do a query to mysql of the kind SELECT * FROM table WHERE id IN ('set of ids');...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.