I want to transfer data from a MySQL database, select the column related to the language of values from my goal.
MySQL table's name is catalogs
Mysql column names are id, name_en, name_de, name_ru, content_en, content_de, content_ru, image
language short code comes from _GET request
For example, the URL looks like the following
catalogs.php?id=1&lan=en
My PHP Code Looks like the following
$get_id = $_GET['id'];
$lan = $_GET['lan'];
$sql = $mysqli -> query("SELECT id, name_$lan, content_$lan, image FROM catalogs WHERE id='$get_id'");
while($row = $sql->fetch_array()){
echo $row['name'];
//nothing display
}
Related
I have several tables that are being imported by an external app. Their names are e.g Table_1, Table_296 and so on.
In each table, there is one column named Name.
My question is: How do I retrieve the names of all tables so I can put them into another query that will retrieve rows from each column Name?
Basically, I need to check for value and list a table (or tables) that contains it.
I can retrieve all tables names using the code below, but as far as I know, they can't all be SELECTED using a variable.
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.tables WHERE TABLE_NAME LIKE '%Table%' AND TABLE_SCHEMA='mydb'
I also know that the same column name cannot be selected if table names are not specified. Correct me if I'm wrong, please
First get all the table name then loop like below
$con = mysqli_connect("localhost","root","","databaseName");
$listdbtables = array_column(mysqli_fetch_all($con->query('SHOW TABLES FROM databaseName')),0);
foreach ($listdbtables as $key => $value) {
$sqlqry = "select Name from ".$value;
$nameResult = mysqli_query($con,$sqlqry);
while($nam = $nameResult->fetch_array()) {
echo($nam['Name']);
}
}
I want to print the values of custom columns for all rows, I've got a sql developer file, i mean that i want to print some columns of every rows, if for example my table is named client and has name, subname and email i want to print every client name, subname and email I've found how to make it with mysql but I want to make it with php and sql developer.
The code I've shown only shows the first user name and not the rest names and other columns of other users.
<?php
function clientes($conexion) {
$consulta = "SELECT NOMBRE FROM CLIENTE";
$stmt = $conexion -> prepare($consulta);
$stmt -> execute();
return $stmt -> fetchColumn();
}
echo clientes($conexion);
?>
You're only getting one column since you're explicitly only ask for one column (by using fetchColumn()) and the fact that you've only told SQL to return one single column.
Change your query:
// Replace column1 etc to the real column names
SELECT column1, column2, column3 FROM CLIENTE
Then instead of using fetchColumn() (which is for fetching one column), use:
return $stmt->fetchAll(PDO::FETCH_ASSOC);
and you should get an array containing associative arrays (one per row) with all three columns.
However, you have another issue:
echo clientes($conexion);
Since you're now returning an array, you can't just echo it straight off. If you do, you will get a warning about "Array to string conversion". You need to iterate through the array and output the data as you want it:
$data = clientes($conexion);
// Again, change the column names
foreach ($data as $row) {
echo $row['column1'];
echo $row['column2'];
echo $row['column3'];
}
This will output all the data. You get to decide on the layout though.
I want to print every client name, subname and email i've found
Then you should use fetchall() this will fetch all the results as an array then iterate through the array and print each element of the array
function clientes($conexion) {
$stmt = $conexion ->query("SELECT name,subname,email FROM CLIENTE")->fetchall();
foreach($stmt as $row){
echo "Client Name : ".$row['name']."<br>";
echo "Client subname : ".$row['subname']."<br>";
echo "Client email : ".$row['email']."<br>";
}
}
clientes($conexion);
?>
For getting all columns
You need to address your SQL Query different for it to work probably. At the moment you are only looking for the field nombre. Try to change it to:
$consulta = "SELECT * FROM CLIENTE";
With the asterisk you calling all fields in the table cliente. For performance you should only call the fields your are needing. This would look like this:
$consulta = "SELECT nombre,dni,sexo FROM CLIENTE";
For getting all rows
At the moment you just address one row here. You need to change your code to
return $stmt ->fetchAll();
Hello my data base have various columns , in a mysql fetch I am listing only the title column, but I would like that list to make a link on the already listd result which link will open a new page with the same result but this time listing all the columns from the table on this row.
mt SQL fethc is:
(!$result = mysqli_query($con,"SELECT * FROM tablename WHERE type = 'Panes'"))
and my sql result is: <?php echo $row['name']; ?> -> which result I would like to be link.
I wold like to know if somebody have any suggestion for a possible resolution. Thanks!
You cannot do that if you want to have a single query in your code(base from your query).
You must do another query for the click item or link..
If you do not need all the columns in your table to not use * instead select only what you need to make your code more readable and to be faster to execute.
Revised your string query,
"SELECT id, name FROM tablename WHERE type = 'Panes'"
and in your code to display the result to make a link,
<?php echo $row['name']; ?>
And in your page displayAllColumns.php you must get the passed data through get method
$id = $_POST['id'];
//then make a query for that to select all the data on that id
//just have to type only the string so
"SELECT * FROM tablename WHERE id = '$id'"
//you must use `*` because you need to display all the columns then write your table code to display the result.
Hope it helps
Try echoing an anchor tag. $row['name']
Use something like this:
<?php echo $row['name']; ?>
The display_item.php script then displays the row with the ID in $_GET['id'].
I'm building a simple bug tracking tool.
When you create a new project, all the info you fill in in the form, gets stored in the database.
When you create the new project you get redirected to a unique project page.
On top of the page it shows the name of the project, but it's not the name of the project I just created, it always shows the name of the first project in the MySQL table.
How can I show the name of the project I just created?
With this query I retrieve the data from the database.
$query = "SELECT CONCAT(name)
AS name FROM projects";
$result = #mysql_query ($query)
With this I show the project name, but it always shows the name of the first record in the table.
<?php
if ($row = mysql_fetch_array ($result))
echo '<h5>' . $row['name'] . '</h5>';
?>
It isn't yet SQL Injection prove and is far from complete... But I'm really struggling with this problem.
You need an AUTO_INCREMENT field on your table for a unique identifier (at least, you really should). Then you can do something like this:
<?php
$sql = new MySQLi('localhost', 'root', '', 'database');
$sql->query('INSERT INTO `projects` (`name`) VALUES ("Test Project");');
$projectID = $sql->insert_id; // Returns the auto_increment field value of the last insert query performed
// So this assumes you have a field in your table called "id" in this example
$res = $sql->query('SELECT CONCAT(`name`) AS `name` FROM `projects` WHERE `id` = '.$projectID.';');
if ($row = $res->fetch_assoc()) {
echo '<h5>'.$row['name'].'</h5>';
}
?>
Since you were calling for a redirect to the unique project page, you should have something like this: header("Location: project.php?id=$projectID");
Then, on project.php, you can attempt to fetch the project with the query above, only your query's WHERE clause should be something like:
'`id` = '.intval($_GET['id']).';'
Technically, you could pass all the project info along to the next page as a request or a session cookie and save yourself a query altogether. Just make sure you keep the id handy so it's easy to update the record.
Try using ORDER BY.
$query = "SELECT CONCAT(name)
AS name FROM projects ORDER BY id DESC";
This would show the most recent project (assuming you have an ID column).
However, a much better way is to have an ID variable on the page.
$query = "SELECT CONCAT(name)
AS name FROM projects WHERE id=?";
I am having a Problem with a MySQL Query. My Database Structure looks like this:
id (PRIMARY_KEY, AUTO_INCREMENT)
deviceID (TEXT)
name (TEXT)
latidude (TEXT)
longitude (TEXT)
Now this is how the first Entry looks like (in the order like above):
1 fc29daf8-bc53-4235-a1df-7d54b4e67b4c username 46.993393 5.448076
I'm search the Database with this Query:
$result = mysql_query("SELECT name FROM position WHERE deviceID = '$deviceID'");
But it doesn't find the entry. The deviceID is obtained from $_GET and it is exactly the same as this in the database. I also checked if it is right by printing it with echo.
It is sent to the PHP file like this:
read_uniqueid.php?deviceID=fc29daf8-bc53-4235-a1df-7d54b4e67b4c
and read from the .php file like this:
$deviceID = $_GET['deviceID'];
echo $deviceID.'<br>';
After those lines the query from above is sent.
There is a working connection to the database.I only have a Problem in the query if I search the name like that it works then I get all the other Entries.
This query works and gives me all entries:
$result = mysql_query("SELECT * FROM position WHERE name = '$name'");
Try like,
$result = mysql_query("SELECT name FROM position WHERE deviceID like '%$deviceID%'");
the column deviceID may have space in before or after the value.