php mysql_query() 期望参数 1 是字符串,给定资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8806148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:32:56  来源:igfitidea点击:

mysql_query() expects parameter 1 to be string, resource given

php

提问by AphexBeirut

so my code is this..

所以我的代码是这样的..

<?php

$password=(!isset($_POST['password']));
$username=(!isset($_POST['username']));

    $username = mysql_real_escape_string($username);
    $password = mysql_real_escape_string($password);

$query = mysql_query ("SELECT * FROM tb_funcionario WHERE username='$username' and password='$password'");

   $result = mysql_query($query);

   var_dump($result);

   $num_rows = $result->$num_rows;

    if ($num_rows)
    {
        echo "username already exist";
    }
    else
    {
     $query = "INSERT INTO tb_funcionario (nome_funcionario, username, password) VALUES (
    '$_POST[nome_funcionario]',
    '$_POST[username]',
    '$_POST[password]'
    )";;

       $result = mysql_query($query) or die (mysql_error());

    }


        mysql_query($query);
        mysql_close($bd_con);
?>

And its always giving me the "mysql_query() expects parameter 1 to be string, resource given" and i cant figure out how to solve it.

它总是给我“mysql_query() 期望参数 1 是字符串,资源给定”,我不知道如何解决它。

Can yall help me?

你能帮我吗?

回答by David

This is your problem:

这是你的问题:

$query = mysql_query ("SELECT * FROM tb_funcionario WHERE username='$username' and password='$password'");

$result = mysql_query($query);

You're running a query on the first line, which returns a "resource" as a result of the query. Then on the immediate next line you try to use that resource as another query to run again. You don't need the second line, the $resultcan be set in the first line.

您在第一行运行查询,该查询返回“资源”作为查询结果。然后在紧接的下一行,您尝试将该资源用作另一个查询以再次运行。你不需要第二行,$result可以在第一行设置。

回答by miku

You have some unnecessary/wrong lines in your code, especially:

您的代码中有一些不必要/错误的行,尤其是:

$query = mysql_query ("SELECT * FROM tb_funcionario WHERE 
    username='$username' and password='$password'");

$result = mysql_query($query);

I guess you wanted to write:

我猜你想写:

$query = "SELECT * FROM tb_funcionario WHERE 
    username='$username' and password='$password'";

$result = mysql_query($query);

回答by jere

your problem is here $query = mysql_query ("SELECT * FROM tb_funcionario WHERE username='$username' and password='$password'");

你的问题在这里 $query = mysql_query ("SELECT * FROM tb_funcionario WHERE username='$username' and password='$password'");

then you are running mysql_query($query)and it's trying to run the command against the resultset returned by the first statement, not a string like it should actually be

那么你正在运行mysql_query($query),它试图针对第一个语句返回的结果集运行命令,而不是像它实际上应该的字符串

回答by Crontab

$query = mysql_query ("SELECT * FROM tb_funcionario WHERE username='$username' and password='$password'");
$result = mysql_query($query);

Is incorrect. You should just be assigning the query string to $query- what you're doing is running the query once, then trying to run the result of the query as a query again. You should have:

是不正确的。您应该只是将查询字符串分配给$query- 您所做的是运行一次查询,然后尝试再次将查询结果作为查询运行。你应该有:

$query = "SELECT * FROM tb_funcionario WHERE username='$username' and password='$password'";
$result = mysql_query($query);

which would give you the results you seek.

这会给你你寻求的结果。

回答by Nobita

Look at this:

看这个:

$query = mysql_query ("SELECT * FROM tb_funcionario WHERE username='$username' and password='$password'");
$result = mysql_query($query);

Change it for:

将其更改为:

$query = "SELECT * FROM tb_funcionario WHERE username='$username' and password='$password'");
$result = mysql_query($query);

回答by jbrtrnd

<?php
$query = "INSERT INTO tb_funcionario (nome_funcionario, username, password) VALUES (
        '".mysql_real_escape_string($_POST[nome_funcionario])."',
        '".mysql_real_escape_string($_POST[username])."',
        '".mysql_real_escape_string($_POST[password])."'
        )";
?>

回答by Yann Boisclair-Roy

Your problem comes from those lines:

你的问题来自这些行:

$query = mysql_query ("SELECT * FROM tb_funcionario WHERE username='$username' and    password='$password'");

$result = mysql_query($query);

Your calling the mysql_query()function twice.

您调用该mysql_query()函数两次。

You can do:

你可以做:

$sql = "SELECT * FROM tb_funcionario WHERE username='$username' and password='$password'";

$result = mysql_query($sql);

Or either:

或者:

$result = mysql_query( "SELECT * FROM tb_funcionario WHERE username='$username' and password='$password'"; );