doudao1282 2012-10-17 12:20
浏览 16

too long

I use NetBeans to run PHP code. The code is used to update a database table on our company's server. So I used the code:

$link = mysql_connect('192.168.X.X:X','X','X');
mysql_select_db('X',$link);
if (!$link) {die('Could not connect: ' . mysql_error());}
            echo 'Connected successfully';
            mysql_close($link);

but when I click run, this error message is returned:

( ! ) Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on '192.168.0.140' (10060) in C:\wamp\www\database\index.php on line 9
( ! ) Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in C:\wamp\www\database\index.php on line 10
Call Stack
  • 写回答

3条回答 默认 最新

  • dongyun3805 2012-10-17 12:32
    关注

    The first error is self-explanatory. PHP can't connect to a database at the given host. Why this may be will require further investigation on your part (is the SQL server running? Is it configured to accept non-local connections? Is there a firewall protecting the port the SQL server is listening to?)

    The second error is due to the fact you're not bothering to check whether or not the connect succeeded and just assuming that it did. If the connection cannot be made then mysql_connect will return false. You need to check for this before you can be sure that you have a valid open connection.

    $link = mysql_connect('192.168.X.X:X','X','X');
    if (false !== $link)
    {
        mysql_select_db('X',$link);
        // Rest of your code goes here
    }
    

    Incidentally, you should consider the mysql_* functions to be deprecated. There are far better alternatives such as mysqli or PDO, and you should be using one of those instead. The manual page on PHP.net makes some suggestions regarding alternatives.

    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决