473,776 Members | 1,645 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

MySQL: Most efficient method retrieving single result.

Hi all,

Wanted to know what is the most efficient way of doing a select query
for mysql that only returns one value. For example:

$mysqli->query('selec t count(*) from log');
$temprec = $result->fetch_assoc( );
$count = $temprec['count'];

That doesn't seem efficient. How should I do it? Or is this as efficient
as it gets?

--

Belmin Fernandez

Visit: http://www.belminfernandez.com/homepage
Email: belminf at gmail period com
Jul 23 '05 #1
2 3891
Oh sorry, using PHP.

Written by "Belmin" on 2/21/05 7:05p:
Hi all,

Wanted to know what is the most efficient way of doing a select query
for mysql that only returns one value. For example:

$mysqli->query('selec t count(*) from log');
$temprec = $result->fetch_assoc( );
$count = $temprec['count'];

That doesn't seem efficient. How should I do it? Or is this as efficient
as it gets?


--

Belmin Fernandez

Visit: http://www.belminfernandez.com/homepage
Email: belminf at gmail period com
Jul 23 '05 #2
http://dev.mysql.com/doc/mysql/en/sh...le-status.html
To get information about a table ie. rows, size, etc. you could use:
$mysqli->query("show table status like 'log'");
$temprec = $result->fetch_assoc( );
print_r ($temprec);
Also you could use the LIMIT clause at the end of you query:

$mysqli->query("selec t * from log limit 1");
$temprec = $result->fetch_assoc( );
print_r ($temprec);

http://dev.mysql.com/doc/mysql/en/li...imization.html
http://dev.mysql.com/doc/mysql/en/select.html

The LIMIT clause can be used to constrain the number of rows returned
by the SELECT statement. LIMIT takes one or two numeric arguments,
which must be integer constants.

With two arguments, the first argument specifies the offset of the
first row to return, and the second specifies the maximum number of
rows to return. The offset of the initial row is 0 (not 1):

mysql> SELECT * FROM table LIMIT 5,10; # Retrieve rows 6-15

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
5449
by: Durai | last post by:
Hi All, I tested "concurrent testing" in MySQL. It works fine. But I couldn't do in PostgreSQL 7.3.4 on HPUX IPF. I got deadlock problem. I used the PHP script to update table( one script increment the column & another one decrement the column). Is the postgres support the concurrent access to update database? I got the following errors: test=# ERROR: deadlock detected ERROR: deadlock detected ERROR: deadlock detected .....
1
2830
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. Despite entering the required lines to "my.ini" (the new my.cnf) through notepad AND MySQL Administrator, the cache does not work. So, today I took a peek at the 'Health' tab in MySQL Administrator.
6
1843
by: Ridge Burner | last post by:
Can someone tell me which of these 2 SQL queries will be more efficient? I'm having a debate with another guy about which would be less resource intensive for MySQL. The first uses MySQL to pick a random row in a single statement: <?php $sql = "SELECT FROM myTable ORDER BY RAND() LIMIT 1"; $query = mysql_query($sql,$conn); ?>
4
6150
by: monomaniac21 | last post by:
hi! is it possible to do the aforementioned query - selecting only distinct in 1 col but retrieving all other cols at the same time. regards marc
6
2640
by: jej1216 | last post by:
I am trying to put together a PHP search page in which the user can select none, one, two, or three fields to search, and then the results php will build the SQL with dynamic where caluses to reflect the fields chosen. The first page, where they select the search fields and submit: <?php $db = mysql_connect("localhost", "root", "yeahright"); if (!$db) { die('Could not connect:'.mysql_error);
6
38520
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get through this without much trouble. Programming knowledge is not required. Index What is SQL? Why MySQL? Installing MySQL. Using the MySQL command line interface
221
367737
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application needs to store entire files, the preferred method is to save the file onto the server’s file-system, and store the physical location of the file in your database. This is generally considered to be the easiest and fastest way to store files. ...
4
6501
by: TechnoAtif | last post by:
Hi ALL I have entered some array values using checkboxes into mysql database through a form. Next iam creating a searchpage where all those cateogories inserted through checkboxes has to be retrieved using list/menu box. When i check only a single checkbox to insert the checked category ,selecting that category through list box gives out the entire data of the user corresponding to that category. However when i check multiple checkboxes and...
2
2685
by: fishnfrogs | last post by:
Hello everyone! I have an app that uses a for loop to go through an array and it selects all the values from my MySQL database and returns the values. I can't imagine that this is the best approach. My code kinda looks like this: $array = array('blah', 'bleep', 'bloop'); $password = 'same_value_for_each_item_in_the_array'; $name = 'same_value_for_each_item_in_the_array'; $userArray = array(); $passArray = array(); $nameArray =...
0
9628
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10289
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10120
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10061
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9923
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8952
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5493
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4031
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3622
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.