I have this code for getting how many results there are in results there are in a column:
$result = "SELECT name FROM users";
echo $result;
How can I display a certain number of a specific string? For example, I want to search for the name "Andrew" in the column "name". There are 20 results for "Andrew". How can I echo out "20"?
You would COUNT them:
$result = "SELECT COUNT(`name`) AS `NameCount` FROM `users` WHERE `name` = 'Andrew'";
For all names you could do this:
SELECT `name`, count(`name`) AS `NameCount`
FROM `users`
GROUP BY `name`
Which would result in something like this:
name | NameCount
----------------------
Andrew | 20
Bob | 6
Carol | 125
If you will use Mysqli, you can use $num_rows. Somelithing like:
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if ($result = $mysqli->query("SELECT Code, Name FROM Country ORDER BY Name")) {
/* determine number of rows result set */
$row_cnt = $result->num_rows;
printf("Result set has %d rows.\n", $row_cnt);
/* close result set */
$result->close();
}
/* close connection */
$mysqli->close();
?>
also, you can use mysql count() function like:
$result = "SELECT count(*), name FROM users";
Related
i have the following code. When i run into phpmyadmin the result returns correctly 9 rows of users and a column called |count(*) with the count next to each user. Whats wrong on my while and cant return me the count? It returns just the user when in php code
<?php
if ($result = $mysqli->query("SELECT n.user, count(*) AS count_user FROM metadata n group by n.user")) {
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
printf ($row['user'], $row[count]);
}
}
/* free result set */
?>
hi if i understand correctly your Question try this :
<?php
$mysqli = new mysqli("localhost", "root", "root", "test");
if (mysqli_connect_errno()) {
printf("Échec de la connexion : %s\n", mysqli_connect_error());
exit();
}
if ($result = $mysqli->query("SELECT name FROM users ORDER BY name")) {
/* get num rows */
$row_cnt = $result->num_rows;
while($row = mysqli_fetch_assoc($result)) {
printf($row['name']);
}
printf('Nombre of line %s',$row_cnt);
$result->close();
}
$mysqli->close();
You have used wrong index. you gave alias count_user in your query so you must use it when you fetch.
Just change this
printf ($row['user'], $row[count]);
to
printf ($row['user'], $row['count_user']);
I have the following table set in my mysql database
mem_id | pid | content
0 | 1 | All the content is here
0 | 2 | All the content is here
0 | 3 | All the content is here
Now the problem is to get all matching mem_id values and store it in a array in php.
Example: A variable called $id has value 0
So now I have to get all values under the column content but only those which matches the mem_id of the user.
Could anyone help me with this, I need it in php and using mysql query to get all the values.
My current code:
$con=mysqli_connect("localhost","*****","******","*****");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM user_friends WHERE mem_id = '$_SESSION[SESS_MEMBER_ID]' LIMIT 1");
while($row = mysqli_fetch_array($result))
{
$friends = $row['fid'];
}
SELECT * FROM yourTABLE WHERE mem_id=0 // or 1 or 2 or 3 etc
Using PHP you could query it like:
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$id = intval($id); // Put your ID here
$query = "SELECT * FROM yourTABLE WHERE mem_id=$id";
if ($result = mysqli_query($link, $query)) {
while ($row = mysqli_fetch_assoc($result)) {
print_r($row);
}
mysqli_free_result($result);
}
?>
$query="select * from TABLE_NAME where `mem_id`='$id'";
*Note, if you can think of a more constructive title, please do edit.
I have a table that looks like this:
id | forGame | otherType | otherName | otherDesc
9 |+Stellar+Dawn |Character |Car |Car
10 |+Stellar+Dawn |Item |Brugson Burson |a guy
11 |+Stellar+Dawn |Item |Space Pie |A pie from space
Now, my problem is that I'm trying to seperate multiple queries into one:
$gameOther_typeItem = $database->queryDB("SELECT * FROM `mainSite_others` WHERE otherType='Item' AND forGame='$gameName'");
$gameOther_typeEntity = $database->queryDB("SELECT * FROM `mainSite_others` WHERE otherType='Entity' AND forGame='$gameName'");
$gameOther_typeCharacter = $database->queryDB("SELECT * FROM `mainSite_others` WHERE otherType='Character' AND forGame='$gameName'");
(Don't worry about the $database->... part, that's my class.)
How might I combine these into one?
Thank you.
You could use "`OR'".
SELECT * FROM my_table WHERE column1 = value1 AND column3 = value3 OR column2 = value2 AND column3 = value3
Or, you could use multiple query to execute multiple queries at the same time?
http://php.net/manual/en/mysqli.multi-query.php
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT CURRENT_USER();";
$query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";
/* execute multi query */
if ($mysqli->multi_query($query)) {
do {
/* store first result set */
if ($result = $mysqli->store_result()) {
while ($row = $result->fetch_row()) {
printf("%s\n", $row[0]);
}
$result->free();
}
/* print divider */
if ($mysqli->more_results()) {
printf("-----------------\n");
}
} while ($mysqli->next_result());
}
/* close connection */
$mysqli->close();
?>
SELECT * FROM mainSite_others WHERE (otherType='Item' OR otherType =
'Entity' OR otherType = 'Character') AND forGame='$gameName';
EDIT: I'm sorry I was unclear, I try to explain it right this time.
I have this data in a database table called tMenu:
id page_nl text
1 index_1 index1_text
2 index_2 index2_text
3 index_3 index3_text
These are 3 pages on my website called (in this case) index_1, index_2 and index_3. I have programmed it is such a way that each page shows there index1_text.
What I want now is to show page_nl in a menu. The code I have now is:
$qh = mysql_query('SELECT id, page_nl FROM tMenu ORDER BY id');
$row = mysql_fetch_array($qh);
$id = 'id';
<? echo $row['page_nl']; $id=="1" ;?>
<? echo $row['page_nl']; $id=="2" ;?>
<? echo $row['page_nl'];?>
In the way it is now it shows only page_nl from id 1, but I want that the next link shows page_nl from id 2. I hope my question is more clear now.
Your question isn't very clear - are you asking for something like this
$sql = "select * from yourtable where id = 1";
$result = mysql_query($sql);
//I am assuming there are more than 1 rows for ID 1
while($row = mysql_fetch_assoc($result)) {
echo $row['page_nl'];
}
OR ============================
$sql = "select * from yourtable"; //Select All
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)) {
if($row['id'] == 1)
{
echo $row['page_nl'];
}
}
Presuming you mean database table, you need a routine to connect to the database then fetch the info:
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "databasename"); // database name
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT * FROM table_name"; // put table name here
$result = $mysqli->query($query);
/* numeric array */
/* associative array */
$row = $result->fetch_array(MYSQLI_ASSOC);
printf ("%s (%s)\n", $row["id"], $row["page_nl"]);
/* free result set */
$result->free();
/* close connection */
$mysqli->close();
?>
You need to use a foreach($var as $key =>$value) loop
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP - Simple way to read single record from MySQL
I want to query multiple rows in mysql as variable. for example:
SELECT name, tid FROM term_data WHERE vid = 2
this is the result:
name | tid
-----|----
Jack | 55
Tony | 87
John | 32
then I want to use while:
while (...) {
print "My name is: $name and my ID is: $tid";
//name and tid should be printed from database.
}
I can query one row and put it in a while but How is this possible?
Example from the PHP manual:
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";
if ($result = mysqli_query($link, $query)) {
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
}
/* free result set */
mysqli_free_result($result);
}
/* close connection */
mysqli_close($link);
?>
<?php
//Conection
...
$query= "SELECT name, tid FROM term_data WHERE vid = 2";
if (query_run=mysql_query($query)) {
while ($query_row=mysql_fetch_assoc($query_run)) {
$name=$query_row['name'];
$tid=$query_row['tid'];
echo 'My name is'.$name.'and my ID is'.$tid;
}
}
else {
echo mysql_error();
}
?>
Only thing remains is resolving the connection which is up to you.