Screwey Database
I'm trying to set up a simple database that contains users:
name
email
usename
password
admin status
I'm trying to display the info via PHP, but its not working. When I do this:
<?php
<HTML>
$id = $_GET[id];
$db = mysql_connect(\"localhost\", \"root\", \"\");
mysql_select_db(\"test\",$db);
$result = mysql_query(\"SELECT * FROM users WHERE id=$id\",$db);
$myrow = mysql_fetch_array($result);
echo \"First Name: \".$myrow[\"name\"];
echo \"<br>Last Name: \".$myrow[\"email\"];
echo \"<br>Nick Name: \".$myrow[\"username\"];
echo \"<br>Email address: \".$myrow[\"password\"];
echo \"<br>Salary: \".$myrow[\"admin\"];
</HTML>
?>
it works
when I do this, however:
<?php
<HTML>
$u = $_GET[u];
$db = mysql_connect(\"localhost\", \"root\", \"\");
mysql_select_db(\"test\",$db);
$result = mysql_query(\"SELECT * FROM users WHERE admin=$u\",$db);
$myrow = mysql_fetch_array($result);
echo \"First Name: \".$myrow[\"name\"];
echo \"<br>Last Name: \".$myrow[\"email\"];
echo \"<br>Nick Name: \".$myrow[\"username\"];
echo \"<br>Email address: \".$myrow[\"password\"];
echo \"<br>Salary: \".$myrow[\"admin\"];
</HTML>
?>
It gives me this eror:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in d:\program files\appserv\www\test\displayusers2.php on line 7
It's basically the same code, what's up?
Abhishek Reddy posted this at 06:16 — 7th September 2004.
He has: 3,348 posts
Joined: Jul 2001
1) Put quotes around literals. $_GET[u] becomes $_GET['u'], likewise with id, even though it works.
2) Did you remember to change the name/id in the form or the query in the URL?
Dragon of Ice posted this at 12:25 — 7th September 2004.
He has: 578 posts
Joined: Jun 2004
didn't do it. it has something to do with putting a letter. It seems I can only put numbers in it, but that's not right! Ugh.
Abhishek Reddy posted this at 13:28 — 7th September 2004.
He has: 3,348 posts
Joined: Jul 2001
Letters, numbers? You lost me. How about linking to the whole code -- including form and all?
Dragon of Ice posted this at 20:48 — 8th September 2004.
He has: 578 posts
Joined: Jun 2004
You have both the php displays.
There is no form. Since I'm using get, and I'm just trying to learn database, and this is an experiment.
Abhishek Reddy posted this at 22:21 — 8th September 2004.
He has: 3,348 posts
Joined: Jul 2001
How are you accessing the page? What url do you go to? Have you changed ?id=X to ?u=X ?
Dragon of Ice posted this at 01:56 — 19th September 2004.
He has: 578 posts
Joined: Jun 2004
Ok, I'm starting fresh and it seems I'm having the same problem. I am not using a form at all, just typing the values into the adress bar. I'm using the address:
and getting this error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in d:\program files\appserv\www\cms tutorial\displaycomments.php on line 10
Here is the code:
<?php
<HTML>
<BODY>
$name = $_GET['name'];
$pass = $_GET['pass'];
$db = mysql_connect(\"localhost\", \"root\", \"\");
mysql_select_db(\"news\",$db);
$result = mysql_query(\"SELECT * FROM users WHERE name=$name\",$db);
$myrow = mysql_fetch_array($result);
{
echo $myrow[\"name\"];
echo \"<br>\";
echo $myrow[\"password\"];
}
</BODY>
</HTML>
?>
can anyone figure out whats up?
kb posted this at 02:31 — 19th September 2004.
He has: 1,380 posts
Joined: Feb 2002
change
<?php
echo $myrow[\"name\"];
?>
<?php
echo $myrow['name'];
?>
Also, what are the curly brackets for? Before the first echo and after the last?
Dragon of Ice posted this at 02:37 — 19th September 2004.
He has: 578 posts
Joined: Jun 2004
Ugh. Still no luck.
The brackets are there from when that was a while loop.
Sorry to be so bothersome, but I just don't know enough about PHP and mysql to trouble shoot any further than I have (which is a very small bit) so I came to you guys.
Abhishek Reddy posted this at 02:49 — 19th September 2004.
He has: 3,348 posts
Joined: Jul 2001
Tried debugging? die()? mysql_error()? Test the SQL in phpMyAdmin?
Check if all the variables and values are being passed by printing them at various stages.
Use die() with mysql_query() and mysql_connect(), and also use mysql_error() for mysql calls so you know what's failing.
Try the SQL from phpMyAdmin or even command line if you know how. Maybe it's a problem with the tables or database.
Also, try removing the resource identifier ($db) from mysql_query. Just do mysql_query("SELECT ... ");
You can modify example code in the PHP manual to suit your data too. If that works, it might reveal problems with your code.
Btw, remember to mysql_free_result() and mysql_close().
Dragon of Ice posted this at 03:53 — 19th September 2004.
He has: 578 posts
Joined: Jun 2004
OK, I've figured out that It has something to do with
"WHERE name=$name"
When I take out that bit it displays, but I can't really get rid of, considering I need to be able to view each user individually.
Busy posted this at 04:33 — 19th September 2004.
He has: 6,151 posts
Joined: May 2001
try something like this
<?php
// db connect etc
$query = "SELECT * FROM users WHERE name=$name";
// echo $query;
$result = mysql_query($query);
if(mysql_num_rows($result)>0)
{
$the_name = mysql_result($result,0,"name");
$the_email = mysql_result($result,0,"email");
}else{
echo " sorry, no one home ";
}
echo $the_name;
echo "<br>";
echo $the_email;
?>
If there is no database entries it will display 'sorry no one home'.
If you get mysql errors, uncomment the echo $query; to find out if the name is calling a value. if it shows SELECT * FROM users WHERE name= then you know the $name variable is being lost somewhere
Also if you use anythig like mysql_error() make sure you delete it before uploading, use it for testing only. displaying a mysql error directly to your page for the world to see is like leaving a key under your front door mat
Dragon of Ice posted this at 15:17 — 19th September 2004.
He has: 578 posts
Joined: Jun 2004
MMM... query looks right, it's echoing ...name=jim
I'm still getting an error though. I wonder if this all has something to do with mysql? I think i'm gonna try doing this on another server. Here's the error:
Dragon of Ice posted this at 16:29 — 19th September 2004.
He has: 578 posts
Joined: Jun 2004
Ergh... Tried a different host, and stillt he same problem.
kb posted this at 18:00 — 19th September 2004.
He has: 1,380 posts
Joined: Feb 2002
Wait...I get that error sometimes when I have nothing in the database. Do you have anything filled in in the dB?
Dragon of Ice posted this at 18:57 — 19th September 2004.
He has: 578 posts
Joined: Jun 2004
yes, I have one row filled in with the name being jim and the password being pass.
Busy posted this at 21:35 — 19th September 2004.
He has: 6,151 posts
Joined: May 2001
what happens when you hardcode the name: name='jim' ?
Dragon of Ice posted this at 03:03 — 2nd October 2004.
He has: 578 posts
Joined: Jun 2004
Same thing.
Busy posted this at 05:42 — 2nd October 2004.
He has: 6,151 posts
Joined: May 2001
try
WHERE name='".$name."'";
if you can't work out the quotes, it's single, double, dot ..... dot, double, single, double
Dragon of Ice posted this at 21:01 — 2nd October 2004.
He has: 578 posts
Joined: Jun 2004
I'm to the point of about giving up and reading more articles. Then maybe I'll understand this stuff better.
Busy posted this at 22:48 — 2nd October 2004.
He has: 6,151 posts
Joined: May 2001
One more go, can you copy and paste your code again please, mainly the bits above the query.
If you hard coded the value there is no way it can't be passed, unless it's in a if statement, wrong case, your using htmlspecialcharacters and the variable starts and end in pointy brackets.
Dragon of Ice posted this at 22:51 — 2nd October 2004.
He has: 578 posts
Joined: Jun 2004
<?php
<HTML>
<BODY>
$name = $_GET['name'];
$pass = $_GET['pass'];
$db = mysql_connect(\"localhost\", \"root\", \"\");
$query = \"SELECT * FROM users WHERE name='\".$name.\"'\";
echo $query;
$result = mysql_query($query);
if(mysql_num_rows($result)>0)
{
$the_name = mysql_result($result,0,\"name\");
$the_pass = mysql_result($result,0,\"pass\");
}else{
echo \" sorry, no one home \";
}
echo $the_name;
echo \"<br>\";
echo $the_pass;
</BODY>
</HTML>
?>
Busy posted this at 22:59 — 2nd October 2004.
He has: 6,151 posts
Joined: May 2001
change $name = $_GET['name']; $pass = $_GET['pass']; to $name = "jim"; $pass = "pass";
then show what it displays on the page
Later, incase I forget, always do addslashes($inputted_value) when querying databases or a script kiddy can do stuff.
Dragon of Ice posted this at 23:04 — 2nd October 2004.
He has: 578 posts
Joined: Jun 2004
Also, what does addslashes do?
Abhishek Reddy posted this at 23:13 — 2nd October 2004.
He has: 3,348 posts
Joined: Jul 2001
Use or die(mysql_error()) at the end of your mysql_ calls so you know which one is going wrong -- or have you tried this already? The error output shown presently isn't very helpful.
addslashes escapes quote characters by prepending a slash before them. If the user submits "bob" (with quotes included), it becomes \"bob\". The point is to stop the user from escaping your SQL string and inserting their own potentially malicious code.
Busy posted this at 23:15 — 2nd October 2004.
He has: 6,151 posts
Joined: May 2001
If you have jim in the database it should work.
change $query = "SELECT * FROM users WHERE name='".$name."'"; to $query = "SELECT * FROM users WHERE name='".%$name%."'";
This script is wrong in several ways, login should use _POST and never _GET, the incoming information should be trimmed (removed leading and trailing white space) and slashes added.
using your method above, if I was to put yoururl.pge?name=admin';# I would be logged in as admin, Ideally you should be checking for name AND password matches, but if you had $query = "SELECT * FROM users WHERE name='$name' and pass='$pass'"; what I entered would still break your code and be logged in as admin as what I put admin';# would make the query stop after the name (with the semicolon) and the # would make anything after that a comment. adding addslashes, slashes any quotes to help avoid this, so this "I don't slash"; would become "I don\'t slash"
Abhishek Reddy posted this at 23:20 — 2nd October 2004.
He has: 3,348 posts
Joined: Jul 2001
Wait a minute. Why aren't you using mysql_select_db()? How is the script to know which database to find the table 'users' in?
Busy posted this at 23:25 — 2nd October 2004.
He has: 6,151 posts
Joined: May 2001
Abhishek, I'm guessing he has it above the where the form is
The result on the page is only a warning, not an error, so you can even supress this if you which (add @ infront of the effected line). At a guess I'd say you have error reporting to all - which is good but warnings and errors are different things.
In this case we have no output so there is a fault
Abhishek Reddy posted this at 03:04 — 3rd October 2004.
He has: 3,348 posts
Joined: Jul 2001
But that would be before mysql_connect(), which isn't right...
I see he has it in previous versions of the code in this thread. Not sure why it's missing.
Busy posted this at 23:38 — 2nd October 2004.
He has: 6,151 posts
Joined: May 2001
Once you get it sorted you can make some changes and make your form/ page, somethign like:
" />
....
then your php section, something like
if($_POST[login] == 'yes')
{
if($_POST[attempts] >= '4')
{
// send them to another page (error page) offering some reasons why they can't login with link back to login page - possible brute force attack.
exit;
}
$name = addslashes($_POST[name]);
$pass = addslashes($_POST[pass]);
// if not already connected to db, do it now
// do the query as normal
// if login fails, $attempts = $attempts + 1;
// include form again or if login successful send to another page or display the goodies
}
Busy posted this at 03:15 — 3rd October 2004.
He has: 6,151 posts
Joined: May 2001
true, good spoting bat man,
Dragon of Ice, add this to the top of your page
<?
$db = @mysql_connect("localhost", "root", "");
if (!db)
{
echo ("Unable to connect to database server");
exit();
}
if (!@mysql_select_db("???????"))
{
echo ("Unable to connect to database");
exit();
}
?>
replace the ???????? with your database table name
and remove the $db = mysql_connect("localhost", "root", ""); you have there now
Dragon of Ice posted this at 14:12 — 3rd October 2004.
He has: 578 posts
Joined: Jun 2004
I understand all the security things I'm doing wrong, but right now I'm just trying to get the database to work. It works, but now the only thing is that it's outputting the query.
Here's the revised code:
<?php
<HTML>
<BODY>
$name = jim;
$db = mysql_connect(\"localhost\", \"root\", \"\");
mysql_select_db(\"news\",$db);
$query = \"SELECT * FROM users WHERE name='\".$name.\"'\";
echo $query;
$result = mysql_query($query);
if(mysql_num_rows($result)>0)
{
$the_name = mysql_result($result,0,\"name\");
$the_pass = mysql_result($result,0,\"password\");
}else{
echo \" sorry, no one home \";
}
echo \"<br>\";
echo $the_name;
echo \"<br>\";
echo $the_pass;
</BODY>
</HTML>
?>
Dragon of Ice posted this at 14:14 — 3rd October 2004.
He has: 578 posts
Joined: Jun 2004
whoops just found it
It was during debugging. gotta take out "echo $query"
Also, I got it to work with GET, so that means it should work with POST. Now on to the security, but first, I have to finish some layout things for the page (namely the entire site).
Dragon of Ice posted this at 00:47 — 5th October 2004.
He has: 578 posts
Joined: Jun 2004
WOOT!
From all the help and info I got here, the database is in operation! Thanks alot guys!
The site's at:
http://www.zapgraphics.no-ip.com/zap/php/
Abhishek Reddy posted this at 05:17 — 5th October 2004.
He has: 3,348 posts
Joined: Jul 2001
I don't know what was wrong but I'm glad you fixed it. Well done.
Dragon of Ice posted this at 20:32 — 5th October 2004.
He has: 578 posts
Joined: Jun 2004
Abhi, were you able to access the site? Because I can't off any other computer than the host it self.
Abhishek Reddy posted this at 23:58 — 5th October 2004.
He has: 3,348 posts
Joined: Jul 2001
Ah, no, I wasn't able to. Nor can I now.
Busy posted this at 04:32 — 6th October 2004.
He has: 6,151 posts
Joined: May 2001
I get time out error.
Dragon of Ice, can you get to a non server side page (only has html) ?
If you can the blockage could be caused by a run away loop (while 1=1 .... )
Dragon of Ice posted this at 15:48 — 6th October 2004.
He has: 578 posts
Joined: Jun 2004
no. I can't access anything.
Abhishek Reddy posted this at 03:02 — 7th October 2004.
He has: 3,348 posts
Joined: Jul 2001
Probably a network blockage if you can access it locally. Go over network configs, routers, firewalls, ISP blocks, etc.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.