Jump to content

PHP and MySQL


CWeaver

Recommended Posts

Here's what I'm doing. The result is consistantly No records found. select * from patientsThere are two records in the table and I can access them through MySQL Client.

<?phpsession_start();include ("Common.Inc.php");@ $db = mysql_pconnect('some ip address that works here', 'username', 'password');if (!$db){	echo "<BR />There's been a problem connecting with our server.";	echo "<BR />Please try this service again later.";	exit;}	DisplayPatientSelection();?>//in Common.Inc.php:function DisplayPatientSelection(){	$query = "select * from patients";	$result = mysql_query($query);	if ($result)	{  $num_results = mysql_num_rows($result);  if ($num_results > 1) 	 {    echo $num_results . " patients found."; 	 }  else 	 {    echo $num_results . " patient found."; 	 }	}	else	{  echo "<br/>";  echo "No records found.";  echo "<br/>";  echo $query;	}}

Sorry that none of the indentations in my code come across here. Any ideas on what I'm doing wrong? Other similar queries to other tables on the same server within the same database work.

Link to comment
Share on other sites

When querying your database, you should use this instead:$query=mysql_db_query ("your database","select * from patients");Just makes it look nicer from my standpoint. Also, I don't see where your actually telling the MYSQL to get the information from. Your not telling it what database to get the information from.

Link to comment
Share on other sites

$query=mysql_db_query ("your database","select * from patients");
I like clarity in code as well. I've made this change in mine.
I don't see where your actually telling the MYSQL to get the information from. Your not telling it what database to get the information from.
This is a critical point! In other scripts the same code worked for other tables. I'm not certain how. Anyway, including the name of the database above did not make a functional difference.Can you see anything else wrong with my code? Is there another place that I should be naming the database to use?
Link to comment
Share on other sites

Whats with the @ sign before $db. I just tried your code on a page of mine and it displayed everything properly. I tried it a few different ways and it worked. I don't see anything else which is causing a problem. Possibly make sure that the tables are named the same. Maybe rename your table and see if that solves the problem.

Link to comment
Share on other sites

I think there's something else going on. Row retrieval that was working fine yesterday is not today. The only reason I didn't suspect my host sooner is that I have no trouble retrieving data through MySQL Client. I don't think it's my host. It just doesn't make sense that MySQL Client can get data and my PHP can't because of something on the server. Thanks for running my code on your system. As for the @ sign -- I took it from a template that I saw. I'm just getting started with this database connectivity stuff.

Link to comment
Share on other sites

I am assuming your using a host on the internet from the sounds of it to develop these pages of yours. If your just beginning to learn PHP and MYSQL I suggest that you download a personal server from the net which will allow you to develop webpages and view them on your home computer. This is what I do for my many pages I code and it allows for quick, easy modifications and also if the net goes down, I can still do some coding.

Link to comment
Share on other sites

Very good advice. I'm currently running Apache with PHP and MySQL for my localhost stuff. But on this project, my first using MySQL, I've developed the data on the host and not locally. Which brings up another issue, I don't know how to transfer data from one server to another -- hence the reason for putting it on the remote server; it's the only way I could be sure that my customer would be able to see what I was developing.

Link to comment
Share on other sites

OK, here's what the problem was, I think:I was incorrectly specifying the database, and superfluously as well!

$result = mysql_query($query, $databasename);

mysql_select_db("databasename", $connection);

When I removed the incorrect inclusion of the databasename in the mysql_query call, everything worked fine. The second parameter in mysql_query is meant to specify the connection to use. By default, it uses the most recent connection.Thanks for your help, and sorry I didn't include the errant code the first time; just didn't see it. :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...