Forum Moderators: coopster

Message Too Old, No Replies

MySQL Failing

         

SeanF

10:49 pm on Feb 16, 2021 (gmt 0)

5+ Year Member Top Contributors Of The Month



OK... I'm Baffled...

I have the following Script which produced a null result:

echo "User: $uname <br/>";

$get_users = "SELECT *
FROM users
WHERE user_name = '$uname'";
echo "Get User: $get_users<br/>";
$get_users_res = mysql_query($get_users, $conn) or die(mysql_error());
echo "GTR $get_users_res <br/>";
$newArray = mysql_fetch_array($get_users_res);
echo "Array: ";
print_r($newArray);

$user_id = $newArray['user_id'];
echo "User ID: $user_id <br/>";

(I know the mysql_ set of extensions is deprecated but it's old code and I don't have time to rewrite it all. MySQL queries work fine on the other pages... for now)

This returns the following:
User: msperson1
Get User: SELECT * FROM users WHERE user_name = 'msperson1'
GTR Resource id #5
Array: User ID:
that user name was not found; please go back and try again

For some reason the array is not being returned... If I copy the MySQL command and paste it into PHPMySQL, it returns the expected result.

What am I missing? It's driving me crazy.

(All of the echos are just trying to figure out what's failing)

Thanks

lammert

11:50 am on Feb 17, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You are still using the deprecated mysql_ functions. Try their mysqli_ cousins instead and upgrade PHP and your MySQL database server in the process.

If that doesn't give the desired result, install a database management tool like phpMyAdmin to jump in the database directly and run test queries to see what causes the problem.

w3dk

12:05 pm on Feb 17, 2021 (gmt 0)

10+ Year Member Top Contributors Of The Month



For some reason the array is not being returned...


In that case I would assume it's returning bool(false) (using var_dump() instead of print_r() would be more informative here). In which case the query would seem to be returning "no rows".

I would certainly try just removing the WHERE clause. Also write the statement on a single line to make sure there's no erroneous characters in there (it's a very short statement anyway so doesn't really warrant multiple lines).

Are you connected as the correct user, to the correct DB?!

SeanF

10:20 pm on Feb 17, 2021 (gmt 0)

5+ Year Member Top Contributors Of The Month



Thanks for the reply...

Thant's why I have the query statement echoed to the screen so I can see how the query is represented. If I copy the remainder of the line that starts with "Get User: " and paste it directly into PHPMyAdmin returns the desired result... one company with a unique company ID.

I have double checked the database login and database select commands and they seem to be correct. Besides, since each of those lines ends with "... or die(mysql_error())", if there was a problem I assume the error message would be sent to the screen.

I will continue to investigate.

SeanF

10:45 pm on Feb 17, 2021 (gmt 0)

5+ Year Member Top Contributors Of The Month



Interesting...

Your suggestion to remove the "WHERE" clause (and adding a "while($newArray = ...)" loop) returns all of the user IDs from the table.

So, it must be the formulation of the parameter $uname...

That value comes from the following Line:
$uname = trim(strip_tags($_POST['uname']));

If I remove the trim(), that seems to fix the problem.

What am I missing in the trim() function? The intention is simply to remove any accidental spaces that might have been entered into the form field. It's probably unnecessary anyway and I will remove them but why would that cause the query to fail?

Thanks again

londrum

10:53 pm on Feb 17, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What’s the
echo "GTR $get_users_res <br/>";

line for, I don’t think that’s ever going to show anything when it’s placed there.
Probably won’t help if you remove it, but you never know

w3dk

12:03 am on Feb 18, 2021 (gmt 0)

10+ Year Member Top Contributors Of The Month



If I remove the trim(), that seems to fix the problem.


That would seem to imply there is some whitespace surrounding the user_name in the database?! What kind of form control is used for $_POST['uname']?

When you traversed all the user IDs (without the WHERE clause), did you also examine the user_name field? Are these values as expected?

Character encoding issue?!

universenet

1:08 am on Feb 18, 2021 (gmt 0)

Top Contributors Of The Month



SeanF
Maybe your page should be saved utf-8 so is just fonts problem maybe together with deprecated command