/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] sql/php error

I am making a profile system and I want to make it where when someone types [url]www.mysite.com/profile.php?id=2[/url] it runs a statement to select * from users WHERE id = ‘2’

so I set up the stuff and I came out with this code

[code=php]
<?
session_start();
if(isset($_GET[‘id’])){

$sqlconn = “localhost”;
$sqluser = “username”;
$sqlpass = “password”;
$sqldb = “database”;
$id = $_GET[‘id’];

$conn = mysql_connect($sqlconn, $sqluser, $sqlpass);
if(!$conn) {
die(“Could not connect to: “.$sqlconn.”:.:”.mysql_error());
}

mysql_select_db($sqldb, $conn)
or die(“Could not select:.:”.$sqldb.” – “.mysql_error());

$querytxt = “SELECT * FROM users WHERE id=’$id'”;
$selectquery = mysql_query($querytxt);

$row = mysql_num_rows($selectquery);

echo $querytxt.”<p>”;
echo $selectquery.”<p>”;
echo “username: “.$row[‘username’];

}

?>[/code]

which [b]SHOULD[/b] echo out the query, the result of the query , and the username of the user with whatever ID i set in the GET[‘id’] in the URL. But It is outputing this

[code]SELECT * FROM users WHERE id=’2′

Resource id #3

username: [/code]

Just that, and of course, I set GET[‘id’] to 2 in the url there, and there is a user with id number 2, but it just returns that resource Id # 3 and I am not sure why. All help is appreciated, Thanks.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 15.2006 — If id is a numeric field, then don't quote the value in the query:
[code=php]
$querytxt = "SELECT * FROM users WHERE id=$id";
[/code]
Copy linkTweet thisAlerts:
@Heavy_MetalauthorJul 15.2006 — I took the quotes off of the $id and is still outputting the same result
Copy linkTweet thisAlerts:
@NogDogJul 15.2006 — You need to do some sort of mysql_fetch to get the values from the query result. (The following also includes a free defensive coding example. ? )
[code=php]
$selectquery = mysql_query($querytxt);
$numRows = mysql_num_rows($selectquery);
if($numRows == 1)
{
$row = mysql_fetch_assoc($selectquery);
$user = $row['username'];
}
elseif($numRows === 0) // use "===" to differentiate from FALSE
{
user_error("No match found for user ID", E_USER_ERROR);
}
elseif($numRows > 1)
{
user_error("Data error, multiple users found with same ID", E_USER_ERROR);
}
else
{
user_error("Query error: " . mysql_error() . "<br>$querytxt", E_USER_ERROR);
}
echo "User Name: $user";
[/code]
Copy linkTweet thisAlerts:
@Heavy_MetalauthorJul 15.2006 — OMG, I just realized why my code wasn't working, I was using mysql_num_rows where I should have been using mysql_fetch_assoc, I caught it when I was going through your script and I remembered having typed mysql_num_rows, anyways, my $row['username'] is working fine on its own now, and I am going to add some defense code like what you specified(in fact, ill probobly just add yours) but Im going to finish up the actual script for now. Thanks.
Copy linkTweet thisAlerts:
@Heavy_MetalauthorJul 15.2006 — Sorry for the double post, but I had to get your attention. I just discovered, that If you enter say ?id=5 (there is not a fifth user yet), it just displays my format for displaying it and I would like for it to display "User Does Not Exist" or something like that. How could I do this?
Copy linkTweet thisAlerts:
@Heavy_MetalauthorJul 16.2006 — i cannot quad. post I cant bring myself to do it, so if noone replys now, I shall simply make a new topic, i didn't want to post in this one anyways, but I didn't want to make that many new topics if could be avoided, anyways: bump
×

Success!

Help @Heavy_Metal spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...