Page 1 of 1

SELECT a specific value

Posted: Tue Feb 08, 2011 3:32 pm
by LuaMadman
I just started using mysql.
I manage to connect to the database just fine, but i got a problem getting a column.
First i ran the following sql statement to get my table.

Code: Select all

CREATE TABLE bytin(
notloggedin TEXT NOT NULL);
INSERT INTO (notloggedin) values ("My Text")
Then in my php file i try to retive My Text
I manged to get the value using

Code: Select all

		Not logged in...<br/>
<?php
		$sql = "SELECT * FROM bytin";
		$result = mysql_query($sql);
		print($result . '<br/>');
		while($line = mysql_fetch_array($result)) {
			print($line['notloggedin']);
		}
However, accoring to me this would print notloggedin text twice, if i was to add another column.
So no matter what, it's a bad and ugly soluion.
This gives

Code: Select all

Not logged in...
Resource id #369
My Text
If it would been an array, i figuer it would be like this
bytin(
notloggedin = "My Text",
)
Beacuse then i would just need to get the value of the notlogged in key. That is what i'm trying to accaomplis here.

Hope i made some sense on what i want.

Re: SELECT a specific value

Posted: Tue Feb 08, 2011 3:40 pm
by Jade

Code: Select all

<?php
                $sql = "SELECT * FROM bytin"; //here's your query
                $result = mysql_query($sql) or die (mysql_error()); //this is a mysql resource ID, not the results
                 $row = mysql_fetch_array($result); //this is your result of your query
                print_r($row);
               echo $row['notloggedin'];
?>

Re: SELECT a specific value

Posted: Tue Feb 08, 2011 3:41 pm
by John Cartwright
Be careful when describing things in technical terms. Columns != rows.

Re: SELECT a specific value

Posted: Tue Feb 08, 2011 3:52 pm
by LuaMadman
Thanks for the help.

As for the $result var, i pretty much copied the code from a book, so when it said result, I asumed it meant, that i was my result.
So thanks for explaining exactly what it was.