I try to get data from category using mysql and php.
Sql Structure:
Category
-cat_id
-name
Date
-id
-url
-category
Php code:
<?php
$sql = "select * from category";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
echo '<option value="'.$row["cat_id"].'">'.$row["name"].'</option>';
}
}
$sql = "select * from date WHERE category='1'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
echo '.$row["url"].';
}
}
?>
when i select the category the data is not listed.
Any idea?
Try this Code . Just removed single quotations
$sql = "select * from date WHERE category='1'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
echo $row["url"];
}
}
May this will work:
PHP Code
<?php
$sql = "select * from category";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
echo '<select id="category" name="category">';
echo '<option value="'.$row["cat_id"].'">'.$row["name"].'</option>';
echo '</select>';
}
}
$sql = "select * from date WHERE category='1'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
echo 'url is: '.$row["url"];
}
}
?>
Try this
$sql = "select * from date WHERE category='1'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
echo $row["url"];
}
}
if you want result with in single quotes
$sql = "select * from date WHERE category='1'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
echo "'".$row["url"]."'";
}
}
Please avoid mysql_* because the mysql_* functions have been removed in PHP7. Use MySQLi instead.
PHP + Mysql :
<?php
$sql = "select * from category";
$result = mysql_query($sql);
echo "<select name='category'>";
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result)) {
echo '<option value="'.$row["cat_id"].'">'.$row["name"].'</option>';
}
}
echo "</select>";
if(!empty($_POST['category'])) {
$category_id = $_POST['category'];
$sql = "select * from date WHERE category = '".$category_id."'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result))
{
echo '.$row["url"].';
}
}
}
?>
PHP + Mysqli
<?php
$servername = "localhost";
$username = "username";
$password = "";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "select * from category";
$result = $conn->query($sql);
echo "<select name='category'>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<option value="'.$row["cat_id"].'">'.$row["name"].'</option>';
}
}
echo "</select>";
if(!empty($_POST['category'])) {
$category_id = $_POST['category'];
$sql = "select * from date WHERE category = '".$category_id."'";
$result = $conn->query($sql);
if($result->num_rows > 0) {
while ($row = $result->fetch_assoc())
{
echo '.$row["url"].';
}
}
}
$conn->close();
?>
Nothing changed.
I'm using this code:
<?php
$sql = "select * from category";
$result = mysql_query($sql);
echo "<select name='category'>";
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result)) {
echo '<option value="'.$row["cat_id"].'">'.$row["name"].'</option>';
}
}
echo "</select>";
if(!empty($_POST['category'])) {
$sql = "select * from date WHERE category = '1'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result))
{
echo $row["url"];
}
}
}
?>
If i delete if condition the data is listed but all the time.
Related
I'm trying to make an html table with the data in the database but when I write echo it just shows me a result. How can I echo all the elements?
$sql = "SELECT * FROM app_spot where company_id='$company_id'";
$result = mysqli_query($db, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)) {
$spot_id = $row["id"];
$spot_name = $row["spot_name"];
$store_location = $row["store_location"];
$spot_budget = $row["spot_budget"];
$spot_status = $row["spot_status"];
}
$spotcount = mysqli_num_rows($result);
} else {
echo "0 results";
}
Echo the table rows in the while loop.
if (mysqli_num_rows($result) > 0) {
echo "<table><tr><th>ID</th><th>Name</th><th>Location</th><th>Budget</th><th>Status</th></tr>";
while($row = mysqli_fetch_array($result)) {
$spot_id = $row["id"];
$spot_name = $row["spot_name"];
$store_location = $row["store_location"];
$spot_budget = $row["spot_budget"];
$spot_status = $row["spot_status"];
echo "<tr><td>$spot_id</td><td>$spot_name</td><td>$store_location</td><td>$spot_budget</td><td>$spot_status</td></tr>";
}
echo "</table>";
}
I have a small problem/question...
I have made a while loop that's echo a href link. How can I give the $_Session the correct value for the selected ncrnummer?
This is my code:
$query1 = "SELECT id FROM `ncr_input` WHERE projectnummer = '".$_POST['Projectnummer']."'";
$result1 = mysqli_query($conn, $query1);
if ($result->num_rows > 0) {
if ($result1->num_rows > 0) {
while ($row = mysqli_fetch_assoc($result1)) {
$ncrnummer = $row['id'];
if ($row['id']) {
$_SESSION['ncrnummer'] = $row['id'];
echo "{$ncrnummer}";
echo "<br>";
}
}
}
}
I am doinga MySQL query to retreive data using PHP. I need to have a logic that if the data set returned is empty it shows a warning message else it displays the results:
$searchQuery = mysql_escape_string($_POST['searchQuery']);
$sql="SELECT * FROM db.tblname WHERE column1 = '".$searchQuery."'";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result)){
echo $row["column1"];
}
mysqli_close($conn);
You need num_rows on the object
$result = mysqli_query($conn,$sql);
if ($result->num_rows == 0) {
echo 'result empty';
} else {
while($row = mysqli_fetch_array($result)){
echo $row["column1"];
}
}
http://php.net/manual/en/mysqli-result.num-rows.php
Change mysql_escape_string to mysqli_escape_string here
$where = "";
if(isset($_POST['searchQuery']) && trim($_POST['searchQuery'])){
$searchQuery = mysqli_escape_string($conn,$_POST['searchQuery']);
$where =" WHERE column1 = '".$searchQuery."'";
}
$sql="SELECT * FROM db.tblname ".$where;
It's as simple as this:
if ($result) {
//Your code
} else {
echo "Error: ".mysqli_error($conn);
}
First check with mysqli_num_rows(). Here is how to
$searchQuery = mysql_escape_string($_POST['searchQuery']);
$sql="SELECT * FROM db.tblname WHERE column1 = '".$searchQuery."'";
$result = mysqli_query($conn,$sql);
if(mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)){
echo $row["column1"];
}
}
else {
echo "No records found";
}
mysqli_close($conn);
$searchQuery = mysql_escape_string($_POST['searchQuery']);
$sql="SELECT * FROM db.tblname WHERE column1 = '".$searchQuery."'";
$result = mysqli_query($conn,$sql);
if(mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)){
echo $row["column1"];
}
} else {
echo "No results found";
}
mysqli_close($conn);
In below code it shows table row value outside while loop but not shows inside while loop. While loop not working; Please let me know whats wrong in it?
<?php
$sql = "SELECT * FROM cl_banner ORDER BY id;";
$res = q($sql) or die(mysql_error());
if($res && mysql_num_rows($res)>0)
{
while($row = mysql_fetch_assoc($res));
{
echo $row["title"];
echo "hi";
} // End While
} // End If
?>
<?php
$sql = "SELECT * FROM cl_banner ORDER BY id";
^^^^
$res = mysql_query($sql) or die(mysql_error());
^^^^^^^^^^
if($res && mysql_num_rows($res)>0)
{
while($row = mysql_fetch_assoc($res));
{
echo $row["title"];
echo "hi";
} // End While
} // End If
?>
Try this:
<?php
$sql = "SELECT * FROM cl_banner ORDER BY id";
$res = mysql_query($sql) or die(mysql_error());
if($res && mysql_num_rows($res)>0)
{
while($row = mysql_fetch_assoc($res))
{
echo $row["title"];
echo "hi";
} // End While
} // End If
?>
I'm trying to use the following scripts so that the result of the first one determines the output of the second one.
<?
$db = mysql_connect('localhost','username','pass') or die("Database error");
mysql_select_db('dbname', $db);
$query = "SELECT pool FROM winners";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result))
if ( $row['pool'] % 2 )
{
echo "<h4>Result 1</h4>";
echo "<br />";
}
else
{
echo "<h4>Result 2</h4>";
echo "<br />";
}
?>
<?php
$db = mysql_connect('localhost','username','pass') or die("Database error");
mysql_select_db('dbnamesameasother', $db);
$query2 = "SELECT * FROM comments";
$result2 = mysql_query($query2);
while ($row2 = mysql_fetch_assoc($result2))
if ( $row2['commentid'] % 2 ==0 )
{
echo $row2['name'];
echo "<br />";
}
else
{
echo $row2['name'];
}
?>
So basically if the first script chooses Result 1 I only want to echo the names which associate with that result. The names are associated by commentid where an odd commentid would be result 2 and an even commentid would be result 1. Is there any way to do this without using a union statement?
It recommend that you to create a function and then call it.something like this :
<?php
$db = mysql_connect('localhost','username','pass') or die("Database error");
mysql_select_db('dbname', $db);
$query = "SELECT pool FROM winners";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result))
if ( $row['pool'] % 2 )
{
echo "<h4>Result 1</h4>";
$names = get_names(1);
foreach ($names as $name) {
echo $name . "<br/>";
}
}
else
{
echo "<h4>Result 2</h4>";
$names = get_names(0);
foreach ($names as $name) {
echo $name . "<br/>";
}
}
Function get_names($pool_result)
{
$name_array = array();
$db = mysql_connect('localhost','username','pass') or die("Database error");
mysql_select_db('dbnamesameasother', $db);
$query = "SELECT * FROM comments WHERE commentid % 2 = $pool_result";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result))
{
array_push($name_array , $row['name']);
}
return $name_array;
}
?>
}