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'].
Related
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();
The query SELECT * FROM TABLE WHERE id LIKE '%1% is not working properly, it's not select the id 1.
mysql_connect('localhost', 'root' , '');
mysql_select_db('database');
$sql = ("select * from search WHERE id LIKE '%3%'");
mysql_query($sql);
$my_variable = mysql_query($sql);
$display_data = mysql_fetch_row($my_variable);
while ($list = mysql_fetch_assoc($my_variable)) {
$id = $list['id'];
$title = $list['title'];
$keywords = $list['keywords'];
$img = $list['img'];
$link = $list['link'];
}
If you are looking to SELECT id 1 then use = not LIKE. The way LIKE is being used it will match every id that has a 1 in it and you are not guaranteed to get the first one in order, so instead use:
SELECT * FROM search WHERE id = 1
According to the PHP documentation of mysql_fetch_row it
Returns a numerical array that corresponds to the fetched row and moves the internal data pointer ahead.
Which means that the first result won't show up in the next (mysql_fetch_assoc) procedure. You could try removing the $display_data = mysql_fetch_row($my_variable); line and only use the while($list = mysql_fetch_assoc($my_variable)) { ... } procedure. See if that solves your problem.
$sql = ("select * from search WHERE id ='3'");
The id is an integer use = instead of like . Equal is more accurate.
And first echo your query in your program->
echo $sql;die;
copy that query and run it on your phpmyadmin
and then check is your column id is int type if it is then like will not give you the result. You have to use the where clause here .But if you have the column id is of type varchar then definitely give you the result .
Try to use search tab under your database->table in your phpmyadmin and put the condition there.
You will definitely get your answer there.
Hello I have 2 textboxes and i want to give to the user the option to choose one in order to find results. The user can search through the id or the name. My problem is because i use LIKE%field% when the user chooses to search through the id the name field stays empty and returns all the table rows. I want to have results only if the user enters some value in the textbox. This is my sql query. I'm using mysql
"SELECT * FROM properties WHERE ID='$id' OR Name LIKE '%$name%'"
Thank you all
If the user has to select which field to search, you can do:
if ($_POST['search'] == 'id') {
$sql = "SELECT * FROM properties WHERE ID='$id'"
} else {
$sql = "SELECT * FROM properties WHERE Name LIKE '%$name%'"
}
You can do this in a single query (values are checked from the query itself):
"SELECT * FROM properties WHERE ('$id'='' OR ID='$id') AND ('$name' ='' OR Name LIKE '%$name%')"
Explanation:
First condition:
The query will select records with ID='$id' only when $id is not empty.
If $id is empty, query will not go for the second part ID='$id'
Second condition:
The query filters records with Name LIKE '%$name%' only when $name is not empty.
If $name is empty, query will not go for Name LIKE '%$name%'.
NB: This technique is extremely useful when you have numerous parameters to check, rather than using a bunch of if...elses at php side.
I have a database table that contains a bunch of different options and their values. The table has 3 columns which are ID, menu_option, and value. So one row may have the following information: 1, "menu_color", "#AB324B".
I want to display the value of each option on the page so the user can edit the option. Right now, i'm creating a query to get the info for each specific option. Like so
SELECT * FROM menu_theme WHERE ID='1'
SELECT * FROM menu_theme WHERE ID='2'
SELECT * FROM menu_theme WHERE ID='3'
...
Instead of making a new query to get the info per row, how can I make 1 query and distinguish what row I want to get the data from and display the data using php?
I'm aware of how to use php while loops with an SQL query, but I can't see how that would work with selecting specific rows.
Maybe something like this
SELECT * FROM menu_theme WHERE ID IN ('1','2','3')
maybe something like this:
<?php
//select all the rows
$sqlSelect="SELECT ID,menu_option,value FROM menu_theme";
$result=mysqli_query($con,$sqlSelect);
while($row=mysqli_fetch_array($result))
{
$id=$row['ID'];
$opt=$row['menu_option'];
$val=$row['value'];
$menuId="input-".$id;
//create the label for the input
echo "<label for='".$menuId."'>".$opt."</label>";
//pre-populate the input with the name,value,id
echo "<input type='text' name='".$menuId."' id='".$menuId."' value='".$val."'/>";
}
?>
We have a problem with our search suggestions. Everytime we click on a suggestion at our website, it puts a space in front of the search query, which causes the query to fail.
The code that we use for the suggestions is this:
$query = $db->query("SELECT DISTINCT productnaam FROM product WHERE merk LIKE '$queryString%' LIMIT 10");
if($query) {
// While there are results loop through them - fetching an Object (i like PHP5 btw!).
while ($result = $query ->fetch_object()) {
// Format the results, im using <li> for the list, you can change it.
// The onClick function fills the textbox with the result.
// YOU MUST CHANGE: $result->value to $result->your_colum
echo '<li onClick="fill(\''.$result->merk.' '.$result->productnaam.'\');">'
.$result->merk.' '.$result->productnaam.''.'</li>';
}
} else {
echo 'ERROR: There was a problem with the query.';
Try out with trim()
$queryString = trim($queryString);
The trim() function removes whitespaces and other predefined characters from both sides of a string.
try the trim() function as Sameera Thilakasiri specified below and also update your query to something like "SELECT DISTINCT productnaam FROM product WHERE merk LIKE '%$queryString%' LIMIT 10" The percent sign on both sides will ensure that your query will select records that contain your input as opposed to records that start with your input.
bellow is some further explanation on the SQL LIKE condition that might help you out
// This query will look for records that start with "sa"
select * from table where name like 'sa%'
// This query will look for records that contain "sa"
select * from table where name like '%sa%'
// This query will look for records that end with "sa"
select * from table where name like '%sa'
hope that helps!