Varchar Query Issues

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Bipper
Forum Newbie
Posts: 4
Joined: Wed Jun 11, 2003 10:02 am
Location: Minnesota

Varchar Query Issues

Post by Bipper »

I am running a query based on user input that will fill out a field called username or id. I throw togeter the query and it works fine for id# but when ever i use the quewry on anything that is varchar type it poops out and gives me this :
:evil:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/bipperv/public_html/MyBeam/php-bin/editUser.php on line 29

Any help would be cool :) 8)

Later

Bipper
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Sounds like there's something wrong with the SQL - could we see your code please.

Mac
User avatar
cactus
Forum Regular
Posts: 343
Joined: Tue Jun 10, 2003 4:16 am
Location: UK

Post by cactus »

Did you have a connection to the database ? Did you lose the connection ??

Regards,
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

could be a couple of things, are your values in the queries enclosed in quotes?

post the SQL so we can see it.
PastAustin
Forum Newbie
Posts: 15
Joined: Wed Jun 11, 2003 11:38 am
Location: Littleton, Colorado
Contact:

Post by PastAustin »

Wayne wrote:could be a couple of things, are your values in the queries enclosed in quotes?

post the SQL so we can see it.
That is exactly what it sounds like to me. You other options are a lost connection, or even a blank database entry or querying the wrong table.
Bipper
Forum Newbie
Posts: 4
Joined: Wed Jun 11, 2003 10:02 am
Location: Minnesota

Update

Post by Bipper »

Well i know the sql is good and the connection and everything is good. I will put code up although due to a non disclosure agreement i can't post much.

Var names have all been changed the structure remains intact -

if ($submit) {

if ($id || $username)
{

if ($username)
{
$sql = "SELECT * FROM user WHERE beamRank=$username";
}

if ($id)
{
$sql = "SELECT * FROM user WHERE id=$id";
}



$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);


?>
<!--Make FORM to print out values for editing-->

<form method="post" action="<?php echo $PHP_SELF ?>">
<table>
<tr>
<td bgcolor="#FFFFCC">

ID#:

</td>
<td bgcolor="#CCFFFF">

<input type="Text" name="idE" value="<?php echo $myrow[id] ?>">
</td>
</tr>
<tr>
<td bgcolor="#CCFFFF">

Username:

</td>
<td bgcolor="#CCFFFF">
<input type="Text" name="usernameE" value="<?php echo $myrow[Uname] ?>">
</td>
</tr>
<tr>
<table>

What i am doing is pulling data based on either a id or username information put in on one form. The data is then filled in on a from that appears and allows for the user to edit it. Then it will eventually be able to update the database.

That is a brief dumbed down version. My table IS populated with correct data and the script works perfect if the integer id field is filled out. However the second you enter the username field it tirds out on me. I know the uname field is there and filled in correctly.

No solutions offered so far have helped - but thanks for the help thus far all :)

Bipper
Bipper
Forum Newbie
Posts: 4
Joined: Wed Jun 11, 2003 10:02 am
Location: Minnesota

Post by Bipper »

LOVE THE AVATAR WAYNE LOL :P :D
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

try :

Code: Select all

$sql = "SELECT * FROM user WHERE beamRank="$username"";
Im assuming that this is the varchar field that you are referring to not working! you can evaluate ints in an sql without the quotes but varchar, char etc require them.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

It is most likely the lack of quotes that is causing the problem, to debug problems like this in the future though you should add some error handling to those database functions:

Code: Select all

$result = mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>');
You can also negate the need for escaping double quotes in your SQL by using single quotes around values (it's better SQL as well):

Code: Select all

$sql = "SELECT * FROM user WHERE beamRank='$username'";
Mac
User avatar
cactus
Forum Regular
Posts: 343
Joined: Tue Jun 10, 2003 4:16 am
Location: UK

Post by cactus »

(my 2 pence/cents worth)
On the subject of quotes I preffer to concatinate strings/queries:

Code: Select all

$sql = "SELECT * FROM user WHERE beamRank='".$username."'";
Helps with syntax highlighing too.

:)
User avatar
cactus
Forum Regular
Posts: 343
Joined: Tue Jun 10, 2003 4:16 am
Location: UK

Post by cactus »

If I used the right BB tags!! :

Code: Select all

$sql = "SELECT * FROM user WHERE beamRank='".$username."'";
;)
Bipper
Forum Newbie
Posts: 4
Joined: Wed Jun 11, 2003 10:02 am
Location: Minnesota

Doh!

Post by Bipper »

Yesh you guys were right just the simple lack of quotes arround the $username variable... How sad lol. Thank you all very much for the help, and I am sorry iI only gave you that little bit to work with :) I have been in a frantic rush trying to get this project wrapped up :)


Thanks again :D

Bipper
Post Reply