Long Break from MySQL...

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Seifer
Forum Commoner
Posts: 25
Joined: Wed Oct 15, 2003 10:03 pm
Location: Columbia City, IN
Contact:

Long Break from MySQL...

Post by Seifer »

I took a long break from MySQL, and scripting..And I am, I hate to say, a newbie again. I am currently working on a self project, an online game. Basically, I need to know how to print out a line from the MySQL database, I know this sounds stupid, but I can't figure it out, it may be the lack of sleep plus the amount of caffiene, who knows. :). Here is my code so far that doesn't work, hopefully I am close:

Code: Select all

<?php
$uname = "random";
$pword = "random";
$db = "random";
mysql_connect("localhost", $uname, $pword);
mysql_select_db($db);
$result = mysql_query("SELECT gold FROM user_info");
mysql_fetch_array($result);
echo($result);
?>
Thanks in advance.
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

Looks like you still got a few of PHP left in you. Let's bring it back up. Here:

Code: Select all

<?php
$uname = "random";
$pword = "random";
$db = "random";
$connection = mysql_connect("localhost", $uname, $pword);
$db = mysql_select_db($db, $connection);
$result = mysql_query("SELECT gold FROM user_info", $connection) or die(mysql_error);
$row = mysql_fetch_array($result);
echo $row['column_name'];
?>
First mistake, you should store the mysql_connect into a variable for later use.

Second, now that the mysql_connect connection is stored in $connection, you have to use that as a second argument in mysql_select_db. And yeah, also store mysql_select_db in a variable.

Now, same for mysql_query, you'll have to use $connection as a second argument so PHP knows which connection to use.

I added the or die(mysql_error()). Not neccessary but very useful incase of errors.

Now store the mysql_fectch_array, which will return an array, into a variable for using as array.

The rest is fine, except you should echo out what column you want by using the $row['name_of_column'].

-Nay
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

I just wanted to offer an alternate suggestion. If you are only selecting one field in the query, mysql_result() might be used to retrieve the results...

Code: Select all

$result = mysql_query("SELECT gold FROM user_info", $connection) or die(mysql_error);
echo mysql_result($result,0); // first row
echo mysql_result($result,3); // 2nd row
Seifer
Forum Commoner
Posts: 25
Joined: Wed Oct 15, 2003 10:03 pm
Location: Columbia City, IN
Contact:

Post by Seifer »

Wow, thanks guys. It seems common sense to me again, as I am sure much will, since I did quite a bit of MySQL stuff on my last projects (a little over a year ago). Had to take a break as I was a freshman in High School and was overloaded with book work, but now I am a sophomore and back to my true love, PHP and MySQL :P. Thanks again.
Post Reply