I’m trying to use "group by" instead of "DISTINCT" in my php file to select some rows that all of them have an specific column value and it’s "idchat".
And I want to get more than one columns
please help me!
I’ve checked every pages but I didn’t understand enything
<?php
$connection = mysqli_connect("localhost","---","pass","---");
$id = $_GET["id"];
$mobile = $_GET["mobile"];
$idchat = $_GET["idchat"];
if (strpos($mobile, '9') !== false) {
$query = "SELECT DISTINCT a,b,c,d,idchat FROM database where mobile = '$mobile' ORDER BY id DESC";
$result = mysqli_query($connection,$query);
while ($row = mysqli_fetch_assoc($result)) {
$array[] = $row;
}
header('Content-Type:Application/json');
echo json_encode($array);
}
mysqli_close($connection);?>
and this code gives me this error:
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in /home/---/test.php on line 12
See next output:
mysql> SELECT * FROM database;
ERROR 1064 (42000): 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 'database' at line 1
mysql> SELECT * FROM mytable;;
ERROR 1146 (42S02): Table 'test.mytable' doesn't exist
ERROR:
No query specified
mysql>
The first query is not understood because database is a reserved word. You should not use that to name a table. A work around is to add backquotes around the name:
mysql> SELECT * FROM `database`;
ERROR 1146 (42S02): Table 'test.database' doesn't exist
mysql>
"Could you correct my codes please?": Ok, but untested:
<?php
$connection = mysqli_connect("localhost","---","pass","---");
$id = $_GET["id"];
$mobile = $_GET["mobile"];
$idchat = $_GET["idchat"];
if (strpos($mobile, '9') !== false) {
$query = "SELECT DISTINCT a,b,c,d,idchat,id FROM `database` where mobile = '$mobile' ORDER BY id DESC";
$result = mysqli_query($connection,$query);
if ($result) {
while ($row = mysqli_fetch_assoc($result)) {
$array[] = $row;
}
header('Content-Type:Application/json');
echo json_encode($array);
}
else {
echo mysqli_error($connection);
}
mysqli_close($connection);?>
}
I also did add id to the query because of comment from #Raymond
Related
I am trying to get the details of a person using their id, I have the following code and it's showing me an error.
$id = isset($_GET['id']) ? isset($_GET['id']) : "";
$sql = "SELECT * FROM `ArtListing` where `id` = $id";
$result = $conn->query($sql);
if (!$result) {
die("Query failed " . $conn->error);
}
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo $row["id"] . "." . $row["name"] ;
}
If I use where the id is some random number like it gives me back the details. The error it's showing me is as following
Query failed 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 '' at line 1
It looks like the id you grabbing is undefined.
You should assign the $id variable as:
$id = isset($_GET['id']) ? $_GET['id'] : "0";
You cannot simply put variable inside a string
You need to properly concatinate it to become a part of a sting as follows
$sql = "SELECT * FROM `ArtListing` where `id` = '".$id."'";
You can test this code here
https://www.tehplayground.com/3HcoDppV0jAqdCYP
Take a look at combination of double quotes and single quotes.
Your SQL query string is created as follows
SELECT * FROM `ArtListing` where `id` = yourid; <-- incorrect
Whereas as per SQL syntax there should be single quotes
SELECT * FROM `ArtListing` where `id` = 'yourid'; <-- check single quotes around 'yourid'
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 am trying to insert into multi table after select query return 0 (not found raws) select query working and insert query never done when submite "displayid" and there is no any syntax error
code:
<?php
if ($_POST["displayid"] == TRUE) {
$sqlid = "SELECT * FROM doc1 WHERE idnum ='$pidnum' AND stats='$ok'";
$result = mysqli_query($conn, $sqlid);
if (mysqli_num_rows($result) > 0) {
$sqlup = "UPDATE doc1 SET m_phone='$pm_phone', seen='$dataseen' WHERE idnum ='$pidnum'";
mysqli_query($conn, $sqlup);
$found = 1;
} else {
$found = 0;
$sqlfail = "INSERT INTO fail(fname,lname,tname,funame,idnum,m_phone,reg_date)
VALUES ('$pfname','$plname','$ptname','$pfuname','$pidnum','$pm_phone','$todaydate')";
$conn->query($sqlfail)
}
}
?>
Use this code:
$sqlfail = "INSERT INTO fail(fname,lname,tname,funame,idnum,m_phone,reg_date)
VALUES ('".$pfname."','".$plname."','".$ptname."','".$pfuname."','".$pidnum."','".$pm_phone."','".$todaydate."')";
make similar changes for update command as well
you actually have one error
$conn->query($sqlfail)
should be
$conn->query($sqlfail);
AND stats='$ok'";
i can't see a variable with this name i think you mean AND stats='ok'";
This code below gives me this error:
"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 ''' where id = '000'' at line 1"
I don't understand the issue here
<?php
include(".conf.php");
$con = mysql_connect($conf['db_hostname'], $conf['db_username'], $conf['db_password']) or die (mysql_error());
$db = mysql_select_db("aTable", $con);
$pr = $_GET['aThing'];
$pr = addslashes(htmlentities($prof));
$info_array = mysql_query("SELECT * FROM '$db' where id = '$pr'", $con) or die(mysql_error());
while($row = mysql_fetch_array( $info_array )) {
echo $row['aThing'];
echo "</br>";
echo $row['aThing'];
echo "</br>";
echo $row['aThing'];
echo "</br>";
echo $row['aThing'];
};
?>
Thanks for your help.
You should put table name into FROM : SELECT * FROM aTable WHERE .....Also, you don't escape variable that comes from user.
You will need something like :
mysql_query("SELECT * FROM aTable where id = '".mysql_real_escape_string($pr)."'", $con) or die(mysql_error());
Function mysql_select_db returns either TRUE or FALSE
Instead, try:
$info_array = mysql_query("SELECT * FROM aTable where id = '$pr'", $con) or die(mysql_error());
Or perhaps:
$dbtable = "aTable";
$info_array = mysql_query("SELECT * FROM $dbtable where id = '$pr'", $con) or die(mysql_error());
I am pretty sure it doesn't have any errors with the exception of the
fatal error killing it.
I would say you'll get to a solution faster if you believe MySQL when it tells you there's a problem.
Re-reading the error message:
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 ''' where id = '000'' at line 1
I would question the table name and the quotes around the id. If that's an integer column, I'd expect to see a number without quotes.
If I remember correctly, mysql_select_db returns true or false. It doesn't return database name.
$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.