PHP MYSQL数据库连接问题求大神指点迷津,新手入门。 Warning: mysql_query() [function.mysql-query]

这是PHP代码
。。省略一部分
***下面是出错的几句
$query="select * from als_signup where UserName='{$_SESSION['UserName']}' and Password='{$_SESSION['Password']}'";
$result=mysql_query($query);
$row=mysql_fetch_array($result);
if ($row)
{
//如果session会话变量用户名与密码匹配,则自动登录,直接跳转到管理页面
header("refresh:1;url=http://localhost/members/manage.php");
exit;
}

这是运行的警告!!

Warning: mysql_query() [function.mysql-query]: Access denied for user
'ODBC'@'localhost' (using password: NO) in D:\wamp\www\phptest\config.php
on line 9

Warning: mysql_query() [function.mysql-query]: A link to the server
could not be established in D:\wamp\www\phptest\config.php on line
9

Warning: mysql_query() [function.mysql-query]: Access denied for user
'ODBC'@'localhost' (using password: NO) in D:\wamp\www\phptest\config.php
on line 12

Warning: mysql_query() [function.mysql-query]: A link to the server
could not be established in D:\wamp\www\phptest\config.php on line
12
Error while creating database (Error1045:"Access denied for user
'ODBC'@'localhost' (using password: NO)")

Warning: session_start()
[function.session-start]: Cannot send
session cache limiter - headers already sent (output started at
D:\wamp\www\phptest\config.php:9) in D:\wamp\www\phptest\login.php on
line 4

Warning: mysql_query() [function.mysql-query]: Access denied for user
'ODBC'@'localhost' (using D:\wamp\www\phptest\login.php on line
6

Warning: mysql_fetch_array(): supplied argument is not a
valid MySQL result resource in D:\wamp\www\phptest\login.php on line
7

php语法错误

你既然把$_SESSION['UserName']直接写在sql语句中,那就不要加那个单引号了,

正确语法是:

$query="select * from als_signup where UserName='{$_SESSION[UserName]}' and Password='{$_SESSION[Password]}'";

 第二处应该修改的是:

$row=mysql_fetch_array($result);

改成:

$row=mysql_num_rows($result);

mysql_num_rows()函数是返回mysql_query()返回的数据源的行数,也就是返回符合sql语句的数据表内的记录行数,如果有这用户,那么至少存在1行,如果没有这个用户,那就是没有一行,就会返回0

追问

$query="select * from als_signup where UserName='{$_SESSION[UserName]}' and Password='{$_SESSION[Password]}'";
$result=mysql_query($query);
$row=mysql_num_rows($result);
按照你的修改后错误提示不变哦~请指教

追答

你确定你连接数据库都正常?
要操作数据库,首先要连接数据库不是?
你得先排除数据库连接方面的问题啊
Warning: mysql_query() [function.mysql-query]: A link to the server
could not be established in D:\wamp\www\phptest\config.php on line
12
Error while creating database (Error1045:"Access denied for user
'ODBC'@'localhost' (using password: NO)")
这条警告,感觉像是数据库连接有问题

追问

我自己打开数据库能连上,能加我聊下吗?我把所有的源码发你看一下,谢谢啦。
我的寇953 984048,到时再给你加分

追答

看了你对其他人的追问

<?php

$server="localhost";

$username="root";

$password="";

$database=""; //这个应该是数据库名称,但是你留空,我就纳闷了,有没有名字的数据库吗?

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-07-12
首先: 你数据库连接是否和本页面有关链? 如果没有直接关联,请在mysql_query(参数1,参数2),参数2处填写好你连接对象(这只是一个示范);具体参考手册!

后面的 在排除你数据库连接问题之前 ,所有警告信息都是由于你库连接失败!!
第2个回答  推荐于2018-03-19
Access denied for user 'ODBC'@'localhost' (using password: NO)
你把连接帐号密码写正确了追问

<?php
$server="localhost";
$username="root";
$password="";
$database="";
这是config.php里面的代码 和我的电脑设置的是一样的,为什么还是打不开呢?

追答

看你的错误信息是用的ODBC这个帐号

追问

ODBC是PHP通过这个数据库的名字吧,能加我聊下吗?我把所有的源码发你看一下,谢谢啦。
我的寇953 984048,到时再给你加分

第3个回答  2013-07-12
数据库的账号和密码不匹配吧追问

<?php
$server="localhost";
$username="root";
$password="";
$database="";
这是config.php里面的代码 和我的电脑设置的是一样的,为什么还是打不开呢?

第4个回答  2013-07-12
PHP中,不能用单引号''包住{}的,要么用""号,要么不用。
你的应该这样写:

$UserName=$_SESSION['UserName'];
$Password=$_SESSION['Password'];
$query="select * from als_signup where UserName='$UserName' and Password='$Password'";
$result=mysql_query($query);
$row=mysql_fetch_array($result);
if ($row).........................................
相似回答