簡體   English   中英

用於查詢數據庫幫助的腳本

[英]script for query a db help needed

我在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();

您實際上想做什么? 您必須描述問題,否則沒人能幫助您...

您沒有適當的錯誤處理。 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());

?>

除了已經提到的錯誤處理之外,對於第二個結果集,您可能要確保$ 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