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']);
Related
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";
I am trying to select a row in one table and if it does exists in the second table,do something and if it doesn't,copy the values from table one into the second.
The problem is that,once it finds match (a row that is present in the first and second table),it shows errors for all other rows that did not match.
This is the error
Notice: Trying to get property of non-object in C:\wamp\www\loans.php on line 26
This is my code
<?php
//error_reporting(0);
$mysqli = new mysqli("localhost", "root", "123456", "test");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$query = "select dest_msisdn,text_message,service_id,sender_name from incoming_sms";
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
$dest_msisdn = $row['dest_msisdn'];
$text_message = $row['text_message'];
$service_id = $row['service_id'];
$sender_name = $row['sender_name'];
/**
Transactions
*/
$m = $mysqli->query("SELECT tel from transactions where tel = $dest_msisdn")->fetch_object()->message;
if(empty($m)){
$ti = "insert into transactions(message,tel) values($text_message,$dest_msisdn)";
$mysqli->query($ti);
}
}
/* free result set */
$result->free();
}
/* close connection */
$mysqli->close();
?>
I want to insert rows that i find in table one and are not in table two.
I suggest you check the rows yielded before accessing any properties that the result yields:
$m = $mysqli->query("SELECT tel, message from transactions where tel = $dest_msisdn");
// hi! im missing ^^
// you are accessing the property "message" but its not a selected column in your query
if($m->num_rows <= 0) {
$ti = "insert into transactions(message,tel) values($text_message,$dest_msisdn)";
$mysqli->query($ti);
}
When you chain like that you are assuming there will always be a row. When there isn't a row you cant get the property from it. That's why you get the error. So do something like this:
$r = $mysqli->query("SELECT tel from transactions where tel = $dest_msisdn");
if ($r->num_rows == 0) {
$ti = "insert into transactions(message,tel) values($text_message,$dest_msisdn)";
$mysqli->query($ti);
}
This doesn't answer your question directly, but it will fix your problem and probably solve some others.
The way you are going about inserting (where you first SELECT and then loop through the results in PHP, then SELECT from another table, then INSERT) seems unnecessarily complex. You can get rid of your PHP loop and just execute the INSERT in a single SQL statement without all the PHP overhead. Let your DB do the work for you.
All of this:
$query = "select dest_msisdn,text_message,service_id,sender_name from incoming_sms";
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
$dest_msisdn = $row['dest_msisdn'];
$text_message = $row['text_message'];
$service_id = $row['service_id'];
$sender_name = $row['sender_name'];
/**
Transactions
*/
$m = $mysqli->query("SELECT tel from transactions where tel = $dest_msisdn")->fetch_object()->message;
if(empty($m)){
$ti = "insert into transactions(message,tel) values($text_message,$dest_msisdn)";
$mysqli->query($ti);
}
}
/* free result set */
$result->free();
Could be changed to just this:
$mysqli->query("INSERT INTO transactions(message,tel) SELECT text_message, tel FROM transactions INNER JOIN incoming_sms on transactions.tel = incoming_sms.dest_msisdn")
This is probably a simple error, but I'm stumped...
I have a database that has entries inserted using AJAX on a chrome extension, which works great and inserts instantly.
I have a separate PHP file that is being used to output the number of entries in a table. This works, but is takes a long time for the entries to update to the right number when called. I need it to be up to date instantly.
Is there any reason why it's taking so long for the query to output the correct number, when the table itself is updating instantly?
<?php
$link = mysqli_connect("localhost", "root", "", "fyp");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if ($result = mysqli_query($link, "SELECT * FROM links")) {
$row_cnt = mysqli_num_rows($result);
printf($row_cnt);
mysqli_free_result($result);
}
/* close connection */
mysqli_close($link);
?>
Thanks.
I think you should simple run:
if ($result = mysqli_query($link, "SELECT count(*) AS `nr` FROM links")) {
$row_cnt = mysqli_fetch_assoc($result);
printf($row_cnt['nr']);
mysqli_free_result($result);
}
It will be the best one. Or if your links are not removed at all you could run:
if ($result = mysqli_query($link, "SELECT id FROM links ORDER BY id DESC LIMIT 1")) {
$row_cnt = mysqli_fetch_assoc($result);
printf($row_cnt['id']);
mysqli_free_result($result);
}
Hand off the processing to the database by using an aggregate query and return a scalar result with mysqli_result:
if ($result = mysqli_query($link, "SELECT COUNT(1) FROM links")) {
$row_cnt = mysqli_result($result, 0);
printf($row_cnt);
mysqli_free_result($result);
}
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.