Solved

Count the number of rows :

Posted on 2012-04-08
4
280 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

930 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

11 Experts available now in Live!

Get 1:1 Help Now