FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Embarrassing newbie php/SQL questions





riv_
OK, I'm brand new at this, and I expect this to be the first of many questions. I've checked in all the appropriate places and I still just don't get it, and I'm finally frustrated enoug to expose myself as the moron I am and just ask you folks for help. THis is kindergarten stuff.
I'm just tryin to test a user entered string against a string from my db.
I've distilled my script down to the bare bones (because it doesn't work) and have found that the problem, of course, is that the stuff coming OUT of my database, doesn't look like what I put IN.
For example, if I've done a query say... SELECT * FROM stuff WHERE x = $x, and then a fetch array.
($x is user entered on html form, and c in the database is "blah")
and then I test it... it keeps coming back false...
sure enough when I check the output...
x from the database is "Resource id#5", but I'm expecting it to be "blah"
What the heck is "Resource id#5"?
Where is it coming from?
How do I turn it into "blah"?
Am I too stupid to do this from, scratch?
Is there a better way/place to learn this stuff from scratch?
macace8
I'm not totally sure, but I think when you say:
SELECT * FROM stuff WHERE x = $x
you need to instead say:
SELECT * FROM stuff WHERE x = '$x'
I think the synatax calls for single quotes around the variable. If that dosn't work, you can always just do
SELECT * FROM stuff
and then use a loop to search through the database for the data you want. For instance:


$query='SELECT * FROM users ORDER BY name ASC';
$result=mysql_query($query);
$num=mysql_numrows($result);
$foundname="null";
$index=0;
while ($i < $num)
{
$listname=mysql_result($result,$i,'name');
if($listname==$x)
{
$foundname=$listname;
$index=$i;
}
}

After this code, $index is the index of the found data and $foundname is the data. Feel free to ask if you have any questions.
riv_
I actually do have the single quotes in the script, but I forgot to include them here. (Sorry!)
I'm not sure if that helps.
What I'm trying to do is to see if individual fields on the user-submitted form match individual fields in the fetched array.
I thought this was straightforward, but...
Using
Quote:
$query=("SELECT * FROM stuff WHERE x = '$x'");
$result= "mysql_fetch_array($query)";
echo "$result['fielda']"."$result['fieldb']";

Yields the wacky "Resource id#6 Resource id#4" stuff... when I'm expecting "stuff data"
Anyways, I simply want to test to see if $posted_fielda==$result['fielda'] etc.
That part's pretty straight forward. (I think)
I just can't figure out why the string coming out of my database doesn't look like what I put in. It's pulling information out, but it's not the same.
(THe code listed in this post is identical to what's in my script, but I've changed the $names to make it a little shorter.
Please help me! I'm missing something!
mathiaus
your using mysql_fetch_array all wrong. Check out this page and look at the example code
http://uk.php.net/mysql_fetch_array
mano
Quote:

$query=("SELECT * FROM stuff WHERE x = '$x'");
$result= "mysql_fetch_array($query)";
echo "$result['fielda']"."$result['fieldb']";


should be:

$query= mysql_query("SELECT * FROM stuff WHERE x = '$x'");
$result = mysql_fetch_array($query);
echo $result['fielda'].$result['fieldb'];

By the way, quotes around variable is not neccessary if it is know that the value is integer
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.