Omael Posted January 8, 2014 Share Posted January 8, 2014 I have a simple query, but for some reason when I display the value of two columns they both return the value of the first column. Syntax: [ Download ] [ Hide ] [ Select ] <?phpmysql_connect($theserver,$username,$password);@mysql_select_db($database) or die( "Unable to select database");$query="SELECT column1, column2 FROM questions WHERE id = '$_GET[id]'";$result1=mysql_query($query);$num1=mysql_numrows($result1);mysql_close(); $colum1=mysql_result($result1,"column1"); $column2=mysql_result($result1,"column2");?><?php echo $column1; ?> <?php echo $column2; ?> For some reason column 2 displays the same value as column 1 but they have different values. Link to comment https://forums.phpfreaks.com/topic/285213-2-database-columns-displaying-the-same-values/ Share on other sites More sharing options...
adam_bray Posted January 8, 2014 Share Posted January 8, 2014 That method is very insecure. Try this - <?php mysql_connect($theserver,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); if( preg_match( "/[0-9]/", $_GET['id'] ) ) { $question_query = mysql_query( "SELECT column1, column2 FROM questions WHERE id = '$_GET[id]';" ) or die( 'MySQL Error: ' . mysql_error() ); $num1 = mysql_num_rows($question_query); while( $row = mysql_fetch_object( $question_query ) ) { echo $row->column1 . '<br />'; echo $row->column2 . '<br />'; } } else { die( 'Invalid ID' ); } mysql_close(); ?> Link to comment https://forums.phpfreaks.com/topic/285213-2-database-columns-displaying-the-same-values/#findComment-1464480 Share on other sites More sharing options...
Omael Posted January 8, 2014 Author Share Posted January 8, 2014 Hmm. I'm getting invalid ID when i try that. Link to comment https://forums.phpfreaks.com/topic/285213-2-database-columns-displaying-the-same-values/#findComment-1464481 Share on other sites More sharing options...
Omael Posted January 8, 2014 Author Share Posted January 8, 2014 I changed my query to this and it now works: mysql_connect($theserver,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query="SELECT column1, column2 FROM questions WHERE questionid = '$_GET[qid]'"; $result1=mysql_query($query); $num1=mysql_numrows($result1); while ($row = mysql_fetch_assoc($result1)) { $column1=$row['column1']; $column2=$row['column2']; } mysql_close(); Link to comment https://forums.phpfreaks.com/topic/285213-2-database-columns-displaying-the-same-values/#findComment-1464483 Share on other sites More sharing options...
Psycho Posted January 8, 2014 Share Posted January 8, 2014 Right, the problem was in the usage of mysql_result(). Per the manual, that function (which is deprecated) takes two parameters. The first is the result resource, which you provided. The second parameter is the 'row' which you did not provide. You provided strings corresponding to the column names. But, the manual states: The row number from the result that's being retrieved. Row numbers start at 0. EDIT: Since you are only expecting 1 row, no need to use a while() loop. if(mysql_numrows($result1)) { $row = mysql_fetch_assoc($result1); $column1=$row['column1']; $column2=$row['column2']; } else { //Error condition } Link to comment https://forums.phpfreaks.com/topic/285213-2-database-columns-displaying-the-same-values/#findComment-1464484 Share on other sites More sharing options...
Omael Posted January 8, 2014 Author Share Posted January 8, 2014 Thank you Pcycho Link to comment https://forums.phpfreaks.com/topic/285213-2-database-columns-displaying-the-same-values/#findComment-1464490 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.