a Small help about php
#1

hey , i know the forum is about samp, but yeh, i need some help about php, i put it in this section cuz its nothing related to samp
take a look at this photo first


What i Want to do and you can see from the photo, is To know the Name of the player using the id..
example : Name "Larry" In the colmun of id '2' , So i'm asking if its possible to search the "Name" Of the Proposed "ID"
Reply
#2

You mean using SQL ?

You can use something similar to:
Код:
SELECT * FROM `users` WHERE `name` = 'Bla'
Just change the where to be the name instead of the ID... :/

And if you mean that you want them to be listed by ID, You can use something similar to the following SQL:
Код:
SELECT * FROM `users` ORDER BY `id` ASC
Offtopic TIP:
If you are making a page to show all players, NEVER SHOW ALL OF THEM IN 1 PAGE.
Make sure to make a paging system to only show like 10 or 30 per page.
You can use the LIMIT keyword, ****** is your best friend.
Reply
#3

What Ahmad didn't note, that its important to use "LIMIT 1" if you expect only one result, and it won't go any further in your database to search for more, once it reached the limit.
Reply
#4

i guess he didn't understand me well :/

what i want is to count the columns of my MYsql table becausein my acp : there is a Support Desk but before entering it , it tells you how much situations are there in a small eclipse... all the support tickets save in a mysql database , so i want to count all the columns in that database and show the INT result(example:the result can be : 6 Support tickets in total) before the admin enters the desk..
Thanks
Reply
#5

Use this sql: (or something similar)

Код:
SELECT count(*) FROM `table`
This is just the concept, You might want to add a WHERE clause to only select tickets by a certain users.

USAGE:
PHP код:
$query mysqli_query($con"SELECT count(*) FROM `table`");
$row mysql_fetch_array($query);
$total $row[0];
echo 
"Total rows: " $total
I don't know if this is the best method to do it or not though, Kaperstone might help u more about it...
Reply
#6

Quote:
Originally Posted by Ahmad45123
Посмотреть сообщение
I don't know if this is the best method to do it or not though, Kaperstone might help u more about it...
EDIT: completely messed up as I looked at his post and your respond.
On his post he requests to count columns, you answered how to count rows, and so I confused.


However, to count fields
PHP код:
$mysqli->query("SELECT * FROM `users` LIMIT 0")->field_count 
a bit shorter way to count rows (which is what being searched for)
PHP код:
$mysqli->query("SELECT count(*) FROM `users`")->fetch_array()[0
MySQL connection giving data to the query, query launches and return data -> data is being converted into array, index 0 in the array is being returned.

I see there is a small confusion between rows, fields and columns, so I want to clarify the difference:
Field's are the values that are being fetched
Columns are vertical rows that you can name in order to know which data to fetch out of a set.
Rows are the horizontal rows that contain the data you stored, you can add an ID and use it as an index to each row of data.

Reply
#7

Quote:
Originally Posted by Kaperstone
Посмотреть сообщение
It will return the number of rows in the database. (which is by the way the least transferring data through the socket, dunno about the performance, but I believe its also faster, because it doesn't have to select anything)

the fastest way to count fields:
PHP код:
$mysqli->query("SELECT * FROM `users` LIMIT 0")->field_count 
Wut ?
You are having a LIMIT 0, That would get no rows at all.. How'd it even work ? :////

E: nvm.. Just saw ur edit :P
He talked to me in skype, And he did want it for rows anyways..
Reply
#8

Quote:
Originally Posted by Ahmad45123
Посмотреть сообщение
Wut ?
You are having a LIMIT 0, That would get no rows at all.. How'd it even work ? :////

E: nvm.. Just saw ur edit :P
He talked to me in skype, And he did want it for rows anyways..
Surprisingly, it does.
I tested it and the query returns the number of selected columns even if there is no result;


EDIT: I do many edits before I get to the final one XD
Reply
#9

Quote:
Originally Posted by Kaperstone
Посмотреть сообщение
Surprisingly, it does.
I tested it and the query returns the number of selected fields even if there is no result;
Fields or rows ? xD

E: Ohhhhh.. Just realized its field_count, dumb me :P
I all that time thought its row_count.

Quote:
Originally Posted by Kaperstone
Посмотреть сообщение
EDIT: I do many edits before I get to the final one XD
I do that too tbh, haha.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)