Connecting to mysql using php

前端 未结 6 1483
逝去的感伤
逝去的感伤 2020-12-18 17:46

Here is my following tutorial code from \"Learning PHP MYSQL and Javascript\" by OReilly.



        
相关标签:
6条回答
  • 2020-12-18 17:57

    Change this

    $result = $mysql_query($query);
    

    to this

    $result = mysql_query($query);
    

    Function name is mysql_query not $mysql_query, $ is used in variable names.

    Also, instead of trying to "see" your code immediately in a browser window you could use a PHP command line instead. If your file is called foo.php you coudl do:

    >> php foo.php
    PHP Notice:  Undefined variable: mysql_query in /var/www/foo.php on line 17
    PHP Fatal error:  Function name must be a string in /var/www/foo.php on line 17
    

    As you see, the error message is pretty clear. Because of using $mysql_query it thinks that mysql_query is a variable, but such a variable is not defined somewhere.

    0 讨论(0)
  • 2020-12-18 18:00

    please write this

    mysql_select_db($db_database, $db_server)
    

    instead of

    mysql_select_db($db_database)
    

    And write this

    $result = mysql_query($query);
    

    instead of

    $result = $mysql_query($query);
    

    One more thing please check your host name :$db_hostname = 'localhost:8888';

    By general it is $db_hostname = 'localhost:8080';

    So please confirm one more time

    0 讨论(0)
  • 2020-12-18 18:11

    $ symbol should be used only for variables not for functions

    $var=5;// variable declaration
    

    if you want to declare function syntax $ should not be used
    Ex:

    $function_var = mysql_query($query);
    


    In this function_var is variable and mysql_query($query) is function.

    0 讨论(0)
  • 2020-12-18 18:18

    Do you try connecting manually to your db by connection infor u set up in code,

    use phpmysql to connect to db first , change phpmysql config file to match the mysql server authentication infor, then ensure all are right before coding anything further.

    0 讨论(0)
  • 2020-12-18 18:19

    Stop using mysql_connect()!

    You should not use this or other related functions related to this extension.

    It's depreciated and removed in PHP 7.0.0 and beyond. An alternative is using PDO or MySQLi. Below is an example script for connecting to MySQL using PDO:

    <?php
    
      /* Connect to a MySQL database using driver invocation */
    
      /* DSN options:
         http://php.net/manual/en/ref.pdo-mysql.connection.php
    
         Error handling options:
         http://php.net/manual/en/pdo.error-handling.php
    
         Learn how to secure against SQL injection attacks:
         http://php.net/manual/en/pdo.prepared-statements.php
      */
    
      $user = 'myusername';
      $pass = 'mypassword';
      $host = 'localhost';
      $mydb = 'mydatabase';
    
      $dsn = "mysql:dbname=$mydb;host=$host"; 
    
      try {
          $dbh = new PDO($dsn, $user, $pass);
      } catch (PDOException $e) {
          echo 'Connection failed: ' . $e->getMessage();
      }
    
    ?>
    
    0 讨论(0)
  • 2020-12-18 18:20

    you can do it like below:

    <?php
        $conn = mysqli_connect("localhost","root","");
        if (!$conn)
        {
            die("Connection failed:".mysqli_connect_error());
        }
        $db = mysqli_select_db($conn,"Database_name");
        if(!$db)
        {
            echo "Connection failed";
        }
    ?>
    
    0 讨论(0)
提交回复
热议问题