标题可能具有误导性。我为此道歉。无论如何,我对SQL和ODBC感到困惑。
在我的网站上,我正在尝试设置一个注册页面。基本上,在运行MYSQL的专用服务器上,我设置了一些register.php文件,这样当一个人在网站上注册时,它就会被放入SQL中的某个数据库中。当我尝试注册时,我得到"odbc未连接"。我需要添加新的数据源吗?html.php文件示例:"Register.php"
<?php
$aw=$_GET["q"];
$aws=$_GET["q2"];
$awts=$_GET["q3"];
$conn = odbc_connect('USER_MEMBERDB','Test','Test123');
//$sql="SELECT * FROM chr_log_info WHERE id_loginid = '$q'";
if ($conn)
{
$query = "select * from chr_log_info where id_loginid = '$aw'";
//perform the query
$result=odbc_exec($conn, $query)or die("Error Here!");;
// $aw = mssql_escape_string($aw);
// $aws = mssql_escape_string($aws);
$slashRead1 = "";
$slashRead2 = "";
for($i = 0; $i < strlen($aw); $i++){
if($aw[$i] == ''''){
$slashRead1 = 'yah';
}
}
for($i = 0; $i < strlen($aws); $i++){
if($aws[$i] == ''''){
$slashRead2 = 'yah';
}
}
if(!empty($slashRead1) && !empty($slashRead2)){
echo "Username and Password values cannot be accepted. Please change your Username and Password!";
}else if(!empty($slashRead1)){
echo "Username value cannot be accepted. Please change your Username!";
}else if(!empty($slashRead2)){
echo "Password value cannot be accepted. Please change your Password!";
}else{
if(empty($aws)){
echo "Please fill-in the form completely!";
}else{
$count = 0;
//fetch tha data from the database by row
while(odbc_fetch_row($result)){
for($i=1;$i<=odbc_num_fields($result);$i++){
$row = odbc_result($result, $i);
if(!empty($row)){
$count++;
}
}
}
if(empty($aw)){
echo "Please fill-in the form completely!";
}else if($count != 0){
echo "Sorry, the username you entered is not available!";
}else if(strlen($aws) < 6){
echo "Please enter more than 6 characters for your password!";
}else{
$query1 = "Select id_idx from chr_log_info";
$result1=odbc_exec($conn, $query1)or die("Error Here!");;
while(odbc_fetch_row($result1)){
for($i=1;$i<=odbc_num_fields($result1);$i++){
$row = odbc_result($result1, $i);
}
}
$row++;
$query2 = "Select propid from chr_log_info";
$result2=odbc_exec($conn, $query2)or die("Error Here!");;
while(odbc_fetch_row($result2)){
for($i=1;$i<=odbc_num_fields($result2);$i++){
$row2 = odbc_result($result2, $i);
}
}
$row2++;
$query3 = "Insert into chr_log_info(id_idx, propid, id_loginid, id_passwd, id_sexType) values('$row', '$row2', '$aw', '$aws', '$awts')";
$result=odbc_exec($conn, $query3) or die("Error Here!");
echo "Congratulations! You have successfully registered!";
}
}
}
//close the connection
odbc_close ($conn);
}
else echo "odbc not connected";
?>
使用的另一个HTML文件是:"responsexml.HTML"
<?php
$q=$_GET["q"];
$conn = odbc_connect('USER_MEMBERDB','Test','Test123');
if ($conn)
{
$q = mysql_escape_string($q);
$slashRead = "";
for($i = 0; $i < strlen($q); $i++){
if($q[$i] == ''''){
$slashRead = 'yah';
}
}
if(!empty($slashRead)){
echo "Username value cannot be accepted!";
}else{
//the SQL statement that will query the database
$query = "select * from chr_log_info where id_loginid = '$q'";
//perform the query
$result=odbc_exec($conn, $query) or die("Wrong!");
//fetch tha data from the database
$count = 0;
while($row = odbc_fetch_row($result))
{
for($i=1;$i<=odbc_num_fields($result);$i++)
{
$row = odbc_result($result, $i);
if(!empty($row)){
$count++;
}
}
}
if(empty($q)){
echo "";
}else if($count != 0){
echo "Sorry, the username is not available!";
}else{
echo "Username is available!";
}
}
//close the connection
odbc_close ($conn);
}
else echo "odbc not connected";
?>
我是不是错过了什么?或者我需要为我的专用服务器下载某些驱动程序才能运行。有什么建议吗?
我的假设是odbc_connect
中的USER_MEMBERDB
是您试图连接到的MSSQL数据库的名称。
你可以试试:
// Microsoft SQL Server using the SQL Native Client 10.0 ODBC Driver - allows connection to SQL 7, 2000, 2005 and 2008
$connection = odbc_connect("Driver={SQL Server Native Client 10.0};Server=$server;Database=$database;", $user, $password);
但是,我建议使用ODBC上的内置MSSQL函数:
$conn = mssql_connect('SERVER', 'username', 'password');
http://php.net/manual/en/ref.mssql.php
如果你有Windows Server,你可以在电脑上添加ODBC连接。通常我使用PDO连接到数据库(而不是像你那样使用ODBC_connect)原则上,这也应该在没有用户和密码的情况下工作。如果在接口中定义。
$pdo = new PDO('odbc:dsnName','user','password');`