2011-04-20 44 views
0

我在PHP下面的代码:脚本查询数据库需要帮助

$host="localhost"; // Host name 
$username="***"; // username 
$password="***"; // password 
$db_name="***"; // Database name 
//$rc_profile_table="rc_profile_table"; // Table name 
//$rc_profile_relation_table="rc_profile_relation_table"; // Table name 


mysql_connect("$host", "$username", "$password"); 
mysql_select_db("$db_name"); 

$sql="SELECT created_at FROM rc_profile_table where created_at > 2011-04-19 08:00:00"; 
$result=mysql_query($sql); 
$count=mysql_num_rows($result); 

$sql="SELECT created_at FROM rc_profile_relation_table where created_at > 2011-04-19 08:00:00"; 
$result2=mysql_query($sql); 
$count2=mysql_num_rows($result); 

mysql_close(); 
+0

接受最近的问题。 – hsz 2011-04-20 10:05:26

+0

请勿发布您的用户名和密码... – rsplak 2011-04-20 10:06:20

+0

请选择您的代码,然后在提问时单击{}图标。这将格式化代码。 – 2011-04-20 10:06:51

回答

0

您没有适当的错误处理。 php提供的mysql功能具有内置函数,可以在屏幕上输出错误。 这将是好了很多:

<?php 
$host="localhost"; // Host name 
$username="***"; // username 
$password="***"; // password 
$db_name="***"; //db name 

$connection = mysql_connect($host, $username, $password) or die("Could not connect to the database: " . mysql_error()); 
mysql_select_db($db_name, $connection) or die("Could not select database: " . mysql_error()); 

$sql = "SELECT `created_at` FROM `rc_profile_table` WHERE `created_at` > '2011-04-19 08:00:00'"; 
$result = mysql_query($sql) or die("Could not execute query: " . $sql . "ERROR: " . mysql_error()); 
$count = mysql_num_rows($result); 

mysql_close($connection) or die(mysql_error()); 

?> 
+0

非常感谢你! – 2011-04-20 10:51:52

+0

如果您发现某人真正有用的答案,您需要接受此答案。您可以通过按下答案左上角的V图标(使其变为绿色)来做到这一点。 (根据票数)。 – 2011-04-20 11:02:04

+0

我现在怎么写输出到一个文本文件,使它看起来不错? 谢谢 – 2011-04-20 11:31:15

3

什么你真的想要吗? 您必须描述问题,否则没有人可以帮助您...

+0

,这是一个评论,而不是一个鼻! – CloudyMarble 2011-04-20 10:10:46

+0

我没有权利到处发表评论:S – KillerX 2011-04-20 10:12:47

+0

然后你不应该评论:-) – 2011-04-20 10:15:54

0

除了已经提到的,对于你的第二个结果的错误处理,您可能希望确保$ COUNT2是在在$ RESULT2返回的行数,而不是第一个结果集($结果)

$sql="SELECT created_at FROM rc_profile_relation_table where created_at > 2011-04-19 08:00:00"; 
$result2=mysql_query($sql); 
$count2=mysql_num_rows($result2);