+ 3

Whats wrong In the following php code

https://code.sololearn.com/wpVmglOSKT0B/?ref=app I'm getting error 'No Results Found' from the code even though I've matching entries in my database! how to fix it?

30th Jan 2018, 4:09 PM
Vithul T Nair
Vithul T Nair - avatar
4 ответов
+ 2
You need to preform some debugging; I'm assuming your "id" and "name" on lines 7 and 8 are correct? (can't hurt to verify) Because you're querying based on specific results of those 2 variables. Next, verify that "mysqli_query" (line 11) is working by using a very basic, very general query. For example, "SELECT * FROM files" and then verify that "$row_count" is the correct amount. (I don't know PHP syntax but what ever it is to simply display variable values on the console; C++ example: cout<<variable_name; but judging by your code I'm assuming it's "echo" similar to Windows CMD batch file) If everything is correct so far, then your issue lies within "mysqli_num_rows" (line 12) because your number of rows are not being calculated correctly (or at all) because according to your code the only time you should see the error "No Results Found" is when the variable "$no_of_rows" is equal to 0 Now, if by changing the Query (specifically one without using variables or SQL strings with the ' character) fixes the problem, then you know it's an issue with your Query. By looking at it, I'm assuming you're not handling the string properly because of all the quotes. You have 2 variables in a string, so I'm assuming you're sending the literal string "$admin_search_id" instead of the value of that variable. (Again, I don't know PHP syntax, that's a later course for me; but you might need to separate the string from the variables. Some languages use the + sign, others use the & sign.) Example: "SELECT * FROM files WHERE id = '" + $admin_search_id + "' OR title = '" + $admin_search_name + "'" ) Notice the single quotes and double quotes in there? because your PHP statement needs the double quotes to make it a string and SQL needs the single quotes. Hope this little lesson on debugging helps :) (Edited for easier Readability)
30th Jan 2018, 7:11 PM
Daffy Dubz
0
thank u! you're right! the issue is with calculating no of rows of the query! as you said in c++ it's cout<< here the echo does the same thing! i should try replacing the variables with actual values and see if it works!
30th Jan 2018, 7:49 PM
Vithul T Nair
Vithul T Nair - avatar
0
i fixed it ! found out the error by debugging! thank you for your help :) and you need to add only add . while appending any variable to a string! here it's not necessary here as just the value of the variable is echoed.
30th Jan 2018, 8:55 PM
Vithul T Nair
Vithul T Nair - avatar
- 1
Glad I could help, my curiosity got the best of me, looks like PHP uses a period [ . ] for including variables in string (according to google). so your query would look like; "select * from files where '" . $variable . "' some more text blah blah" just be mindful of the single quote symbols for the sql string interesting to see how different languages handle the same process haha
30th Jan 2018, 8:53 PM
Daffy Dubz