Affected Rows from SELECT is -1 (wrong)

Why is that my code works perfectly well in my local system but doesn’t work with 000webhost?
Selecting from database gives me $AffectedRows = -1
But I have 1 row in the database.

$link = mysql_connect($host, $username, $password);
if (!$link){die('Not connected : ' . mysql_error());}
$db_selected = mysql_select_db($database, $link);
if (!$db_selected){die ('Can\'t use ' .$database .':'. mysql_error());}
$query = "SELECT * FROM users WHERE userid = '$UserID' ";
$Results = mysql_query($query, $link);
$AffectedRows = mysql_affected_rows($link);
if($AffectedRows < 1)

I’ve tried to select all rows but I get the same results: -1
$query = "SELECT ALL FROM users WHERE userid = ‘$UserID’ ";

1 Like

Try Using

mysql_num_rows

I’ve tried that. Same results.
The problem is not with mysql_affected_rows($link) or with MySQL_num_rows but the fact that my query results in no rows being selected, i.e., why is the row which exists in my database not being selected with 000webhost but in my local system, it works fine (with the same code).

After my query, I put in a print of the results of the SELECT and the result is NUL

$query = “SELECT * FROM users WHERE userid = ‘$UserID’ “;
$Results = mysql_query($query, $link);
print(”$Results
”); <— this print shows there was nothing selected.

You are printing a query? :confused:
Also, your code is open to mysql injections
I recommend you use mysqli, alongside with bind params.

I’m not familiar with mysqli and furthermore I can’t start changing all of my code.
As I said, my code works in my local system (using MySQL) why shouldn’t it work with 000webhost? Does it have a different MySQL?

Tried downgrading PHP version? :slight_smile:
cPanel>Settings>General

I’ve already downgraded to 5.6 because of another problem which got resolved by this downgrade.
Do you have a suggested level?

By the way, many thanks for your help.

1 Like

Try replacing
$Results = mysql_query($query, $link);
With
$Results = mysql_query($query);

Same thing; no results.

Then, try

$query = "SELECT * FROM `users` WHERE `userid` = ‘".$UserID."’ ";

Also, please watch out for case sensitivity :slight_smile:

I will have to wait till tomorrow or later. 000webhost has knocked me off.
I’m on there on a free basis. Difficult to upgrade when you can’t get your site to work. If it doesn’t work on FREE, can I trust that it will work if I pay?
I have been checking the cases but all seems OK. And again I come back to my question: If it works on my local system, why doesn’t it work on 000webhost. My code is the same - case is the same, etc. The only difference is my website name and password and my password on my database. My website name and password are not a problem, and my database password I don’t think is a problem or it would tell me it can’t be accessed. If your car works on Highway 5, it should still work on Highway 6!

Many thanks again for you help and have a good rest of the day!

1 Like

Umm, well, be aware, userid may not be treated as userId

OK, I’m back on 000webhost.
I’ve tried you last suggestion, i.e., try
$query = “SELECT * FROM users WHERE userid = ‘”.$UserID."’ ";
and I get the same bad results.
As for your warning “userid may not be treated as userid”, I don’t see why not. I’ve check and double check and tripled checked my data base and the filed name is definitely “userid” in table “users”. Furthermore, “userid” works perfectly well in my local system so why would it not in ooowebhost? Additionally, I don’t believe there is anything wrong with “userid”. It’s the reading of the database that doesn’t work. I tried to do a SELECT ALL from this table (no dependency on “userid” - just read all rows - and I get no rows read: mysql_affected_rows($link) = -1.
So, conclusion is and has to be, reading of the database is not working. And, there can’t be anything wrong with my Database name, or my password or I would get a no access permitted message.

1 Like

Link to the script please? :slight_smile:

Hi,
I just found the problem. I changed:
$Results = mysql_query($query, $link);
to
$Results = mysql_query($query, $link) or die ('What Error = '. mysql_error());
And it told me I was denied access to my database. So then I knew there was something wrong with my database ID or my database password. It was my database id. I was using the database name I assigned but I needed to add the idxxxxxxx_ assigned by 000webhost.
So having " or die ('What Error = '. mysql_error());" is best.

Thank you so much for your help in debugging this problem.
Have a good day!

1 Like