duanruoyu6675 2014-02-09 16:10
浏览 8

无法通过php连接到db

This is my first time mixing PHP and SQL, and my problem is that I cannot connect to my database with PHP. I am using mysql_connect (and I've also found mysqli_connect, I can't see the difference).

This is my code:

mysql_connect("localhost", "username", "password","db_name");

I have put it between < ?php and a ?> in my index.php file.
And all it has showed me so far is this:

Parse error: syntax error, unexpected 'mysql_connect' (T_STRING) in /home/username/domains/website.com/public_html/index.php on line 63

I can't see the problem since I've been doing everything according to W3C's tutorial.

  • 写回答

1条回答 默认 最新

  • dongwuzun4630 2014-02-09 16:28
    关注

    You must assign the return of mysql_connect !

    $link = mysql_connect(...);

    If you need to do database connection it's time to take a look at PHP documentation :

    http://fr2.php.net/manual/en/function.mysql-connect.php

    Warning

    This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:

    mysqli_connect()
    PDO::__construct()
    

    If you're starting a project I think you haven't set all your queries statements and it'd be a great idea to change to mysqli or pdo now. Migrating parts of code is always a pain since you need to do all application tests for non regression.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥15 帮我写一个c++工程