Solved

Count the number of rows :

Posted on 2012-04-08
4
277 Views
Last Modified: 2012-04-09
Hi, I have the following sql statement in php:

$query_1 = "SELECT patient_id,visit_notes, DATE_FORMAT(visit_date,'%Y-%m-%d') AS event_date FROM patient_visitschedule WHERE visit_date LIKE '$year-$month%'";
$num_rows = mysql_num_rows($query_1);
echo $num_rows;

When I run this code I get the following error, why is this?

Warning: mysql_num_rows() expects parameter 1 to be resource, string given in C:\xampp\htdocs\carePlus\module_patient\viewPatientSchedule.php on line 65

Now when I run the following code;

$events = array();
$query_1 = "SELECT patient_id,visit_notes, DATE_FORMAT(visit_date,'%Y-%m-%d') AS event_date FROM patient_visitschedule WHERE visit_date LIKE '$year-$month%'";
$result_1 = mysql_query($query_1) or die('cannot get results!');
while($row = mysql_fetch_assoc($result_1)) {
$events[$row['event_date']][] = $row;

I get the event information displayed on my php page, but what I need is the count of the number of rows fetched. Can someone help me here. Thank  you.

A
0
Comment
Question by:aej1973
  • 2
4 Comments
 
LVL 17

Expert Comment

by:ramrom
ID: 37822163
instead of $num_rows = mysql_num_rows($query_1);
try $num_rows = mysql_num_rows($result_1);
after, of course, running the query,
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 125 total points
ID: 37822185
$query_1 is a string variable. To get a results set you actually need to run the query!
$query_1 = "SELECT patient_id,visit_notes, DATE_FORMAT(visit_date,'%Y-%m-%d') AS event_date FROM patient_visitschedule WHERE visit_date LIKE '$year-$month%'";
$x = mysql_query($query_1);
if (!$x) due("FAIL $query_1<br/>" . mysql_error() );
$num_rows = mysql_num_rows($x);
echo $num_rows;

Open in new window

0
 

Author Closing Comment

by:aej1973
ID: 37822397
Thank you.
0
 
LVL 17

Expert Comment

by:ramrom
ID: 37823674
Why did you accept a 2nd and later version of my solution instead of mine?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

707 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now