14

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

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

then I want to print selected userid from query.

UltraInstinct
  • 43,308
  • 12
  • 81
  • 104
no_freedom
  • 1,963
  • 10
  • 30
  • 48
  • 5
    We don't know what database wrapper you are using, but whichever one it is, its manual is guaranteed to have an example for this very, very basic thing. Not meant to be rude, but taking that path is really preferable here. Voting to close. – Pekka Mar 01 '11 at 17:17

5 Answers5

59

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

<?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
?>
mailo
  • 2,601
  • 22
  • 19
  • 2
    @serialdownvoter If something is not correct, explain why, both to better our knowledge, and so that we can help you better. – rlb.usa Mar 01 '11 at 17:19
6

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

  • mysql_query("query string here") : returns a resource
  • mysql_fetch_array(resource obtained above) : fetches a row and 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 false value. 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.

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!";
}
UltraInstinct
  • 43,308
  • 12
  • 81
  • 104
2
$query="SELECT * FROM contacts";
$result=mysql_query($query);
rlb.usa
  • 14,942
  • 16
  • 80
  • 128
  • I think you misunderstood the question: no_freedom wanted a way to store the value of the query without having to actually run the query. – Seth Connell Aug 22 '17 at 16:37
-1

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.

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.php MySQLI

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

SuppressWarnings
  • 4,134
  • 4
  • 25
  • 33
Zyllox
  • 13
  • 4
-1
$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.
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459