php php变量中的mysql查询结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5157905/
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-25 16:58:49  来源:igfitidea点击:

mysql query result in php variable

phpmysql

提问by no_freedom

Is there any way to store mysql result in php variable? thanks

有没有办法将mysql结果存储在php变量中?谢谢

$query = "SELECT username,userid FROM user WHERE username = 'admin' ";
$result=$conn->query($query);

then I want to print selected userid from query.

然后我想从查询中打印选定的用户 ID。

回答by mailo

Of course there is. Check out mysql_query, and mysql_fetch_rowif you use MySQL.
Example from PHP manual:

当然有。查看mysql_querymysql_fetch_row如果您使用 MySQL。 PHP 手册中的
示例:

<?php
$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}
$row = mysql_fetch_row($result);

echo $row[0]; // 42
echo $row[1]; // the email value
?>

回答by UltraInstinct

There are a couple of mysql functions you need to look into.

您需要查看几个 mysql 函数。

  • mysql_query("query string here") : returns a resource
  • mysql_fetch_array(resource obtained above) : fetches a rowand return as an array with numerical and associative(with column name as key) indices. Typically, you need to iterate through the results till expression evaluates to falsevalue. Like the below:

    while ($row = mysql_fetch_array($query)){
        print_r $row;
    }

    Consult the manual, the links to which are provided below, they have more options to specify the format in which the array is requested. Like, you could use mysql_fetch_assoc(..)to get the row in an associative array.

  • mysql_query("query string here") : 返回一个资源
  • mysql_fetch_array(上面获得的资源):获取一行并作为具有数字和关联(列名作为键)索引的数组返回。通常,您需要遍历结果直到表达式计算为false值。像下面这样:

    while ($row = mysql_fetch_array($query)){
        print_r $row;
    }

    查阅手册,下面提供了链接,他们有更多的选项来指定请求数组的格式。就像,您可以mysql_fetch_assoc(..)用来获取关联数组中的行。

Links:

链接:

In your case,

在你的情况下,

$query = "SELECT username,userid FROM user WHERE username = 'admin' ";
$result=mysql_query($query);
if (!$result){
    die("BAD!");
}
if (mysql_num_rows($result)==1){
    $row = mysql_fetch_array($result);
    echo "user Id: " . $row['userid'];
}
else{
    echo "not found!";
}

回答by rlb.usa

$query="SELECT * FROM contacts";
$result=mysql_query($query);

回答by Zyllox

I personally use prepared statements.

我个人使用准备好的语句。

Why is it important?

它为什么如此重要?

Well it's important because of security. It's very easy to do an SQL injection on someone who use variables in the query.

嗯,这很重要,因为安全。对在查询中使用变量的人进行 SQL 注入非常容易。

Instead of using this code:

而不是使用此代码:

$query = "SELECT username,userid FROM user WHERE username = 'admin' ";
$result=$conn->query($query);

You should use this

你应该用这个

$stmt = $this->db->query("SELECT * FROM users WHERE username = ? AND password = ?");
$stmt->bind_param("ss", $username, $password); //You need the variables to do something as well.
$stmt->execute();

Learn more about prepared statements on:

了解更多关于准备好的声明:

http://php.net/manual/en/mysqli.quickstart.prepared-statements.phpMySQLI

http://php.net/manual/en/mysqli.quickstart.prepared-statements.phpMySQLI

http://php.net/manual/en/pdo.prepared-statements.phpPDO

http://php.net/manual/en/pdo.prepared-statements.phpPDO

回答by Faiz Ullah

$query    =    "SELECT username, userid FROM user WHERE username = 'admin' ";
$result    =    $conn->query($query);

if (!$result) {
  echo 'Could not run query: ' . mysql_error();
  exit;
}

$arrayResult    =    mysql_fetch_array($result);

//Now you can access $arrayResult like this

$arrayResult['userid'];    // output will be userid which will be in database
$arrayResult['username'];  // output will be admin

//Note- userid and username will be column name of user table.