Page 1 of 1

2 database columns displaying the same values??

Posted: Wed Jan 08, 2014 1:48 pm
by omarel
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.

Code: Select all

<?php
mysql_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.

Re: 2 database columns displaying the same values??

Posted: Wed Jan 08, 2014 2:06 pm
by twinedev
what if instead of the two mysql_result lines, you do:

Code: Select all

$row = mysql_fetch_assoc($result1);

// Debug Test:
var_dump($row);
Keep in mind the that mysql_whatever function set is deprecated and is not in the next release of PHP. you may want to look into using PDO instead.

Additionally, using $_GET directly in the SQL statement is a bad idea, as that allows for SQL injection.

Lastly, it is a bad idea to suppress errors for a function unless you have a specific reason to do so. (the @mysql_select_db )

Re: 2 database columns displaying the same values??

Posted: Wed Jan 08, 2014 2:14 pm
by omarel
I get

Code: Select all

array(2) { ["column1"]=> string(31) "What is the purpose of writing?" ["column2"]=> string(1) "0" } 
So it seems to be capturing the right values.

How would I display each value?

Re: 2 database columns displaying the same values??

Posted: Wed Jan 08, 2014 2:40 pm
by omarel
Thanks you definitely sparked it. I changed my query to this and it's accurate now:

Code: Select all

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();