M SELECT INTO query isn't working - php

There is a minor problem in my query and I can't seem to figure it out. I am using select into statement to copy one table's column to another. The browser is showing error the following error:
undeclared variable:new_tbl
When I open phpmyadmin, there is no new_tbl added to the particular database. Please tell me what's wrong with it. And the new-tbl does not already exists.
$sqli = "SELECT * INTO `new_tbl` FROM `order`";
$query = mysqli_query($con,$sqli);
if($query) {
echo "SELECT INTO query worked...";
} else {
echo "ERROR:".mysqli_error($con);
}

MySQL does not support select into. Use create table as:
create table new_tbl as
SELECT * FROM `order`;
By the way order is a really bad name for a table because it is a SQL keyword.

Related

how to search all the tables of the database using php mysqli and display the answer

i am just beginning to learn mysqli and php ...so i am a novice.
i am trying to build a site for babynames.
i have a database of babynames in phpmyadmin. having tables country1, country2, country3....and so on. all the tables have the same columns id, name, meaning, gender and alphabet.
i print the names table using the following code using PHP and MYSQLI
$sql1="SELECT id,name, meaning, alpha, gender FROM $country WHERE gender='$gender' AND alpha='$alpha' $limit";
$query=mysqli_query($con,$sql1);
while($rows=mysqli_fetch_array($query)){
echo "<td><a href='meaning-of.php?name=$rows[name]'>".$rows['name']."</a></td>";
echo "<td>".$rows['meaning']."</td>";
echo "<td>".$rows['gender']."</td></tr>";
}
the name column is linked to meaning-of.php?name=$rows[name]
Now i want a code in PHP and MYSQLI to search all the tables(say 100 tables) of the database "babynames" for a particular name and display the same name and meaning please.
if the name exists more than once in the tables than all the names should be displayed along with their respective meanings please.
how do i search all the tables for a particular name and display using PHP and MYSQLI please.
i want to search the database by passing only the name variable into the address bar like this http://localhost/meaning-of.php?name=John or http://localhost/meaning-of.php?name=samson and find the meaning by searching all the tables of the database based on only the name please.like if the name is set than getting the name by $name=$_GET["name"]; and then search the whole database tables and display the meaning
is there a code something like this
SELECT *(all columns) FROM * (all the tables or the database name or all the databases) WHERE name=$name;
Please help. Thank You in advance please.
Keep all your data in a single table, adding a field country to distinguish the country.
This is the only proper answer to this question.
You can use UNION clause to get results from multiple select statements mysql doc
(SELECT * FROM county1 WHERE name=$name)
UNION
(SELECT * FROM county2 WHERE name=$name);
You can query all table names from information_schema
SELECT table_name FROM information_schema.tables
WHERE table_name LIKE "country%"
AND table_schema = "<name_of_country_database>";
So the full solution would be something like this:
$tables_sql = "SELECT table_name FROM information_schema.tables WHERE table_name LIKE 'country%' AND table_schema = 'babynames'";
$tables_query = mysqli_query($con,$tables_sql);
$sql1 = '';
while ($table = mysqli_fetch_array($tables_query)) {
if ($sql1 != '') {
$sql1 .= ' UNION ';
}
$sql1.= " (SELECT name, meaning FROM " . $table['table_name'] . " WHERE name='$name') ";
}
$query=mysqli_query($con,$sql1);
while($rows=mysqli_fetch_array($query)){
echo "<tr>";
echo "<td>".$rows['name']."</td>";
echo "<td>".$rows['meaning']."</td>";
echo "</tr>";
}

SQL select statement with blank in passing parameter

I'm trying to write a search function and am using multiple drop-down lists for search criteria.
i have a sql statement like
SELECT * FROM TABLE WHERE OFFICE='$office', NAME='$name', DEPARTMENT='$department';
Sometime I want to search with specific 'name' but without talking about 'department' and 'office'. But when I pass Blank '' to '$office' and '$department' it only return the person with no office and department. Is there anyway around to overcome it?
I tried to use '%' instead of blank but it didn't work as well.
I'm coding with php and MSSQL.
Thanks in Advance
If you want to work with wildcards, you dont need =, but LIKE. Unsure if this query works, but try it:
SELECT * FROM TABLE WHERE OFFICE LIKE '$office', NAME LIKE '$name', DEPARTMENT LIKE '$department';
Now you just have to check if the field is blank, if yes, replace it with a %. As i said, im unsure. I dont have a database availible at the moment for testing this.
for achieving this you have to write some php code like
$sql = "SELECT * FROM TABLE WHERE";
if(isset($office)){
$sql .= "OFFICE='$office',";
}
if(isset($name)){
$sql .= " NAME='$name',";
}
if(isset($department)){
$sql .= " DEPARTMENT='$department'";
}
You can easily do this as follow:
if(isset($office) && isset($department)){
$sql = "SELECT * FROM TABLE WHERE OFFICE='$office', NAME='$name', DEPARTMENT='$department'";
}
else{
$sql = "SELECT * FROM TABLE WHERE NAME LIKE '$name'";
}
mysql_query($connection, $sql);

I am getting following error while running my code

While I am running this code it showing me a following error:-
Column 'virtuemart_product_id' in where clause is ambiguous
Can anyone tell me how to resolve it.
Thank you.
<?php
mysql_connect('localhost','root','');
mysql_select_db('joom');
$get=$_GET['virtuemart_product_id'];
$get1=$_GET['virtuemart_category_id'];
$sql="SELECT
btn9c_virtuemart_products_en_gb.virtuemart_product_id,
btn9c_virtuemart_products_en_gb.product_s_desc,
btn9c_virtuemart_products_en_gb.product_desc,
btn9c_virtuemart_products_en_gb.product_name,
btn9c_virtuemart_product_prices.product_price
FROM btn9c_virtuemart_products_en_gb
JOIN btn9c_virtuemart_product_prices ON
btn9c_virtuemart_products_en_gb.virtuemart_product_id=btn9c_virtuemart_product_prices.virtuemart_product_id
WHERE virtuemart_product_id='$get' ";
if($result=mysql_query($sql))
{
while($row=mysql_fetch_assoc($result))
{
echo $row['product_price'];
}
}
else
{
echo (mysql_error());
}
?>
Put table name (or alias) with column names in where clause which are common between tables you are joining.
Try below:
$sql="SELECT
btn9c_virtuemart_products_en_gb.virtuemart_product_id,
btn9c_virtuemart_products_en_gb.product_s_desc,
btn9c_virtuemart_products_en_gb.product_desc,
btn9c_virtuemart_products_en_gb.product_name,
btn9c_virtuemart_product_prices.product_price
FROM btn9c_virtuemart_products_en_gb
JOIN btn9c_virtuemart_product_prices ON
btn9c_virtuemart_products_en_gb.virtuemart_product_id=btn9c_virtuemart_product_prices.virtuemart_product_id
WHERE btn9c_virtuemart_products_en_gb.virtuemart_product_id='$get' ";
Just add the table name to the WHERE part of the query:
..."WHERE table_you_need.virtuemart_product_id='$get' ";
In your where clause the property virtuemart_product_id is not identified unambiguously by a table name. Just use the table prefix there as well.
SELECT
prod.virtuemart_product_id,
prod.product_s_desc,
prod.product_desc,
prod.product_name,
prices.product_price
FROM btn9c_virtuemart_products_en_gb prod
JOIN btn9c_virtuemart_product_prices prices ON
prod.virtuemart_product_id=prices.virtuemart_product_id
WHERE prod.virtuemart_product_id='$get'
Btw use aliases for the tablenames. Saves a lot of typing and makes things more readable.

Select * distinct one column

I have mysql table tmp with columns pid,city,state,country. I write queries so i can find matching city,state or country, and pid is field that helps me load another table.
The thing is, there is always two rows with same pid, and sometimes (when WHERE find matching city state or country in both), i display data from additional table twice unnecessarily.
So i need to select something like:
SELECT * FROM tmp DISTINCT pid WHERE city='test'
I have no idea how to search solution (i searched here on stackoverflow, but no luck).
Also, there will be a lot of searching in this table, so if there is multiple solutions i would prefer one that is faster.
Thanks
Please try the following SQL statement:
SELECT DISTINCT pid FROM tmp WHERE city='test'
try this
SELECT DISTINCT pid,field1,field2 FROM tmp WHERE city='test'
$keyword="0";
$query = "SELECT DISTINCT titel,id FROM xyz ORDER BY titel ASC ";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result))
{
if($keyword!==$row["titel"])
{
$keyword=$row["titel"];
echo $keyword;
}
}

Mysql query for using count on a view in php

I have a query:
$result = mysql_query("CREATE VIEW temporary(IngList) AS (
SELECT DISTINCT (r1.Ingredient)
FROM recipes r1,
recipes r2
WHERE r1.Country = '$temp'
AND r2.Country = '$temp2'
AND r1.Ingredient = r2.Ingredient)
SELECT COUNT(*) FROM temporary");
I want the query to make a view called temporary and have it return a count of the number of rows in the view temporary. I know this code works without the SELECT COUNT(*) because I checked my database and the view is created.
Yet this code throws the 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 'SELECT COUNT(*) FROM temporary' at line 1
I checked the syntax and it seems to be correct. What seems to be the problem because its quite frustrating.
From the mysql_query documentation:
mysql_query() sends a unique query (multiple queries are not supported)...
You can't create the view, and select from it in a single mysql_query. The view is unnecessary:
$sql = sprintf("SELECT COUNT(DISTINCT r1.Ingredient)
FROM recipes r1
WHERE r.country = '%s'
AND EXISTS(SELECT NULL
FROM recipes r2
WHERE r2.Country = '%s'
AND r1.Ingredient = r2.Ingredient)",
$temp, $temp2);
$result = mysql_query($sql);
For starters you have two statements. What you're writing looks more like a stored procedure. Even if it worked, you would need a semicolon at the end of the first statement. And another statement somewhere saying "DROP VIEW ...." when you are done.
And a temp view is a bit of a non sequitur. I can't find any reference to "CREATE VIEW temporary". Or maybe it's to create a view named temporary with an argument? Views don't take arguments.
I think you might get what you want with a semi-simple SQL statement something like:
$result = mysql_query(
"SELECT COUNT(DISTINCT r1.Ingredient)
FROM recipes r1
JOIN recipes r2 ON r1.Ingredient = r2.Ingredient
WHERE r1.Country = '$temp'
AND r2.Country = '$temp2'");

Categories