Solved

Count the number of rows :

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

860 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