简体   繁体   English

用于查询数据库帮助的脚本

[英]script for query a db help needed

i have the following code in php: 我在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();

What do you actually want to do? 您实际上想做什么? You have to describe the problem else no one can help you... 您必须描述问题,否则没人能帮助您...

You have no proper error handling. 您没有适当的错误处理。 The mysql functionality provided with php have a build in function that outputs the error on the screen. php提供的mysql功能具有内置功能,可在屏幕上输出错误。 This would be a lot better: 这样会更好:

<?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());

?>

In addition to the error handling already mentioned, for your second resultset, you might want to ensure that $count2 is the number of rows returned in $result2 rather than in the first resultset ($result) 除了已经提到的错误处理之外,对于第二个结果集,您可能要确保$ count2是$ result2中而不是第一个结果集中返回的行数($ 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($result2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM