How to get data in PHP from MySQL?

How to get data in PHP from MySQL ?
I am doing this, but no output is coming on the page.
Can someone please help?


$result = mysql_query("select 'post-name' from mytable where id=1 LIMIT 1");
$row = mysql_fetch_array($result);
echo nl2br($row);

My full

first thing you need to do is test your query outside of php

are you using any mysql front-end application? like heidisql, sqlyog, the mysql query browser, phpmyadmin, anything like that? or just the dreaded mysql command line?

try your query in mysql and see what it produces before attempting to incorporate it into a php script

I’m assuming you have confirmed your php connection to the database has been established.

mysql_fetch_array() returns an indexed and associative array of the rows returned by your query.

if you need to, you can check how many rows were returned by using mysql_num_rows().

to display the data returned from the query you need to loop through your result set $result.

you can’t just say


[COLOR=#000066][COLOR=#000066]echo[/COLOR] [COLOR=#000066]nl2br[/COLOR][COLOR=#66cc66]([/COLOR][COLOR=#0000cc]$row[/COLOR][COLOR=#66cc66])[/COLOR];
[/COLOR]

[COLOR=#000066]example: (I’m using mysql_fetch_assoc() but you can use mysql_fetch_array())


[COLOR=#000066][COLOR=black]$query = 'select * from tblUsers';[/COLOR]
 
[COLOR=black]$rs = mysql_query($query,$conn);[/COLOR]
 
[COLOR=black]while($row=mysql_fetch_assoc($rs)) {[/COLOR]
[COLOR=black]   echo 'first name = '.$row['fldFname'].' last name = '.$row['fldLName'].'<br />';[/COLOR]
[COLOR=black]}[/COLOR][/COLOR]

the keys in $row must be the column names (or the aliases) in the table name in your query.[/COLOR]