Forum Moderators: coopster

Message Too Old, No Replies

connecting to a database

         

wincode

7:41 am on Jan 1, 2011 (gmt 0)

10+ Year Member



Hello, how can I get this code to read from the database?

$column="'name'";
$sql=mysql_query("SELECT * FROM table where id='1'");
$row=mysql_fetch_array($sql);
$value=$row[$column];
echo $value;


Please help

Thank you for your help

wincode

9:15 pm on Jan 1, 2011 (gmt 0)

10+ Year Member



Should've added what the problem is..
The problem is that the code above doesn't retrieve the date from the database.
This code works perfectly though:
$sql=mysql_query("SELECT * FROM table where id='1'");
$row=mysql_fetch_array($sql);
$value=$row['name'];
echo $value;


But how can it work if you have several different columns and want to use something like $row[$string]?

Thanks

jecasc

9:24 pm on Jan 1, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




$column="'name'";

Have you tried this:

$column = "name";

Unless the name of your column is not name but 'name' "'name'" won't work.

Matthew1980

10:25 pm on Jan 1, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

$sql=mysql_query("SELECT * FROM table where id='1'");
$row=mysql_fetch_array($sql);
$value=$row['name'];
echo $value;


Just pointing to a possible potential problem. Numerical values shouldn't be quoted, as sql will treat these values as a string, or floating point values (so I have been told), but I know that quoting numerical values WILL give you headaches (Thanks to Rocknbil for helping me with this a few times!)

$SqlString = "SELECT * FROM `table` WHERE `id` = 1 ";
$sql = mysql_query($SqlString) or die(mysql_error());

$row = mysql_fetch_array($sql);

//debug returned array to see if your expected value is there :)
echo "<pre>";
print_r($row['name']);
echo "</pre>";

echo "</br>";
echo $row['name'];


Also build the sql statement into a string so that it is easier to debug, and when deciding your column names, try to choose names that aren't reserved words in sql, else you WILL need to use back ticks to 'escape' these names, this needs to be done when your column names have spaces in too.

Hope that makes sense anyway.

Cheers,
MRb

wincode

10:50 pm on Jan 1, 2011 (gmt 0)

10+ Year Member



You're right Matthew, I actually just used that code as a cleaner version of the code on my page, which explains why I accidentally put quotes around the 1 :P
But I don't know why, when I tried
$column="name";

it still wouldn't work, but now it's working perfectly

Thanks for the help!

rocknbil

7:27 pm on Jan 3, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're missing something important: it's the fieldname that shouldn't be quoted, not the values. You can, and is often recommended to avoid reserved word conflicts, backtick field and table names.

Incorect:

select 'fieldname' from table:

Both correct:
select fieldname from table;
select `fieldname` from `table`;

Matthew is referring to selecting values:

select fieldname from table where numeric_field='1233';

However (sorry M :) ) he is confusing PHP and mysql. When you quote a string in PHP,

$num = '1234';

it casts it as a string, as opposed to

$num = 1234;

However, this is not true in mysql. Both of these are valid, and both will return the field value:

select fieldname from table where numeric_field='1233';

select fieldname from table where numeric_field=1233;

However, a *really good reason* not to use quotes on numeric selections is that it may give you unexpected results, causing you to look in the wrong places. If you have something like this,

$num = null; // or unset, or a string value like 'abc'

And you execute it on a numeric field,

select fieldname from table where numeric_field='$num';

You'll just get an empty set, no error. But like this,

select fieldname from table where numeric_field=$num;

you will get a syntax error from mysql. So it makes it easier to debug to use the right quoting syntax, even if other things work.

Matthew1980

7:35 pm on Jan 3, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>>However (sorry M :) ) he is confusing PHP and mysql.

Thanks Rocknbil, I was rather tired when I posted that, I had a feeling as I had got them upside down/back to front. Can happen to the best of us ;)

Cheers,
MRb