$How_Many_Manufacturers = "SELECT COUNT(manufacturer), manufacturer
FROM products
WHERE name LIKE '%$new_title%'
GROUP BY manufacturer";
$result2 = mysql_query($How_Many_Manufacturers, $connection) or die(mysql_error());
$num_rows = mysql_num_rows($result2);
if ($num_rows == 0)
{
echo "<div id=\"noMatches\">No Matches</div>";
}
else {
}
The if statement will not work.
How can I correct this script?
#Arjan You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-25,25' at line 4 -RPM
make sure you escape $new_title in the query.
$How_Many_Manufacturers = "SELECT COUNT(manufacturer), manufacturer
FROM products
WHERE name LIKE '%".mysql_real_escape_string($new_title)."%'
GROUP BY manufacturer";
SELECT COUNT will always return a row (even if the count is zero). Simply remove the COUNT, or fetch the row to see the count.
Related
I am trying to use a count function to show me how many employees have the title "Sales"
if( !$connect)
{
die("ERROR: Cannot connect to database $db on server $server
using user name $user (".mysqli_connect_errno().
", ".mysqli_connect_error().")");
}
else
{
$userQuery = "COUNT(empID) FROM personnel WHERE jobTitle='Sales'";
$result = mysqli_query($connect, $userQuery);
if (!$result)
{
die("Could not successfully run query ($userQuery) from $db: " .
mysqli_error($connect) );
}
if (mysqli_num_rows($result) == 0)
{
print("No records found with query $userQuery");
}
else
{
print("<h1>SALES STAFF REPORT</h1>");
while ($row = mysqli_fetch_assoc($result))
{
print("<p>There are
".$row['COUNT(empID)']."</p>");
}
}
mysqli_close($connect); // close the connection
}
?>
I am getting this error:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'COUNT(empID) FROM personnel WHERE jobTitle='Sales'' at line 1
on this line:
$userQuery = "COUNT(empID) FROM personnel WHERE jobTitle='Sales'";
I am unsure of what I am doing incorrectly, but any help would be appreciated.
End result should read "There are 4 sales staff".
My professor did include this in the assignment, not sure if that will help.
"There are two different MySQL functions that you can use here: you can
modify the SELECT statement to use a MySQL aggregation function,
or you can use the MySQL function that returns the number of records in the
result set. "
You are missing the SELECT keyword when making the statement.
You have:
$userQuery = "COUNT(empID) FROM personnel WHERE jobTitle='Sales'";
It should be:
$userQuery = "SELECT COUNT(empID) FROM personnel WHERE jobTitle='Sales'";
I want to get the ID of an image that is boostAmount-boostStart>total. Currently if an image exists that is appropriate it works. However, if there is nothing appropriate to show I get this error.
Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY RAND() LIMIT 1' at line 1
$photoToGuess = mysql_query("SELECT photoID,id,total,boostStart,boostAmount,auth,photoUploadDate FROM photos WHERE (boostAmount !=0) AND ((boostAmount-boostStart)>total) AND (auth=2 OR auth=5 OR auth=7) ORDER BY RAND() LIMIT 1") or die("Invalid query: " . mysql_error());
$getphotoToGuess = mysql_fetch_array($photoToGuess);
//Yes
if(mysql_num_rows($photoToGuess) > 0)
{
//do something
}
Try this, I removed die condition you can achieve die condition using if statement...It is working fine chech once.
$photoToGuess = mysql_query("SELECT photoID,id,total,boostStart,boostAmount,auth,photoUploadDate FROM photos WHERE (boostAmount !=0) AND ((boostAmount-boostStart)>total) AND (auth=2 OR auth=5 OR auth=7) ORDER BY RAND() LIMIT 1");
$getphotoToGuess = mysql_fetch_array($photoToGuess);
//Yes
if(mysql_num_rows($photoToGuess) > 0)
{
//do something
}
You can refer this http://php.net/manual/en/function.mysql-query.php
Your if condition is wrong
why are you using $photoToGuess in your if conditioner code here
if(mysql_num_rows($getphotoToGuess ) > 0){
// do something
}
Was hoping someone could give me some help. I have been trying to learn pagination but have run into an issue. when I run this to get my total rows count:
$sql = "SELECT COUNT (*) FROM item WHERE fid='17'";
$query = mysqli_query($con, $sql);
$row = mysqli_fetch_row($query);
$rows = $row[0];
$rows comes back with no value, I am under the impression that $rows should contain the total number of records from item with the fid of 17, when I run SELECT COUNT (*) FROM item WHERE fid='17' in phpmyadmin it returns 98 which is the correct count. Directly before the above code I use this code to connect to the db, which I use again later to display the records which works fine.
$con=mysqli_connect("$host","$username","$password","$dbname");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
This statement displays records later in the script
$sql = mysqli_query($con,"SELECT * FROM item WHERE fid='17' ORDER BY id DESC $limit ");
So there is data and the info is correct.
I have been following this tutorial on the subject http://www.developphp.com/view.php?tid=1349 and it works like a charm on his example in the video, all I have changed is the database particulars to mine. Can't figure this one out, been stuck for days so now I am bothering you fine folks.
Update: Changed
$query = mysqli_query($con, $sql);
$row = mysqli_fetch_row($query);
$rows = $row[0];
to
if ($result=mysqli_query($con,$sql))
{
// Return the number of rows in result set
$rows=mysqli_num_rows($result);
// Free result set
mysqli_free_result($result);
}
And everything is working now. Still wish I knew what was wrong with my original code but I will be moving on. Thanks Phil Perry for all your help this morning!
Well, you could SELECT * FROM... and then call mysqli_num_rows($query). Probably slower than SELECT count(*). BTW, I presume that item is not a reserved word in SQL. You can always wrap table and field names in backticks (not quotes ' or "). You could also try SELECT count(*) AS myCount....
try this,
$sql = "SELECT COUNT(*) FROM item WHERE fid='17'";
$query = mysqli_query($sql);
$row = mysqli_fetch_array($query);
$rows = $row[0];
$rowcount = 'SELECT COUNT(1) FROM (select * from isk.edi_site where postal_code = 123456)';
$stmt= oci_parse($conn, $rowcount);
oci_execute($stmt);
$num_row = oci_fetch_assoc($stmt);
$num = count($num_row, COUNT_RECURSIVE);
echo $num;
It counts and return the number "1". When I use the same SQL query in Oracle SQL developer, it echos out 4000+ count. Where would my mistake be? The column count works well..
A COUNT() query only returns a single row so you need to grab it from the oci_fetch_assoc() call. What you're doing is counting the rows in the result set which will always be 1.
$row = oci_fetch_assoc($stmt);
echo $row['COUNT(1)'];
Or give the count an alias:
SELECT COUNT(1) mycount FROM ...
$row = oci_fetch_assoc($stmt);
echo $row['MYCOUNT'];
First, I coded this, which looks inside a table, gets the last 10 entries, and displays them. The output is as expected, a list of the 10 last entries in the database.
$query = "SELECT dfid FROM downloads_downloads ORDER BY did DESC limit 10";
$dlresult = mysql_query( $query );
$i=0;
$num = mysql_num_rows ($dlresult);
while ($i < $num) {
$dfid= mysql_result($dlresult,$i,"dfid");
echo "<b>filenumber:</b> $dfid <br>";
++$i;
}
But I don't just need the filenumber. I need the actual filename and url from another table. So I added a select statement inside the while statement, using the file number.
But for some reason, this code only displays one filename instead of 10. I know, from the above code, it's getting all 10 file numbers.
$query = "SELECT dfid FROM downloads_downloads ORDER BY did DESC limit 10";
$dlresult = mysql_query( $query );
$i=0;
$num = mysql_num_rows ($dlresult);
while ($i < $num) {
$dfid= mysql_result($dlresult,$i,"dfid");
$query2 = "SELECT file_name, file_name_furl FROM downloads_files WHERE file_id = '$dfid'";
$dlresult2 = mysql_query( $query2 );
$dlfile_name= mysql_result($dlresult2,$i,"file_name");
$dlfile_name_furl= mysql_result($dlresult2,$i,"file_name_furl");
echo "filenumber: $dfid <br>"; //Shows 10, as expected.
echo "filename: $dlfile_name - $dlfile_name_furl <br>"; //Shows only 1?
++$i;
}
I can manually execute the sql statement and retrieve the file_name and file_name_furl from the table. So the data is right. PHP isn't liking the select within the while statement?
Looks like you're only going to ever have 1 row, in your 2nd select statement, because you are just selecting one row, with your where statement.
So, you're only going to ever have row 0 in the 2nd statement, so its only finding row 0 on the first loop. try instead:
$dlfile_name= mysql_result($dlresult2,0,"file_name");
$dlfile_name_furl= mysql_result($dlresult2,0,"file_name_furl");
However, insrtead of making 11 separate queries, try using just one:
$link = new mysqli(1,2,3,4);
$query = "SELECT downloads_downloads.dfid, downloads_files.file_name, downloads_files.file_name_furl FROM downloads_downloads LEFT OUTER JOIN downloads_files ON downloads_files.file_id = downloads_downloads.dfid ORDER BY downloads_downloads.dfid DESC limit 10;
$result = $link->query($query) ;
if((isset($result->num_rows)) && ($result->num_rows != '')) {
while ($row = $result->fetch_assoc()) {
echo "filenumber: $row['dfid'] <br>";
echo "filename: $row['file_name'] - $row['file_name_furl'] <br>";
}
Read up on mysql joins http://www.keithjbrown.co.uk/vworks/mysql/mysql_p5.php
I'm not sure if this is syntax correct, but it gives you the right idea =)