Screwey Database

He has: 578 posts

Joined: Jun 2004

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's picture

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?

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's picture

He has: 3,348 posts

Joined: Jul 2001

Letters, numbers? You lost me. How about linking to the whole code -- including form and all? Smiling

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's picture

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 ?

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:

Quote: http://localhost/CMS%20Tutorial/displaycomments.php?name=jim

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?

He has: 1,380 posts

Joined: Feb 2002

change

<?php
echo $myrow[\"name\"];
?>
to
<?php
echo $myrow['name'];
?>

Also, what are the curly brackets for? Before the first echo and after the last?

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's picture

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(). Wink

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's picture

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

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:

Quote: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in d:\program files\appserv\www\cms tutorial\displaycomments.php on line 11

He has: 578 posts

Joined: Jun 2004

Ergh... Tried a different host, and stillt he same problem.

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?

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's picture

He has: 6,151 posts

Joined: May 2001

what happens when you hardcode the name: name='jim' ?

He has: 578 posts

Joined: Jun 2004

Same thing.

Busy's picture

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

He has: 578 posts

Joined: Jun 2004

Quote:
SELECT * FROM users WHERE name=''
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in d:\program files\appserv\www\cms tutorial\displaycomments.php on line 11
sorry, no one home

I'm to the point of about giving up and reading more articles. Then maybe I'll understand this stuff better.

Busy's picture

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.

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's picture

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.

He has: 578 posts

Joined: Jun 2004

Quote:
SELECT * FROM users WHERE name='jim'
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in d:\program files\appserv\www\cms tutorial\displaycomments.php on line 11
sorry, no one home

Also, what does addslashes do?

Abhishek Reddy's picture

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.

Smiling

Busy's picture

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's picture

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's picture

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's picture

He has: 3,348 posts

Joined: Jul 2001

Busy wrote: Abhishek, I'm guessing he has it above the where the form is

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's picture

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's picture

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

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

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).

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's picture

He has: 3,348 posts

Joined: Jul 2001

Dragon of Ice wrote: 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/

Laughing out loud

I don't know what was wrong but I'm glad you fixed it. Well done. Smiling

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's picture

He has: 3,348 posts

Joined: Jul 2001

Ah, no, I wasn't able to. Nor can I now.

Busy's picture

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 .... )

He has: 578 posts

Joined: Jun 2004

no. I can't access anything.

Abhishek Reddy's picture

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.