“Warning: mysql_query(): supplied argument is not a valid MySQL-Link” - Why?

前端 未结 7 1186
有刺的猬
有刺的猬 2021-01-26 04:17

What\'s wrong with my code? I keep getting this error: Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in functions.php on line 4 error readin

相关标签:
7条回答
  • 2021-01-26 04:43

    You forgot to open the connection to your database ($db in your code) and select a database before executing a query on it. The error says that the $db variable in your code is not a valid resource and thus it is not defined.

    See: http://php.net/manual/en/function.mysql-query.php and http://www.php.net/manual/en/function.mysql-connect.php

    0 讨论(0)
  • 2021-01-26 04:55

    I assume that $db is not a valid database connection. Did you connect to the database beforehand? Is $db available in that function's scope at all?

    You can make it have global scope by using global $db before calling the function.

    0 讨论(0)
  • 2021-01-26 04:56

    Well I'd say that $db is not initialized correctly. What you want to do is to use mysql_select_db as such :

    <?php
    $host = "localhost"; //database location
    $user = "user"; //database username
    $pass = "pass"; //database password
    $db_name = "thename"; //database name
    
    //database connection
    $link = mysql_connect($host, $user, $pass);
    mysql_select_db($db_name);
    
    //sets encoding to utf8
    mysql_query("SET NAMES utf8");
    ?>
    

    (snippet via)

    If you do need to have a specified database variable on each query for some reason, try looking if :

    • the $db variable is set properly

    • the $db variable is within the scope of your function. Consider making it global if needed or passing it to the function as an argument

    0 讨论(0)
  • 2021-01-26 04:59

    $db is not a local variable inside function gameTableCheck, you need to add a global $db; statement at the top of the function.

    0 讨论(0)
  • 2021-01-26 05:02

    The problem is here: mysql_query("SHOW TABLES LIKE '$gn'",$db)

    There is no $db in scope.

    If you are only using one database connection and you have already connected, you can just remove this argument.

    0 讨论(0)
  • 2021-01-26 05:06

    to find the error you can try putting mysql_error()

    $result = mysql_query("SHOW TABLES LIKE '$gn'",$db) or exit( mysql_error() );
    
    0 讨论(0)
提交回复
热议问题