I'm trying to get the number of comments on a particular item.
$stmt1 = $conn->prepare("SELECT COUNT(r_value) FROM ratings WHERE r_snippet=? AND r_value=3 OR r_value=2");
$stmt2 = $conn->prepare("SELECT COUNT(c_id) FROM comments WHERE c_snippet=?");
foreach ($snippets as $snippet){
$s_id = $snippet['s_id'];
$s_thumb = $snippet['s_thumb'];
$stmt1->bind_param("i",$s_id);
$stmt1->execute();
$stmt1->bind_result($numLikes);
$stmt1->fetch();
$stmt2->bind_param("i",$s_id);
$stmt2->execute();
$stmt2->bind_result($numComments);
$stmt2->fetch();
?>
** HTML here **
<?php
}
$stmt1->close();
$stmt2->close();
?>
$numLikes works fine, but $numComments seems to be returning NULL (from var_dump) and I'm not sure why. The SQL in phpMyAdmin works fine and returns the number of comments...
You probably want change this for your first query
... AND r_value=3 OR r_value=2
To
... AND (r_value=3 OR r_value=2)
OR THIS
... AND r_value IN (2,3)
But for the second query try something simple just for debug
$stmt2 = $conn->prepare("SELECT * FROM comments WHERE c_snippet=10");
then
$stmt2 = $conn->prepare("SELECT count(*) FROM comments WHERE c_snippet=10");
Related
Following a tutorial, I execute SQL queries using PHP like so:
$query = $pdo -> prepare("SELECT id FROM Families WHERE family_name = ?");
$query -> bindValue(1, $family_name);
$query -> execute();
$query2 = $pdo -> prepare("SELECT * FROM Categories WHERE family_id = ?");
$query2 -> bindValue(1, $query); //ISSUE IN THIS LINE
$query2 -> execute();
My question: How do I take the result of the first query, the id, and put it as input in the second query?
You need to fetch the result, execute only runs the query in db, it does not get the values, try this which will return an array:
$result = $query->fetchAll();
If you know you only want one result, try:
$result = $query->fetch();
store the first query result in an object like
$result = $query->execute();
if ($result && $result->num_rows > 0)
{
$family = $Result->fetch_object();
$query2 = $pdo -> prepare("SELECT * FROM Categories WHERE family_id = ?");
$query2 -> bindValue(1, $family->id);
$query2 -> execute();
}
Please use limit 1 so that object don't return more than one id
$stmt1 = $pdo->prepare("SELECT id FROM Families WHERE family_name = ?");
$stmt1->bindValue(1, $family_name);
$stmt1->execute();
$family = $stmt1->fetch(PDO::FETCH_OBJ); //You can use fetch inorder to return 1 result for more use fetchAll();
//Here $family is now an object array
//Stored id in $family_id variable
$family_id = $family->family_id; //All the columns will save as properties now so you can easily access each of them
$stmt2 = $pdo->prepare("SELECT * FROM Categories WHERE family_id = ?");
$stmt2->bindValue(1, $family_id);
$stmt2->execute();
$stmt2->fetchAll(PDO::FETCH_OBJ); //Used fetchAll method because It's returning more than 1 result I guess
Try combining the Queries:
SELECT f.* FROM Families INNER JOIN Categories c on c.id = f.family_id WHERE c.family_name = ?
Fetch the result as Mohsen Nazari has stated
I have numbers stored in my MySQL (paid). I need to SUM the columns.
$sql= "SELECT SUM(furniture) FROM paid";
$stmt = $connect->prepare($sql);
$stmt->execute();
$furniture = (int) $stmt->fetchColumn();
$sql= "SELECT SUM(groceries) FROM paid";
$stmt = $connect->prepare($sql);
$stmt->execute();
$groceries = (int) $stmt->fetchColumn();
//so on....
There are morthan 50 columns in the database. My question is, Is there a shorter way to write this so I can get the SUM for each column and assign it to a variable?
Try with single query
$sql = "SELECT SUM(`furniture`) AS sumFurniture,
SUM(`groceries`) AS sumGroceries ,
...
FROM `paid` ";
result can be get with
$sth = $connect->prepare($sql);
$sth->execute();
$result = $sth->fetch();
$sumFurniture = $result['sumFurniture'];
$sumGroceries = $result['sumGroceries'];
....
You can combine them as a Single SQL Query
SELECT SUM(furniture) AS furniture, SUM(groceries) AS groceries....... FROM paid
If your where clause is same you can combine the query like this:
select sum(<column_name1>) column_name1,sum(<column_name2>) column_name2 from tablename where <where>
From php, you can fetch it: using array index "column_name1","column_name2"
The array showcasef holds 20 items per page. I do 3 different queries within the foreach loop, which is 60 queries (just for the loop, there's additional queries too).
<?php
foreach($showcasef as $itemf){
$sf_id = $itemf['sf_id'];
$sf_url = $itemf['sf_url'];
$sf_title = $itemf['sf_title'];
$sf_urltitle = post_slug($sf_title);
// Fetch number of favs
$stmt = $conn->prepare("SELECT COUNT(f_id) FROM favourites WHERE f_showcaseid=?");
$stmt->bind_param("i", $sf_id);
$stmt->execute();
$stmt->bind_result($numfFavs);
$stmt->fetch();
$stmt->close();
// Fetch class
$stmt = $conn->prepare("SELECT avg(r_class) FROM ranks WHERE r_showcaseid=?");
$stmt->bind_param("i", $sf_id);
$stmt->execute();
$stmt->bind_result($sf_class);
$stmt->fetch();
$stmt->close();
// Fetch number of classes
$stmt = $conn->prepare("SELECT COUNT(r_class) FROM ranks WHERE r_showcaseid=?");
$stmt->bind_param("i", $sf_id);
$stmt->execute();
$stmt->bind_result($numfClasses);
$stmt->fetch();
$stmt->close();
?>
Render HTML here
<?php } ?>
Will this be a severe performance issue, or are these particular queries relatively simple? If I keep the columns indexed, should it perform okay with millions of rows (potentially)? Or can the queries be optimized/simplified?
Here's how I get the showcasef:
$stmt = $conn->prepare("SELECT s_id,s_url,s_title FROM showcase WHERE s_userid=? ORDER BY s_date DESC LIMIT $skippingFactor, 20");
$stmt->bind_param("i", $u_id);
$stmt->execute();
$stmt->bind_result($sf_id,$sf_url,$sf_title);
while($stmt->fetch())
{
$showcasef[] = [
'sf_id' => $sf_id,
'sf_url' => $sf_url,
'sf_title' => $sf_title
];
}
$stmt->close();
A few suggestions here.
Reuse prepared statements
You are creating three prepared statements inside the loop. Why don't you create your statements only once, and then reuse them using multiple binds?
<?php
$stmt1 = $conn->prepare("SELECT COUNT(f_id) FROM favourites WHERE f_showcaseid=?");
$stmt1->bind_param("i", $sf_id);
$stmt1->bind_result($numfFavs);
$stmt2 = $conn->prepare("SELECT avg(r_class) FROM ranks WHERE r_showcaseid=?");
$stmt2->bind_param("i", $sf_id);
$stmt2->bind_result($sf_class);
$stmt3 = $conn->prepare("SELECT COUNT(r_class) FROM ranks WHERE r_showcaseid=?");
$stmt3->bind_param("i", $sf_id);
$stmt3->bind_result($numfClasses);
foreach($showcasef as $itemf) {
$sf_id = ...
$stmt1->execute();
$stmt1->fetch();
/* if the fetch succeedes then $numfFavs will contain the count */
$stmt2->execute();
...
$stmt3->execute();
..
}
$stmt1->close();
$stmt2->close();
$stmt3->close();
Use a single query to Count the rows and calculate the average
You can combine the second and third statement a single SQL query:
SELECT COUNT(r_class) AS cnt, AVG(r_class) AS average
FROM ranks
WHERE r_showcaseid=?
Use a single query instead a foreach loop
With the previous suggestions you can get better performances. But are you really sure you need a foreach loop?
If your IDs are returned by another query, instead of a foreach loop is better to use a subquery:
SELECT f_showcaseid, COUNT(f_id)
FROM favourites
WHERE f_showcaseid IN (SELECT id FROM ... WHERE ...)
GROUP BY f_showcaseid
or you can provide a list of IDs to the query:
SELECT f_showcaseid, COUNT(f_id)
FROM favourites
WHERE f_showcaseid IN (?,?,?,?,?)
GROUP BY f_showcaseid
(you can dynamically create the list of ? if the number of IDs is not fixed)
You could do this in a single query I think.
Something like the following:-
SELECT f_showcaseid, COUNT(f_id), avg(r_class), COUNT(r_class)
FROM ranks WHERE r_showcaseid IN (".implode(',', $showcasef).")
GROUP BY f_showcaseid
Of course, to use parameters you would need to do that a bit more elegantly:-
<?php
$stmt = $conn->prepare("SELECT f_showcaseid, COUNT(f_id), avg(r_class), COUNT(r_class)
FROM ranks WHERE r_showcaseid IN (".implode(',', str_split(str_repeat('?', count($showcasef)), 1)).")
GROUP BY f_showcaseid");
foreach($showcasef as $itemf)
{
$stmt->bind_param("i", $itemf['sf_id']);
}
$stmt->execute();
$stmt->bind_result($numfClasses);
$stmt->fetch();
$stmt->close();
?>
hope someone can help me.
i have a very simple prepared SELECT statment in PHP:
$query_select = ("SELECT * FROM companies where user_name = ? ");
$stmt = $mysqli->prepare($query_select);
$stmt->bind_param("s", $user_name);
$stmt->execute();
$count = $stmt->num_rows;
in companies table I have several rows with the $user_name i`m trying to query. But i still get 0 rows as a result.
The strange thing is that the non PREPARED version works:
$query = 'SELECT * FROM companies WHERE user_name="'.$user_name.'"';
$result = $mysqli->query($query);
$count= $result->num_rows;
echo "Aantal: ".$count;
So my question is, does anyone know why the prepared version returns ZERO and the non prepared version returns the correct number of rows?
Add this line to your code between execute and num_rows statement.
$stmt->store_result();
You have to store it before counting it.
For mysqli prepared statements, you must take an additional step: storing the result.
Try this:
$query_select = ("SELECT * FROM companies where user_name = ? ");
$stmt = $mysqli->prepare($query_select);
$stmt->bind_param("s", $user_name);
$stmt->execute();
$stmt->store_result(); // <-- new line
$count = $stmt->num_rows;
May be you need to bind the result:
/* bind result variables */
$stmt->bind_result($district);
Full example here
I want to get a single value(aktiv) out of a table called wartung.
include('blog/includes/db_connect.php');
$query = $db->prepare("SELECT * FROM wartung");
$query->execute();
$query->bind_result($wartung_id, $aktiv, $grund);
The value of aktiv in the database is 1.
but this code doesn't get this 1 into the $aktiv variable.
PS: I'm not used to code with MySQL and PHP so if there is a beginner-bug please don't be mad.
Try:
include('blog/includes/db_connect.php');
$query = $db->prepare("SELECT aktiv FROM wartung");// I am assuming your column is named aktiv
$query->execute();
$query->bind_result($aktiv);
$query->fetch();
You are not fetching the value
include('blog/includes/db_connect.php');
$query = $db->prepare("SELECT * FROM wartung");
$query->execute();
$query->bind_result($wartung_id, $aktiv, $grund);
$query->fetch();