I am trying to output a count of the number of results I have in a survey. The query itself ($sql) seems correct, because I do not get the error I have included, when I know that works. What isn't happening is the output. I have all my PHP tags and my database connection in place just fine, so I haven't included them here. They function just fine. What I need to know is how to get this outputted. This echoing for a count had worked for a previous PHP database set, but it is not working for this one.
My database has just ONE table, named survey. 'sur_cnt' is an auto-increment field that adds up whenever a new input is added to the database. My page output comes out blank, so I believe something is wrong with the echo, but I'm not sure what.
$sql = "SELECT COUNT(`sur_cnt`) FROM Survey";
$num = mysqli_query($db, $sql) or die('Error
querying database.');
$num_results = $result->num_rows ;
echo $num_results ;
try this query
because COUNT() using with groub by cluase
$sql = "SELECT * FROM Survey"; //remove count()
$result= mysqli_query($db, $sql) or die('Error querying database.');
$num_results = mysqli_num_rows($result) ;
echo $num_results ;
You need to use some form of fetch method to actually retrieve the count from your SQL statement - just the same as any other type of data from the database...
$sql = "SELECT COUNT(`sur_cnt`) FROM Survey";
$num = mysqli_query($db, $sql) or die('Error querying database.');
$row = mysqli_fetch_array($num) ;
echo $row[0] ;
Related
im trying to figure out what's wrong with this code.
im getting the Call to a member function fetch_assoc() error but I can't figure out why
for the record - im trying to build an eCommerce site for a project in college and with the following code im trying to reduce the item stock when a purchase is made.
Just to mention - I executed the SELECT query in PhpMyAdmin and it worked
<?php
require 'config.php';
session_start();
$sql = "SELECT * FROM cart,product WHERE product.product_code = cart.cart_product_code";
$result = mysqli_query($conn, $sql);
if ($result->num_rows > 0){
while($row = $result->fetch_assoc()){
$stock=$row['product_stock']-$row['quantity'];
$id_stock=$row['cart_product_code'];
$sql= "UPDATE product SET product_stock='$stock' WHERE product_code='$id_stock'";
$result = mysqli_query($conn, $sql);
$query="TRUNCATE TABLE Cart";
$clear= mysqli_query($conn, $query) or die("Invalid query");
}
}
You are overriding the $result object inside the while loop by querying an update query. You could execute the query without setting any variable :
$sql= "UPDATE product SET product_stock='$stock' WHERE product_code='$id_stock'";
mysqli_query($conn, $sql);
Also, I strongly suggest you to use prepared statement to avoid any form of SQL injection.
I am having trouble selecting my data from a database and displaying it. I have looked at tutorials and i still get the same error. Some help would be appreciated. The error i am getting is couldnt fetch result.
$sql = "SELECT * FROM data";
$result = mysql_query($sql) or die("couldnt fetch result");
if($result > 0){
while ($rows = mysql_fetch_array($result)){
$username = $rows['username'];
echo $username;
}
}
Just do that (assuming got it right connecting to DB, first thing to check !)
$sql = "SELECT * FROM `data`"; // data is a reserved keyword, protect it !!!
$result = mysql_query($sql) or die("couldnt fetch result"); // potentially diying here
if($result){
while ($row = mysql_fetch_assoc($result)){
$username = $row['username'];
echo $username;
}
}
If what you're getting is literally 'couldnt fetch result' it means your mysql_query() fails, and die statement takes over. Check your database connection.
I think the very simple problem is that you check if the $result is greater then 0. But you get an resource.
$conn = mysql_connect.......
$sql = "SELECT * FROM data";
$result = mysql_query($sql) or die("couldnt fetch result");
if($result){
while ($rows = mysql_fetch_array($result)){
$username = $rows['username'];
echo $username;
}
}
And if you see your die statement you have an error in your SQL Syntax. Its very short but its possible that your table doesn't exist in that database you're trying to connect. I hope you have a connect before and its not your complete code.
You use the old mysql functions. Its better to use MySQLi or PDO.
And DATA is a reserved keyword its possible that you get problems if you use it in your query. Rename your table in prefix_data for example.
https://dev.mysql.com/doc/refman/5.7/en/keywords.html
I am trying to delete a file or copy a row into a new table, depending on a $_GET.
The $_GET works fine, and I'm not including all the code, I know it isn't relevant.
The table copy works, but the select statement that gets called when the $_GET is a different value returns nothing, except when I copy the query directly into phpmyadmin.
Base code:
$pID = $_GET['pID'];
$con = mysqli_connect("...","...","...","...");
The following works:
$query = 'INSERT INTO `photos` (`id`, `photo1`, `photo2`, `demographic_id`)
SELECT `id`, `photo1`, `photo2`, `demographic_id`
FROM `photos_queue`
WHERE `photos_queue`.`demographic_id` = '.$pID;
mysqli_query($con, $query);
This does not:
$query = 'SELECT `photo1` FROM `photos_queue` WHERE `demographic_id` = '.$pID;
$result = mysqli_query($con, $query);
print($result);
unlink($result);
I've printed $query and the value of it is valid; I can copy it directly into phpmyadmin and it will work fine.
mysqli_query() doesn't return the table data, it just returns a resource that can be used to fetch it. You need to do:
$result = mysqli_query($con, $query) or die (mysqli_error($con));
$row = mysqli_fetch_assoc($result);
$filename = $row['photo1'];
print($filename);
unlink($filename);
($row = mysqli_fetch_array($result)
This should be placed after,
$result = mysqli_query($con, $query);
I'm trying to pull some information from a database, and the connection is working, but for some reason it isn't recognizing my query, even though I confirmed the query in the database with SQL and had it "generate PHP code". The echo statement is coming up blank. It's a mySQL database. Thanks for your help.
$query = "SELECT `contact` FROM `contactinfo` WHERE member=\'Henry\'";
$contact = mysqli_query($db,$query);
echo $contact;
$contact contains MySQL result object you need to fetch data from this to use this in your application.
$query = "SELECT `contact` FROM `contactinfo` WHERE member = 'Henry'";
$contact = mysqli_query($db, $query);
while ($row = mysqli_fetch_row($contact)) {
echo $row[0]; // 0 to n indicates the Column(s) Selected in SELECT Query
}
$db_user="root";
$db_host="localhost";
$db_password="root";
$db_name = "fayer";
$conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn't connect to server");
// perform query
$query = 'SELECT * FROM posts';
$result = mysqli_query($conn, $query) or die ("Couldn't execute query.");
// use returned data
while($row = mysqli_fetch_assoc($result))
{
echo $row['title'];
}
I get in the browser: "mysql problem".
Help!
UPDATE
I have echoed the query. It shows SELECT * FROM posts and when I query manually it gets the rows.
I think it has something to do with mysqli. I think i should use mysql. Do u think I have incompatibility problems with mysqli?
i have echoed it. it shows SELECT * FROM posts. and when i query manually it gets the rows.
i think it has something to do with mysqli. i think i should use mysql. do u think i have incompatibility problems with mysqli?
You have empty WHERE clause. Remove it or add a search condition.
Change
$result = mysqli_query($conn, $query) or die ("Couldn't execute query.");
to
$result = mysqli_query($conn, $query) or die ("Couldn't execute query because: " . mysqli_error());
and you will know why the query is failing. Rule of thumb: Whenever you have a failed query, print it out and run it through phpmyadmin or some other raw-query executor and you will discover very quickly what the problem is.