Jump to content

pulling a single result from a DB using a query


whatnow

Recommended Posts

I do intend to minimse on posting stupid threads but please allow me this one...feckin noob that I am.

ALL i wanted to do was get a single result from my database. I've searched many online books my uni provide and i've trawled the net but I never got a proper explination of how to use mysql_db_query that I had to ask here.

I know how to call it, I just can't get my query right, take a look:

[code]
$cookie_info = explode("-", $_COOKIE['mysite_username']);
$usernameCookie = $cookie_info[0];

mysql_connect($server, $db_user, $db_pass) or die (mysql_error());
$result = mysql_db_query($database, "SELECT image FROM $table WHERE username = '$usernameCookie'") or die (mysql_error());

echo "username: $usernameCookie result: $result image: $image";

if (mysql_num_rows($result)) {
   echo "list of users:<ul>";
   while ($qry = mysql_fetch_array($result)) {
      echo "<li>$qry[username]</li>";
   }
   echo "</ul>end list of users.";
}[/code]
I chose the query becuase I have a table which is $table from my config file, this is read fine. The structure of the table is:

username
password
image

I can echo $username so it's read correctly from the cookie but I can't select the user's 'image' variable (URL of a image) from the DB. this is what's outputted from the code:

[!--html--][div class=\'htmltop\']HTML[/div][div class=\'htmlmain\'][!--html1--]username: qwerty result: Resource id #5 image: list of users:
end list of users.[!--html2--][/div][!--html3--]


It's mystified me, after a day learning php this is the thing that i've spent most time on and got nowhere with. After the 3rd hour I had to post on here as I was going around in circles !


tia.
Link to comment
Share on other sites

mysql_db_query has been depreciated, more info here:
[a href=\"http://us2.php.net/manual/en/function.mysql-db-query.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.mysql-db-query.php[/a]

try this and see if it works

[code]
<?php
$link = mysql_connect......
$db_select = mysql_select_db(<database name>, $link);

$query = "SELECT image FROM $table WHERE username = '$usernameCookie'";
$result = mysql_query($query, $link)
     or die("Couldn't select result".mysql_error() );

echo mysql_result($result, 0);
?>
[/code]
Link to comment
Share on other sites

Thanks for the speedy response. Your code looks a lot better, thank you. The problem is I still can't get it to work :(

here's what i've got:

[code]
<?php
//start a session to remember variables
session_start();

include "config.php";
include "header.inc";

$cookie_info = explode("-", $_COOKIE['mysite_username']);
$usernameCookie = $cookie_info[0];

$link = mysql_connect($server, $db_user, $db_pass)
$db_select = mysql_select_db($database, $link);

$query = "SELECT image FROM $table WHERE username = '$usernameCookie'";
$result = mysql_query($query, $link)
     or die("Couldn't select result".mysql_error() );

echo mysql_result($result, 0);

include 'footer.inc';
?>[/code]

that's the page in it's entirity. On viewing I get a white page :(
Link to comment
Share on other sites

[!--quoteo(post=351159:date=Mar 2 2006, 11:29 PM:name=earl_dc10)--][div class=\'quotetop\']QUOTE(earl_dc10 @ Mar 2 2006, 11:29 PM) [snapback]351159[/snapback][/div][div class=\'quotemain\'][!--quotec--]
hmmm... well, I can see that you forgot a ";" at the end of $link, that's about all I see though
[/quote]
thanks mate ;) all working like a dream now !

I really appreciate your input, this puts me well ahead of myself now.


mystery solved
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.