Error: Unknown column 'joinedactivities.searchact id = searchact.id' in 'on clause' - php

I keep getting this error Error: Unknown column 'joinedactivities.searchact id = searchact.id' in 'on clause'. I have checked table names and tried changing it but I keep getting errors.
I have tables:
joinedactivities- id, user id(foreign key), searchact id(foreign key)
searchact-id,postcode, lat, long, hobby, venue
I am trying to display rows from table searchact from the foreign key searchact id.
$user=$_SESSION['id'];
$sql ="SELECT * FROM `joinedactivities` JOIN `searchact` ON `joinedactivities.searchact id = searchact.id` WHERE `user id`=$user ";
$result = mysqli_query($conn, $sql)or die("Error: ".mysqli_error($conn));
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
// output data of each row
echo "Hobby : " . $row["searchact id"];
}
} else {
echo "You have not joined any groups";
}

Backticks enclose database objects. So you're telling the query engine that this entire thing is a single object (in this case a column):
`joinedactivities.searchact id = searchact.id`
I don't think you have a column named joinedactivities.searchact id = searchact.id, so the query is failing. (And even if you did have a column named that, it would still be an incomplete ON clause.) Enclose individual database objects in backticks:
`joinedactivities`.`searchact id` = `searchact`.`id`

Your backticks are incorrect:
`joinedactivities.searchact id = searchact.id`
^--------------------------------------------^
You've turned that entire string into a single identifier. You probably want something more like
`joinedactivities`.`searchact_id` = `searchact`.`id`
And note that NONE of those identifiers are reserved words, which means that the backticks are not necessary at all.

Your code is right but a slight mistake - Wrong backtracks. Use the code below
$user=$_SESSION['id'];
$sql ="SELECT * FROM `joinedactivities` JOIN `searchact` ON `joinedactivities`.`searchact id` = `searchact`.`id` WHERE `user id`=$user ";
$result = mysqli_query($conn, $sql)or die("Error: ".mysqli_error($conn));
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
// output data of each row
echo "Hobby : " . $row["searchact id"];
}
} else {
echo "You have not joined any groups";
}
Hope this helps you

Related

delete a data from database from multiple tables

<?php
//after creating connection
$dbname = 'bca2y';//database name
$sql = "SHOW TABLES FROM $dbname";
$result = mysqli_query($conn , $sql);
if (!$result) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
while ($row = mysqli_fetch_row($result)) {
$table = "{$row[0]}\n";
echo "$table<br>";
if($conn == TRUE){
echo "connection is possible<br>";
}
$sqld = "DELETE FROM $table";
$resultd = $conn->query($sqld);
if($resultd === TRUE){
echo "Data deleted succesfully ";//checking if data deleted
}
else {
echo "some error<br>";// for checking if code is not running
}
}
?>
Here I am trying to find name of table and delete data from the given table
But I think there is any syntax error in using variable as a table name.
my code has not giving any error but it still not working.
If I am understanding your intention -- to find table(s) and delete them from the database, "DELETE FROM mes" does not accomplish this goal. In SQL, "DELETE FROM" removes records from the specified database. It is more common to see DELETE FROM TableName WHERE ColumnName is 'SomeValue'; -- a deletion of some set of records. But DELETE FROM TableName is a valid SQL query -- it deletes all records from the table named TableName.
If you wish to delete all records from the table names retrieved from your first query, you would need to use a variable name in your delete statement rather than the static string mes.
If you want to remove the table (not just delete the data contained therein), use DROP TABLE.
If you want to DELETE all rows in a table, you should use TRUNCATE.
DELETE will delete row per row, which might take a lot of time because a lot of stuff is done by mysql for each delete operation. TRUNCATE will empty your table almost instantly
Here's your script adapted to achieve what you want
$dbname = 'bca2y';//database name
$sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA LIKE '".$dbname."'";
$result = mysqli_query($conn , $sql);
if (!$result) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
$rows = $result->fetch_all(MYSQLI_ASSOC);
foreach($rows as $table)
{
$sql = "TRUNCATE TABLE `".$table['TABLE_NAME']."`";
$resultd = $conn->query($sql);
if($resultd === TRUE){
echo $table['TABLE_NAME']. " truncated succesfully ";//checking if data deleted
}
else {
echo "some error<br>";// for checking if code is not running
}
}
Your user should have TRUNCATE permissions of course. And THINK before executing this, it is a dangerous script if used on the wrong database.

Remote MySQL : I Can Read Database Tables Names, But Can't Read The Rows

I just create a Remote MySQL access from my other database server (remote) and I can see tables in it :
// SHOW ALL TABLES NAME
$sql = "SHOW TABLES";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_row($result)) {
echo "Table: {$row[0]} <br>" ;
}
I can also see what columns name on each tables by using this :
// SHOW COLUMNS NAME OF A TABLE
$sql = 'DESCRIBE wp_ps_product_sku';
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_row($result)) {
echo "Column: {$row[0]} <br>" ;
}
but why I can't see any rows inside each table?
$sql = 'SELECT * wp_ps_product_sku';
$result = $conn->query($sql);
print_r($result);
it always gives me an empty result. for any tables. same result. it's always empty. what did I missed here? is it possible that server just give me an access to read the database structure only, but not the data?
thank you.
On your select statement, you are missing the 'from' clause.
select * from wp_ps_product_sku;

Getting 0 result from MySQL database , while my query and database name every thing is right

I have prefixed the custom and deafaults tables of wordpress.
Also I have put data in tables but I'm getting result 0.
Connection to database is ok and working
$sql = "SELECT u_name, u_email FROM jobify_user";
$result = $conn->query($sql);
if ($result->num_rows> 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
return "id: " . $row["u_name"]. "name:".$row["u_email"]. " <br>";
}
}
else {
return "0 results";
}
$conn->close();
Considering so the prefix is "jobify" and you're using wordpress tables, and according to https://codex.wordpress.org/Database_Description#Table:_wp_users, the table_name must be jobify_users, and columns must be user_name and user_email.
So, your query must surely be :
$sql = "SELECT user_name, user_email FROM jobify_users";

Associative array key value pair or Mysql alias name is more efficient

Associative array key name or Mysql alias name is more efficient
I have a mysql table called student having columns id, name and age
I need to get the result like
student_id student_name age
I have two options:
$sql = "SELECT id as student_id, name AS student_name, age FROM student";
$result = mysqli_query($link, $sql) or die(mysqli_error($link));
if (mysqli_num_rows($result) > 0) {
while ($studentRow = mysqli_fetch_assoc($result)) {
$studentInfo[] = $studentRow;
}
}
OR
$sql = "SELECT id, name, age FROM student";
$result = mysqli_query($link, $sql) or die(mysqli_error($link));
if (mysqli_num_rows($result) > 0) {
$i=0;
while ($studentRow = mysqli_fetch_assoc($result)) {
$studentInfo[$i]['student_id'] = $studentRow['id'];
$studentInfo[$i]['student_name'] = $studentRow['name'];
$studentInfo[$i]['age'] = $studentRow['age'];
$i++;
}
}
Note: Don't put solutions to alter the table column names.
I think the first is more efficient and more appropriate. Because in second option, you need restore value from one array to another array that can take little bit more time.
So according to me, first option will be more appropriate.
Mysql alias name is more efficient, because may be in future you will also have Class table with id field in it. You can resolve this conflict by using alias like AS class_id.

Multiple SELECT Statements and INSERTS in 1 file

I'm working with a file and I'm attempting to do multiple select statements one after another and insert some values. So far the insert and the select I've got working together but when attempting to get the last SELECT to work I get no value. Checking the SQL query in workbench and everything works fine. Here's the code:
$id = "SELECT idaccount FROM `animator`.`account` WHERE email = '$Email'";
$result = mysqli_query($dbc, $id) or die("Error: ".mysqli_error($dbc));
while($row = mysqli_fetch_array($result))
{
echo $row[0];
$insert_into_user = "INSERT INTO `animator`.`user` (idaccount) VALUES ('$row[0]')";
}
$select_userid = "SELECT iduser FROM `animator`.`user` WHERE iduser = '$row[0]'";
$results = mysqli_query($dbc, $select_userid) or die("Error: ".mysqli_error($dbc));
while($rows = mysqli_fetch_array($results))
{
echo $rows[0];
}
I do not want to use $mysqli->multi_query because of previous problems I ran into. Any suggestions? And yes I know the naming conventions are close naming... They will be changed shortly.
Your code makes no sense. You repeatedly build/re-build the $insert_int-User query, and then NEVER actually execute the query. The $select_userid query will use only the LAST retrieved $row[0] value from the first query. Since that last "row" will be a boolean FALSE to signify that no more data is available $row[0] will actually be trying to de-reference that boolean FALSE as an array.
Since you're effectively only doing 2 select queries (or at least trying to), why not re-write as a single two-value joined query?
SELECT iduser, idaccount
FROM account
LEFT JOIN user ON user.iduser=account.idaccount
WHERE email='$Email';
I'm not sure what you're trying to do in your code exactly but that a look at this...
// create select statement to get all accounts where email=$Email from animator.account
$id_query = "SELECT idaccount FROM animator.account WHERE email = '$Email'";
echo $id_query."\n";
// run select statement for email=$mail
$select_results = mysqli_query($dbc, $id_query) or die("Error: ".mysqli_error($dbc));
// if we got some rows back from the database...
if ($select_results!==false)
{
$row_count = 0;
// loop through all results
while($row = mysqli_fetch_array($result))
{
$idaccount = $row[0];
echo "\n\n-- Row #$row_count --------------------------------------------\n";
echo $idaccount."\n";
// create insert statement for this idaccount
$insert_into_user = "INSERT INTO animator.user (idaccount) VALUES ('$idaccount')";
echo $insert_into_user."\n";
// run insert statement for this idaccount
$insert_results = mysqli_query($dbc, $insert_into_user) or die("Error: ".mysqli_error($dbc));
// if our insert statement worked...
if ($insert_results!==false)
{
// Returns the auto generated id used in the last query
$last_inisert_id = mysqli_insert_id($dbc);
echo $last_inisert_id."\n";
}
else
{
echo "insert statement did not work.\n";
}
$row_count++;
}
}
// we didn't get any rows back from the DB for email=$Email
else
{
echo "select query returned no results...? \n";
}

Categories