mysql query result in php variable

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.

This question and answers originated from www.stackoverflow.com
Question by (3/1/2011 5:16:18 PM)

Answer

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
?>
Answer by

Find More Answers
Related Topics  php  mysql
Related Questions
  • looping MySQL query result in PHP

    $b = 1; $d = mysql_query("select *from 'table_name'"); while($b < 10){ while($e = mysql_fetch_array($d)){ echo $e['name_field']; } $b++; } I want to loop the output, but I'm stuck. …
  • MySQL query php variable in variable?

    I have a PHP function that makes a query to MySQL DB. function regEvent($event, $l) { $sqlz_upd="UPDATE {$event} SET f1a='$_POST[F1A"'.$l.'"]'"; The question is what is the syntax to use vari…
  • Query time result in MySQL w/ PHP

    Is there a way that I can get the time of a MySQL query (specifically with PHP)? The actual time it took to complete the query, that is. Something such as: Results 1 - 10 for brown. (0.11 seconds…
  • Variable from URL in PHP MySQL query

    I'm trying to get a variable from the URL, but for some reason, I can't get it to work. This is all coming from another website's form, that's why I need to get it from the URL. This is what I have …
  • Query result in MySQL Console does not match the result of PHP's mysql_query()

    My MySQL query returns different results depending on how the query is submitted. When the query is submitted through the MySQL Console results in. mysql> SELECT `modx`.coverage_nation.id, …
  • Printing result of mysql query from variable

    So I wrote this earlier (in php), but everytime I try echo $test", I just get back resource id 5. Does anyone know how to actually print out the mysql query from the variable? $dave= mysql_query(…
  • PHP MYSQL query result "RANKING"

    I need to get a list of users Ranking by points and from my command line (MySQL) is was able to generate the necessary code: SET @rank=0; SELECT rank, iduser, pontos FROM ( SELECT @rank:=@rank+1 …
  • Search in array query result in php/mysql

    I have the mysql results ( $date_options ) as below Array ( [0] => stdClass Object ( [id] => 4 [start_date] => 2010-09-29 ) [1] => stdClass Object ( [id] => 13 …
  • PHP and 'variable' mySQL query

    I know I do many, many things wrong in this, but I don't have a clue how to fix this (except making many separate queries. mysql_query( 'SELECT * FROM opdracht where'. if(isset($opleidin…
  • MySQL query in PHP gives obvious wrong result

    I'm using PHP and PHPMyAdmin to create a small profile site. I'm giving members an ID number, based on which is the biggest number currently in the database, +1 I did 25 tests before I got the PH…