Need help with PHP and Mysql Rows

#1

Hello! I am a newbie in php and mysql.

I have 2 rows . 1 is called ITEM_ID and the other PLR_ID
I want to count how much rows are with ITEM_ID = 2 and have the PLR_ID = “$userid”!

I have been trying to do it but i am not going somewhere. can you help me out?

$result =  mysql_query("SELECT * , (SELECT ITEM_ID FROM plr_items WHERE ITEM_ID = '2') AS PLR_ID FROM plr_items WHERE PLR_ID = ". $user_id . " LIMIT 1");

					if(mysql_num_rows($result))
					{
						$user_itemheal = mysql_fetch_array($result);
					}
				
				}				
			echo '</section>
		    <tr>
		    <td style="width:200px">HEAL:</td>
		    <td style="background-color: #ddffdd; width:300px;">'. $user_itemheal['ITEM_ID'] . '</td>
		    </tr>

Here is the code i am trying

#2

First of all, if you are just starting out you need to stop right now.
I need to first point out you are using an extension that does not exist in PHP anymore.
mysql is long gone, you have to use mysqli or PDO to communicate with your database.
If you have been using mysql functions, that means you were also using an obsolete and unsupported verion of PHP because it simply won’t work in current versions.

There is no point starting out learning obsolete code.
You need to use a current PHP version and that will mean using mysqli of PDO too.

1 Like
#3

I am using PHP 5.4 i think. Well i understand but i got this work and i am not able to do a lot of changes!
I want just to start modifying this and achieve something!
Can you please help me out with this problem ?

#4

Well I’m not exactly clear what you are trying to do, the query does not make sense with the question.
You are asking about counting, but using a query limited to one result.

I guess you mean columns not rows here?

#5

As you can see in the photo!
I want to count how much are with PLR_ID = $userid and ITEM_ID = 2
Example in the photoo
PLR_ID = 2 and ITEM_ID = 13
I want to show that are 10 that have PLR_ID = 2 and ITEM_ID =13
I hope i explained good! I hope

#6
SELECT COUNT(*) as match FROM plr_items WHERE ITEM_ID = :item_id AND PLR_ID = :user_id
#7

Warning : mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\pages\user_profile.php on line 481

Notice : Undefined variable: user_itemheal in C:\xampp\htdocs\pages\user_profile.php on line 490

I AM GETTING THIS ERROR

#8

Set your values instead of :item_id and :user_id. They both are placeholders by PDO preparing and actually PDO with preparing strongly recommended for work with relational databases.

2 Likes
#9
				$result = mysql_query("SELECT COUNT(*) as match FROM plr_items WHERE ITEM_ID = :13 AND PLR_ID = ". $user_id . " ");
		
					if(mysql_num_rows($result))
					{
						$user_itemheal = mysql_fetch_array($result);
					}
				
				}				
			echo '</section>
		    <tr>
		    <td style="width:200px">HEAL:</td>
		    <td style="background-color: #ddffdd; width:300px;">'. $user_itemheal['ITEM_ID'] . '</td>

I dont know why i am still getting that message

I am getting also 2nd error here

    <td style="background-color: #ddffdd; width:300px;">'. $user_itemheal['ITEM_ID'] . '</td>
#10
  1. Just 13, not :13.

  2. Your value in $user_itemheal[‘match’]

#11

The same error!

#12

Use mysql_fetch_assoc instead of mysql_fetch_array

#13

Warning : mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\pages\user_profile.php on line 480

Notice : Undefined variable: user_itemheal in C:\xampp\htdocs\pages\user_profile.php on line 489

The same still!

#14

mysql_num_rows() expects parameter 1 to be resource, boolean given

means your query is not correct. Or possibly you have no connection with database. Show your query again.

#15

I am newbie. Dont understand! Explain me pleasee how to show my query again

#16

The only error i am getting! I cant fix ittt!
Help me out pleasee

#17

Ok. This is the first step you should actually get working first. I highly highly highly suggest you not try to code in PHP first of all since the main problem is not being able to get the SQL started. What I strongly suggest is that you either use MySQL Workbench or some alternative. Try getting the SQL to actually work properly before you start implementing it into a PHP code. This is the most effective route because you will know 100% that your SQL statement actually works properly.

The next thing after that is to properly write PHP code. The mysql_* extensions have long since been dead and there is no legitimate reason to be using these. No matter what version of PHP you are using, there is no legitimate reason to be using them anymore. It’s not some kind of “scare tactic” to make you switch. There really is no reason to be using mysql_*. You should be using mysqli_* or PDO as both @SamA74 and @igor_g has been alluding to the whole time. Don’t use regular queries (->query or _query), use prepared statements (->prepare) because you are dealing with user input.

1 Like
#18

Have a read of this article which covers migrating over to using PDO to interact with the database.

2 Likes
#19

The second error is a direct result of the first error. Because your query did not execute, when you try to use the results of that query, you won’t be able to because the results do not exist. Hence the variable is undefined.

Step 1: run your query from phpmyadmin or whatever you used to create the database and put that information into it. Once you have a working query, then you can look at how to run the query from PHP and use the results.

There is a reluctance on here to help anyone learn how to use an outdated set of functions. The mysql_ library was removed from the language for PHP 7. I understand you just want to get something working so you can build on it, but it’s really not the correct way to go. You can use PDO (which would be my choice over PDO) without having the complexity of prepared statements, and then learn about those later on once you’ve seen something working. But it really is a bit of a waste of your time to learn how the old functions work, unless you’re planning to spend your time converting outdated systems to use the new versions.

You don’t need to change PHP version to use PDO, as it was included from PHP version 5.1. It would be better to be on a newer version, but not mandatory.

1 Like
#20

If it isn’t prepared statements but the OOP syntax that’s holding you back you should try mysqli (mysql improved) that can be written with both OOP and procedural syntax. It doesn’t have my preferred named placeholders but does have prepared statements.

In any case, mysql (the extension, not MySQL the database) is now obsolete and any version of PHP that supported it - this is important - has reached End Of Life and no longer gets any fixes at all including security fixes.

2 Likes