Echo Result of MySQL SELECT Query in PHP

Jason Stallard

I have page where I want to display a single value from one table based on a value found in another:

$username = 'Joe';
$query = 'SELECT * FROM chars WHERE uname = "$username"';
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_assoc($result);
$query = 'SELECT cname FROM chars INNER JOIN usrs 
    ON chars.cid = usrs.clastused WHERE usrs.uname = "$username"';
$q = mysql_query($query) or die(mysql_error());
$r = mysql_fetch_assoc($q);
echo '<p>q = "'.$q.'" and r = "'.$r['cname'].'"</p>'; //for debugging
foreach ($row['cname' as $opt) {
    $output .= '<option value=\"'.$opt.'\"';
    if ($opt=$r) {$output .= ' selected';}
    $output .= '>'.$opt.'</option>';
}

PHPMyAdmin gives the response I expect CNAME Joe and $q gives Resource id#5 but $r is blank. How do I echo the result? What I want at the end of the day is to have a dropdown list where the clastused is the preselected option.

Jason Stallard

I fixed this by using the array variable directly:

foreach ($r as $opt) {
    $output .= '<option value=\"'.$opt.'\"';
    if ($opt=$cname) {$output .= ' selected';}  
    $output .= '>'.$opt.'</option>';
}

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

MySQL select query that doesnt have to return a result set

분류에서Dev

Loop the select query result set in MYSQL Stored Procedure

분류에서Dev

Better query to select the max value of an aggregation with a count result and retain another column MySQL?

분류에서Dev

Difference in select query result on MyISAM Vs InnoDB MySQL engines(especially for FULL TEXT SEARCHES)

분류에서Dev

mysql select recursive query

분류에서Dev

MySQL Case select query

분류에서Dev

Loop array in mysql query and for the result

분류에서Dev

php codeigniter select query

분류에서Dev

mysql_fetch_array ($ result) echo $ rows in html

분류에서Dev

다중 옵션 echo php & mysql

분류에서Dev

Multiple select query with concatinated result displayed

분류에서Dev

How to select a specific part of a SQL query result?

분류에서Dev

PHP different result of error_log and echo in different contexts

분류에서Dev

PHP MySQL SELECT WHERE NOT IN

분류에서Dev

Run MySQL Query with ü by php

분류에서Dev

MySql query on PHP

분류에서Dev

MySQL join query result filtered by tags

분류에서Dev

Removing \n from result of mysql query

분류에서Dev

MySQL JOIN multiple query result to 1 result set

분류에서Dev

MYSQL - Return Result of one SELECT minus another SELECT

분류에서Dev

count rows and echo the result

분류에서Dev

PHP mysql 2 select to 1 select

분류에서Dev

PHP mySQL select via html select

분류에서Dev

php mysql select data to gridview

분류에서Dev

MySQL query not efficient (PHP search)

분류에서Dev

php mysql return wrong boolean result

분류에서Dev

기본 PHP-echo mysql, 어떻게?

분류에서Dev

echo 3 열 값 (병합) php mysql

분류에서Dev

Mysql Insert query with values and select as subquery