Jump to content


Photo

pulling a single result from a DB using a query


  • Please log in to reply
4 replies to this topic

#1 whatnow

whatnow
  • Members
  • PipPip
  • Member
  • 12 posts
  • Locationuk

Posted 02 March 2006 - 10:40 PM

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:

$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."; 
}
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.

#2 earl_dc10

earl_dc10
  • Members
  • PipPipPip
  • Advanced Member
  • 71 posts

Posted 02 March 2006 - 10:56 PM

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

<?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);
?>

got a problem? Google helps many of those in need

#3 whatnow

whatnow
  • Members
  • PipPip
  • Member
  • 12 posts
  • Locationuk

Posted 02 March 2006 - 11:11 PM

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:

<?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';
?>

that's the page in it's entirity. On viewing I get a white page :(

#4 earl_dc10

earl_dc10
  • Members
  • PipPipPip
  • Advanced Member
  • 71 posts

Posted 02 March 2006 - 11:29 PM

hmmm... well, I can see that you forgot a ";" at the end of $link, that's about all I see though
got a problem? Google helps many of those in need

#5 whatnow

whatnow
  • Members
  • PipPip
  • Member
  • 12 posts
  • Locationuk

Posted 02 March 2006 - 11:33 PM

[!--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) View Post[/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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users