Solved

Count the number of rows :

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

809 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