I have successfully got my php code to connect to my database and retreieve data and display it. I am now trying to take this data and display it in a dropdown menu, however when I do this the dropdown menu displays the correct number of options that correlates with the data in the database(ie. it gives three options if there is 3 values in the db). But it doesn't display the text all the options are blank. Any ideas as to why it is not displaying the text?
<?php
$dbhost= 'Host IP';
$dbuser ='My Username';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
$sql='SELECT event_id FROM events';
mysql_select_db('db_name');
$retval= mysql_query($sql, $conn);
echo'<select name=dropdown value=', '>Dropdown</option>';
while($r= mysql_fetch_array($retval))
{
echo "<option value={$r["event_id"]}>{$r["events"]}</option>";
}
echo "</select>";
?>
http://i.imgur.com/30Uq0Ok.png
The link is what the menu currently returns
Thanks
You did not select events from database. You need to select all(*) or required columns by its name in the query .
Change your query:
$sql='SELECT event_id FROM events';
to :
$sql='SELECT * FROM events';
You also need to take option value inside quotes.
Change this line inside loop:
echo "<option value={$r["event_id"]}>{$r["events"]}</option>";
to:
echo "<option value=\"{$r['event_id']}\">{$r['events']}</option>";
try it like this (change your query to select *)
echo '<option value='.$r["event_id"].'>'.$r["events"].'</option>';
You are just selecting event_id from the table. So, $r["events"] is always empty.
Select the title field, too.
<?php
$dbhost= 'Host IP';
$dbuser ='My Username';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
$sql='SELECT event_id,event_title FROM events';
mysql_select_db('db_name');
$retval= mysql_query($sql, $conn);
echo'<select name=dropdown value=', '>Dropdown</option>';
while($r= mysql_fetch_array($retval))
{
echo "<option value={$r["event_id"]}>{$r["event_title"]}</option>";
}
echo "</select>";
?>
I assumed, that the field name is "event_title".
Related
I am trying to filter out data from my table using a searchbox in HTML. My search box which should return value from SQL query.
But even if I search, the filtered table is not displayed.
I have checked the 'LIKE' query in phpMyAdmin with '%n' (which I meant an entry in my table ending with 'n' ) and it works, but since in mine I am searching for a specific text that is entered in the search box, I couldn't check for the query that I am using.
Would really appreciate any help and thanks in advance.
<?php
//error_reporting(E_ERROR | E_PARSE);
$db_host = 'localhost';
$db_user = 'zamil'; // Username
$db_pass = '1234'; // Password
$db_name = 'resi'; // Database Name
$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (!$conn) {
die ('Failed to connect to MySQL: ' . mysqli_connect_error());
}
else{
print("connected");
}
$output = '';
$query = '';
if (isset($_GET['search'])){
$searchq = $_GET['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$query = mysqli_query( $conn, "SELECT * FROM 'salesflow' WHERE 'Rep Name'
LIKE '%$searchq%'") or die("could not search!");
$count = mysqli_num_rows($query);
if($count == 0){
$output = 'There was no entries';
}else{
while ($row = mysqli_fetch_array($query)) {
$cname = $row['Source of Content'];
$rname = $row['Rep Name'];
$output .= '<div>'.cname.' '.rname.'</div>';
}
}
}
if ($query != 0) {
die ('SQL Error: ' . mysqli_error($conn));
}
?>
<form action="Sales1.php" method="post">
Search: <input type="text" name="search" />
<input type="submit" value="Search" /><br />
</form>
<?php print("$output"); ?>
</body>
</html>
Your SQL query is incorrect:
SELECT * FROM 'salesflow' WHERE 'Rep Name' LIKE '%$searchq%'
You put single quotes (') around the salesflow table name and Rep Name column name but you should use backticks (`) instead.
For more information see Using backticks around field names.
I think you have entered a wrong query syntax.
$query = mysqli_query( $conn, "SELECT * FROM 'salesflow' WHERE
'Rep Name' LIKE '%$searchq%'") or die("could not search!");
It should be written like this:
$query = mysqli_query( $conn, "SELECT * FROM 'salesflow' WHERE
'Rep Name' LIKE '%".$searchq."%'") or die("could not search!");
Always use ' for string in query.
Also, my additional recommendation is not to use SQL for searching in the table, instead, use DataTable based on AngularJS.
I have a database table in which there are 2 columns. The first is the ID, the second is the price. When the user selects a product on the site, a product card is opened (this will be id). At the bottom there is a calculator with prices where you need to load the price from the second column. Question: how to make a request for a specific ID(for loading price from second column). Here's the code that I could do.This code displays all IDs and prices, but need only when you select the product and only for it. Any help. Thank you
<?php
ini_set('display_errors','On');
error_reporting('E_ALL');//error's show
$host = 'localhost'; // host name
$database = 'test_sql'; // database name
$user = 'root'; // user name
$pswd = ''; // password
$dbh = mysql_connect($host, $user, $pswd) or die("Could not to connect MySQL.");//connect to mySQL
mysql_select_db($database) or die("Could not to connect database.");
$query = "SELECT * FROM `oc_product`";//load from product table
$res = mysql_query($query);
$row = mysql_fetch_array($res);//array call
while($row = mysql_fetch_array($res)){
echo "ID: ".$row['product_id']."<br>";//output for each ID
echo "Цена: ".$row['price']."<br>";//output for each price
}
?>
when you select a product pass that id to the function and update your query like :
$dbh = mysql_connect($host, $user, $pswd) or die("Could not to connect MySQL.");//connect to mySQL
mysql_select_db($database) or die("Could not to connect database.");
$query = "SELECT * FROM `oc_product` WHERE 'product_id' = <id that you passed>";//load from product table
$res = mysql_query($query);
$row = mysql_fetch_array($res);
I am trying to create a dropdown menu which has values from the database and when selected it should show all the information that are connected to the selected value.
In MySQL I have three tables, person, address and resume. I created two drop-down menu's(see code below) which are showing the values from address > address_state and address > address_city. What needs to happen is when I selected a state it will show only the persons living in the selected state. And when selecting city it has to show all the persons living in the same city.
My drop-down menu select state only:(got the same code city)
$servername = "localhost";
$username = "root";
$password = "usbw";
$dbname = "persons";
// CREATE A CONNECTION WITH THE DATABASE
// CONNECTIE MAKEN MET DATABASE
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//$sql="SELECT name,id FROM student";
$sql="SELECT DISTINCT address_state FROM address ORDER BY address_state asc";
/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */
echo "<select name=address value=''>Student Name</option>"; // list box select command
foreach ($conn->query($sql) as $row){//Array or records stored in $row
echo "<option value=>$row[address_state]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box
?>
I know you cannot provide any code because I don't have any code to show which actually "selects" the persons. But I have searched on Google for tutorials and I did not find one which did the thing I needed. So I hope you guys can help me or can provide any tutorials.
Replace this line:
echo "<option value=>$row[address_state]</option>";
With
echo "<option value='".$row['address_state']."'>".$row['address_state']."</option>";
First replace:
echo "<select name=address value=''>Student Name</option>";
To:
echo '<select name="address">';
Next replace this:
echo "<option value=>$row[address_state]</option>";
With:
echo '<option value="'.$row[address_state].'">'.$row[address_state].'</option>';
I have a table with the following:
6xx 8xx 9xx 11xx 12xx
1 0.01 0.002 0.004 0.001 0.025
2 0.025 0.125 0.002 0.01 0.011
I would like to find the Smallest Value from the column make that column to be green color.
For example in 1st the smallest value is 0.001 so i want it to be green color, for second 0.002 is smallest value i want it to be green color.
can any one guide me how to make this ,thanks
below is the code how i selecting it from database and displaying int in a table
<?php
$dbHost = 'localhost'; // usually localhost
$dbUsername = 'xxxx';
$dbPassword = 'xxxx';
$dbDatabase = 'xxxx';
$db = mysql_connect($dbHost, $dbUsername, $dbPassword) or die ("Unable to connect to Database Server.");
mysql_select_db ($dbDatabase, $db) or die ("Could not select database.");
$ColumnNames = mysql_query("SELECT column_name
FROM information_schema.COLUMNS
WHERE table_name = 'supplierprice'
AND column_name NOT IN ('supp_price_id',
'region',
'country',
'net_id',
'networkname',
'mcc',
'mnc',
'mnp')")
or die("mysql error");
$columnArray=array();
$i=0;
while($rows=mysql_fetch_array($ColumnNames))
{
$columnArray[]=$rows[0];
echo "<th style='width:67px;' class='. $columnArray[$i] .' >" . $columnArray[$i] . "
</th>";
$i++;
}
?>
foreach($columnArray as $value) {
//$columnArray[]=$rows1[0];
echo '<td style="width:67px;font-weight:'.$text.'" id="CPH_GridView1_xxx" width="0px;" class="'.$value.' '.$rows["net_id"].'"><p>'.$rows[$value].'</p></td>';
}
one answer for your problem may be this:
1- extract minimum of each column by sql query like this:
$res1=mysql_query('select min(6xx) as min6, min(8xx)as min8, min(9xx) as min9, min(11xx)as min11, min(12xx) as min12 from tbl_name');
$rec1=mysql_fetch_array($res1);
$min6=rec1['min6'];
$min8=rec1['min8'];
$min9=rec1['min9'];
....
2- when you fetch information in html you should check if the value is like min then background (some css) become green:
$res=mysql_query('select * from tbl_name');
echo "<table>";
foreach($rec=mysql_fetch_array($res))
{
echo "<tr>"
echo "<td";
if($rec['6xx']==$min6) echo "class='green_cell' ";
echo "";
echo $rec['6xx'];
echo "</td>";
....
echo "</tr>"
Not entirely sure on your requirements. You appear to be getting some column names for a table. I presume that you want to get the values of these columns and display them in a table, highlighting the one with the lowest value.
If some something like this. Gets the columns, loops through them once to display the headings. For each row it then calls a function with displays them. The key of the lowest value is found (if 2 keys share the lowest value then the lowest key is used). It loops around the columns returned and echos them out, putting out a color:#ff0000; in the style for the lowest value column.
<?php
$dbHost = 'localhost'; // usually localhost
$dbUsername = 'xxxx';
$dbPassword = 'xxxx';
$dbDatabase = 'xxxx';
$db = mysql_connect($dbHost, $dbUsername, $dbPassword) or die ("Unable to connect to Database Server.");
mysql_select_db ($dbDatabase, $db) or die ("Could not select database.");
$ColumnNames = mysql_query("SELECT GROUP_CONCAT(CONCAT('`', column_name, '`')) AS some_columns
FROM information_schema.COLUMNS
WHERE table_name = 'supplierprice'
AND column_name NOT IN ('supp_price_id',
'region',
'country',
'net_id',
'networkname',
'mcc',
'mnc',
'mnp')")
or die("mysql error");
if($rows=mysql_fetch_array($ColumnNames))
{
$sql = "SELECT ".$row['some_columns']." FROM supplierprice";
$query_values = mysql_query($sql);
if($rows2=mysql_fetch_assoc($ColumnNames))
{
echo "<tr>";
foreach($rows2 AS $key=>$value)
{
echo "<th style='width:67px;' >".$key." </th>";
}
echo "</tr>";
process_row($rows2);
while($rows2=mysql_fetch_assoc($ColumnNames))
{
process_row($rows2);
}
}
}
function process_row($in_row)
{
$lowest_values_key = min(array_keys($in_row, min($in_row)));
echo "<tr>";
foreach($in_row AS $key=>$value)
{
echo "<td style='width:67px;".(($lowest_values_key == $key) ? 'color:#ff0000;' : '' )."' >".$value." </th>";
}
echo "</tr>";
}
?>
At the moment I am displaying only the first ID sorted ascending.
I have to display multiple ID results, from the same table. How would I accomplish this task?
<?php
$dbhost ='localhost';
$dbuser =‘user’; $dbpass =‘pass’;
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(!$conn){
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT * FROM table_name ORDER BY id ASC LIMIT 0, 1';
mysql_select_db('database_name’);
$retval = mysql_query($sql, $conn);
if(!$retval){
die('Could not get data: ' . mysql_error());
}
?>
<?php while($row = mysql_fetch_assoc($retval))
{echo "{$row[‘full_name’]}”.”
{$row[‘telephone']}"."
{$row[‘email’]}”;}
mysql_close($conn);
?>
All you have to do is change the LIMIT 1 to whatever number you want, or just remove it. Removing it would look like:
$sql = 'SELECT * FROM table_name ORDER BY id ASC'
why do you have a limit if you want all the ids to be displayed?
you can use a while loop in your script
for eg:
while($row = mysql_fetch_assoc($test)){
<div id="'.$row['id'].'">'.$row['id']'.</div>;
}
which will output:
<div id="my id 1">my data 1</div>
<div id="my id 2">my data 2</div>
<div id="my id 3">my data 3</div>
.
.
.
and so on
your modified code that outputs the data in different divs
<?php
$db = mysqli_connect("localhost", "user", "pass" ,"database name") or die("Could not connect database");//keep it in one line and use (mysqli) instead of (mysql) because newer mysql versions donot support (mysql)
$retreive=mysqli_query($db,'SELECT * FROM table_name ORDER BY id');//if youre connection is in another file you need to include the connection variable before the selection line.In this case $db is the connection variable
while($row=mysqli_fetch_array($retrieve))
{ the following code creates a div for each result
echo '<div class="'.$row['fullname'].'">'.$row['fullname'].'</div>';.
echo '<div class="'.$row['telephone'].'">'.$row['telephone'].'</div>';
echo '<div class="'.$row['email'].'">'.$row['email'].'</div>';
}
?>
tried the above code on my test server and it works