PDA

View Full Version : Total PHP/MySQL noob, how do I grab a row and then output it?



Recursion
16-05-2010, 01:59 PM
Hey,

I'm a total n00b with PHP/MySQL and I'm learning so;

How do I grab the data from a MySQL table? I have a table called siteconfig and in there I want to grab the single result from sitename.

Now, I did:


$sitenquery = "SELECT sitename FROM siteconfig";
$sitendb = mysql_query($sitenquery);
$sitename = mysql_result($sitendb);


and then:


<title><?php echo($sitename); ?></title>


But it doesn't work?

Sorry if this is a stupid question but I am still learning! :(

MattFr
16-05-2010, 02:23 PM
If you wanted a single row and you knew, for example, that the ID was, again for example, 1, you could do this:



$query = mysql_query("SELECT * FROM `siteconfig` WHERE `id` = '1'");
$row = mysql_fetch_assoc($query);

echo $row['sitename'];

That should work :)

Apolva
16-05-2010, 03:19 PM
If you just want one field, use mysql_result like you tried.



<?php
$query = mysql_query("SELECT `sitename` FROM `siteconfig`;");
$sitename = mysql_result($query,0); // From the 0th row

echo $sitename;
?>
If you want the whole result set in an array, use MattFr's method.

Want to hide these adverts? Register an account for free!