If I have a mySQL database, and I want to call the data from a specific field in the database, i.e. <? echo $field1 ?> what code should I be adding to the .php file to call the data? I have created a .inc.php file with database username, password etc. but I'm not sure where to go from here. This is my first attempt at PHP. Darren
we have the file that connects the database: db.inc.php the database table: test ->id ->name the script that gets 'name' field: <?php $sql = "select * from test where id=1"; //the query $query = mysql_query($sql) or die("Error: ".mysql_error()); $row = mysql_fetch_assoc($query); echo $row['name']; //name is the specific field ?> PHP:
Thanks, but I don't want to run a query. I want to call up the mysql database, and then call up the contents of a field in the database. Simple example: <title>My favourite DVD - <? echo $film ?></title> The field is film within a table called dvds and the table is within a mysql database. I want the title to be <My favourite DVD - Pirates of the Caribbean</title> Pirates of the Caribbean is the default value in the field 'film' I know this sounds a silly scenario but I am trying to learn mysql and php so at the moment its just to test and play around. Hope this is understandable.
Ehm... You HAVE to do a query before you can show anything. You might want to do a few tutorials first.
True. The title "$film" that you are trying to display has to be queried (i.e. retrieved) from the database.
You'll find that once you get started with PHP, it takes on a snowball effect and only gets easier from there.
One small hint they usually tend to forget in PHP manuals; Put the database-connection-code into an include file so you won't have to code it everytime. When you have created the include file, simply call it before a query, like: include('inc.dbconnect.php'); $result = mysql_query("SELECT * FROM tablename;"); Code (markup): mod.dbconnect.php: <?php $db = mysql_connect("localhost","root","password here"); mysql_select_db("dbname",$db) or die("error!"); ?> Code (markup): Never name an include file *.inc! Most books say you should use the .inc extension but the php server doesn't recognize .inc as a php file and will parse the file as a normal textfile, thereby exposing your code. Of course you can use htaccess to ignore it but you are just adding an extra security risk.
http://www.webmonkey.com/webmonkey/programming/php/tutorials/tutorial4.html That tutorial got me started in PHP, really great, I suggest anyone trying to learn PHP to read it. Just skip the first 2-3 pages that talk about how to setup PHP on the server and go to the actual PHP coding. It also teaches you how to interact with the MySQL server without PHPMyAdmin, You can do all the MySQL stuff he talks about in PHPMyAdmin though just in the MySQL query box.
This is very true unless your host has configured the .inc file to be recognized as a .php, otherwise someone could easily find your Database username and password.
Never a truer word sopken!! I used to use .inc files until I met a guy who "as a friend" hacked a system of mine. Luckily he was a friend and he told me about it straight away and helped me fix the security issues, but as Casper said NEVER name a file .inc!!
The other posters have it right. Just because your host has configured this does not mean that you should do it. Always code in a manner which will work as expected on any machine and under any OS. You never know when you need to move or when your host may change their policies to reflect a more "standards compliant" approach to using PHP and other programs.
U probably mean http://www.phpclasses.org Good resource but the amount of ads is shocking. So annoying. You come to expect ad free sites with the open source movement but I suppose they have to pay for hosting