No results from mysql_query - php

I am coding some PHP working with two MySQL databases. What I am working toward is different information to be sourced from the two databases which will then populate some form fields like the drop-down menu. The form will then be posted to create a printable document yada yada...
What Works
The connection to the first database works fine, the field is populated and there are no errors.
What Doesn't Work
When I introduce the second Database I get no errors but the form wont populate. I make this change...
From One Database:
$sql = mysql_query"SELECT * FROM car WHERE color='blue' ORDER BY sqm ASC";
To Two Databases:
$sql = mysql_query("SELECT * FROM car WHERE color='blue' ORDER BY sqm ASC", $conn);
The Connection
Source: http://rosstanner.co.uk/2012/01/php-tutorial-connect-multiple-databases-php-mysql/
How do you connect to multiple MySQL databases on a single webpage?
<?php
// connect to the database server
$conn = mysql_connect("localhost", "cars", "password");
// select the database to connect to
mysql_select_db("manufacturer", $conn);
// connect to the second database server
$conn2 = mysql_connect("localhost", "cars", "password");
// select the database to connect to
mysql_select_db("intranet", $conn2);
?>
The Execution
It appears that $sql = mysql_query("SELECT * FROM car WHERE color='blue' ORDER BY sqm ASC", $conn); Is my problem
<form name="form" method="post" action="review.php">
<table><td>
<select>
<option value="">--Select--</option>
<?php $sql = mysql_query("SELECT * FROM car WHERE color='blue' ORDER BY sqm ASC", $conn);
$rs_result = mysql_query ($sql);
// get the entry from the result
while ($row = mysql_fetch_assoc($rs_result)) {
// Print out the contents of each row into a table
echo "<option value=\"".$row['carname']."\">".$row['carname']."</option>";
}
?>
</select>
</td></table>
</form>
Thanks In advance for any help :)

you've got 2 mysql query commands going...
<?php
$sql = mysql_query("SELECT * FROM car WHERE color='blue' ORDER BY sqm ASC", $conn);
$rs_result = mysql_query ($sql); // <-- $sql here is the result of the first query (ie. not a sql command)
should be
<form name="form" method="post" action="review.php">
<table><td>
<select>
<option value="">--Select--</option>
<?php
$sql = "SELECT * FROM car WHERE color='blue' ORDER BY sqm ASC";
$rs_result = mysql_query( $sql, $conn );
// get the entry from the result
while ($row = mysql_fetch_assoc($rs_result)) {
// Print out the contents of each row into a table
echo "<option value=\"".$row['carname']."\">".$row['carname']."</option>";
}
?>
</select>
</td></table>
</form>
good luck!

Related

Use PHP loop to fetch tables data from the table which contain names of database tables

I have table named category which contain names of other tables in the same database. I want to fetch table names from category table and then fetch data from each table from db. So far I have this code below:
$db = new mysqli('localhost', 'root', '', 'db_cat');
if($db){
// $q = "SELECT TABLE";
// $echo = $db->query($q);
// echo $echo;
// $result = $db->query("SHOW TABLES");
$qCat="SELECT * FROM product_category";
$cat_query= $db->query($qCat) or die(mysql_error());
while ($fetch= $cat_query->fetch_object())
{
$cat_id=$fetch->id;
$category=$fetch->category;
$p_cat=str_replace(" ","_",strtolower($category).'_categories');
//if(strlen($category)>22){$fine_product_name= substr($service, 0,19).'...';}else{ $fine_product_name=$category;}
$result = $db->query("SHOW TABLES");
while($row = $result->fetch_array()){
$tables[] = $row[0];
}
}
The second query must be different.
$result = $db->query("SELECT * FROM $category");
while($row = $result->fetch_array()){
$tables[] = $row[0];
}
print_r($tables);
First of all your design to connect to a database is not that good, Please check the below code for a proper way of connecting to it.
<?php
$con=mysqli_connect("localhost","root","","db_cat");
//servername,username,password,dbname
if (mysqli_connect_errno())
{
echo "Failed to connect to MySql: ".mysqli_connect_error();
}
?>
Here is a sample code of getting data from a table ( where this table name is in another table).
$get_table_name ="SELECT TableName FROM table_name";
$get_name=mysqli_query($con,$get_table_name);
$count=0;
while($row_name=mysqli_fetch_array($get_name)){
$count++;
$tbName=$row_name['TableName'];
$_SESSION['table_name'][count]=$tbName;
}
This will show you how to fetch data from one table. You can use a For loop to get all the tables
$table=$_SESSION['table_name'][1];
$get_table ="SELECT * FROM $table";
.... // Normal way of fetching data
You can try to adjust your code according to this and improve it.
For further reference please refer http://php.net/manual/en/book.mysqli.php

Mysql select query using value from a php dropdown

I started programming in php and I'm having a small doubt.
I'm trying to do a search the database using a value from a dropdown.
The problem is that the query always uses the last value of the dropdown.
Does anyone can help me find the error?
Why is research in where clause is always the last value of the dropdown?
Code
<tr><td>Technical:</td><td>
<select>
<?php
$query = "SELECT idTechnical, name FROM technicals";
$result2 = mysql_query($query);
$options="";
while($row=mysql_fetch_array($result2)){
$id=$row["idTechnical"];
$thing=$row["name"];
echo "<OPTION VALUE=$id>$thing</option>";
}
?>
</select>
<?php
if (isset($_POST['Next'])) {
if($_REQUEST['Next']=='Search') {
{
$sql="select idTask, descTask, deadline, idTechnical from tasks where idTechnical = '$id' order by deadline desc";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
}
}
}
?>
I select any value from dropdown, but only uses the last value in clause where :S
Here is what I would do for the form (assuming you have a proper form tag with an action attribute that points to the correct PHP script):
<tr>
<td>Technical:</td>
<td>
<select name="technical">
<?php
$query = "SELECT idTechnical, name FROM technicals";
$result2 = mysql_query($query);
$options="";
while($row=mysql_fetch_array($result2)){
echo '<option value='.$row["idTechnical"].'>
'.$row["name"].'
</option>';
}
?>
</select>
</td>
Then in the PHP script:
$sql='SELECT
idTask,
descTask,
deadline,
idTechnical
FROM tasks
WHERE idTechnical = '.$_REQUEST['technical'].'
ORDER BY deadline DESC';
$result=mysql_query($sql);
$count=mysql_num_rows($result);
This should do it for you.
But please note: The script above is a security risk because it leaves the door wide open for SQL injection
A better way to do this would be to use a PDO Prepared statement, like this:
$db = new PDO('mysql:host=CHANGE_THIS_TO_YOUR_HOST_NAME;
dbname=CHANGE_THIS_TO_YOUR_DATABASE',
'CHANGE_THIS_TO_YOUR_USERNAME',
'CHANGE_THIS_TO_YOUR_PASSWORD');
$sql='SELECT
idTask,
descTask,
deadline,
idTechnical
FROM tasks
WHERE idTechnical = :id
ORDER BY deadline DESC';
$query = $db->prepare($sql);
$query->bindValue(':id', $_REQUEST['technical']);
$query->execute();
$count = $query->rowCount();
If you're just starting in PHP, I would highly recommend that you spend some time to become familiar with PDO Database querying. Good luck and happy coding!

how do i SQL specific items from a column (php)

<?php
mysql_connect('localhost', 'admin', '') or die (mysql_error());
mysql_select_db("elia_internal") or die (mysql_error());
$query = "SELECT * FROM information GROUP BY Karori ";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
print"<tr><td>".$row['Agent']."</td><td>".$row['Address']."</td><td>".$row['Suburb']."</td><td>".$row['Bedrooms']."</td><td>".$row['Bathrooms']."</td><td>".$row['Price']."</td>";
}
?>
I am trying to SQL specific items from columns from my database for example im trying to pull up all houses listed in the suburb Karori. the column is Suburb and there are 5 different suburbs in the column. how do i get the php to show JUST the houses listed in Karori and not the others.
Try this
$query = "SELECT * FROM information where suburb="karori" GROUP BY Karori ";

How to display table data in reverse? (php)

I have simple code for displaying images. I created table with 4 columns (ID, location, capture, equence) and inserted there 18 records. My question is: how to display all records from table in reverse mode? I need to make that the last entry will be displayed first, and the first entry displayed last.
What I need: 18-1
What I have now: 1-18
I was searching for simple codes to do that, but notwing worked at all. So i'd be very grateful if someone will help me to solve that problem.
Heres the basic code of my display script:
<?php
mysql_connect("localhost", "***", "***") or die(mysql_error());
mysql_select_db("martinidb1337") or die(mysql_error());
$result = mysql_query("SELECT * FROM klpgalerija") or die(mysql_error()); while($row = mysql_fetch_array( $result )) {
echo '<p><img src="'.$row['location'].'"></p>';
}
You have to use MySQL ORDER BY clause for that,
SELECT * FROM klpgalerija ORDER BY id DESC
Note: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated.
So use either PDO or MySQLi (IMO PDO is way to go)
Changed query from "SELECT * FROM klpgalerija" to "SELECT * FROM klpgalerija ORDER BY ID DESC"
<?php
mysql_connect("localhost", "***", "***") or die(mysql_error());
mysql_select_db("martinidb1337") or die(mysql_error());
$result = mysql_query("SELECT * FROM klpgalerija ORDER BY ID DESC") or die(mysql_error()); while($row = mysql_fetch_array( $result )) {
echo '<p><img src="'.$row['location'].'"></p>';
}
add an order by desc clause in your sql query
$result = mysql_query("SELECT klpgalerija.* FROM klpgalerija order by klpgalerija.ID desc") or die(mysql_error());

Dynamic display of table based on values in combobox using MySQL and PHP

I have two tables in MySQL db namely vendor and menu.
vendor table
vendor_id(Primary) vendor_name
menu table
menu_id(Primary) menu_details cost vendor_id(Foreign)
I need to create a combo box which fetches vendor_name instances from the MySQL db dynamically. On selection of vendor_name from the combo box, menu table(HTML and table should be in editable mode) should be displayed from the MySQL database. I am new to MySQL and PHP. So please help me to deal with this criteria.
Below is the code that I'm working on:
<html><body>
<select name="vendor" id="vendor">
<option value="Choose">Choose</option>
<?php $conn = mysql_connect('localhost','root','');
if (!$con) { die('Could not connect: ' . Mysql_error()); }
mysql_select_db('project',$conn);
$query = "select vendor_id,vendor_name from vendor";
$result = mysql_query($query,$conn);
while($fetch= mysql_fetch_array($query)){
echo "<option>".$row[1]."</option>";
}?>
</select>
</body></html>
If I understood You correctly then this should do the thing that you're after. It displays the combobox from vendor table and if you change the selection, it reloads the page and shows the menu that corresponds to the vendor_id in the menu table.
<html>
<body>
<?php
// connecting to the server
$conn = mysql_connect('localhost','root','') or die('Could not connect: ' . Mysql_error());
// selecting the database
mysql_select_db('project',$conn) or die("db error: ".mysql_error());
// is menu_id set?
if (isset($_GET['menu']) && intval($_GET['menu'])>0) {
// if yes, then query the menu items
$query = "select menu_id, menu_details, cost, vendor_id from menu where vendor_id=".intval($_GET['menu']);
$result = mysql_query($query, $conn) or die("SQL error: ".mysql_error());
while ($row = mysql_fetch_assoc($result)) {
// print the menu items
print_r($row);
}
}
// select the vendor combobox
$query = "select vendor_id,vendor_name from vendor";
$result = mysql_query($query,$conn);
// If the vendor selection has changed then reload the page
?>
<select name="vendor" id="vendor" onchange="document.location='index.php?menu='+this.options[this.selectedIndex].value;">
<option value="Choose">Choose</option>
<?php
// loop through the results
while($fetch= mysql_fetch_assoc($query)){
echo "<option value=\"".$row['vendor_id']."\">".$row['vendor_name']."</option>";
}
?>
</select>
</body>
</html>
the assumption is that the file is named index.php. (look at the select onchange="" filename. You'd need to change it if You have a different filename.

Categories