exclude status in dropdown list - mysql

I'm trying to exclude all values where status is Defective in my mobo table, but this code doesn't work.
It's still showing everything in my Dropdown
<?php
$conn = new mysqli('localhost', 'root', 'admin2018', 'inventory')
or die ('Cannot connect to db');
$result = $conn->query("select mobo, status FROM mobo WHERE NOT 'status = Defective'");
echo "<select name='mobo'>";
while ($row = $result->fetch_assoc()) {
unset($id, $name);
$id = $row['mobo'];
$name = $row['mobo'];
echo '<option value="'.$id.'">'.$name.'</option>';
}
echo "</select>";
?>

nevermind I already fix it using
<?php
$conn = new mysqli('localhost', 'root', 'admin2018', 'inventory')
or die ('Cannot connect to db');
$result = $conn->query("select mobo, status FROM mobo WHERE status!='Defective'");
echo "<select name='mobo'>";
while ($row = $result->fetch_assoc()) {
unset($id, $name);
$id = $row['mobo'];
$name = $row['mobo'];
echo '<option value="'.$id.'">'.$name.'</option>';
}
echo "</select>";
?>

Related

Multiple WHERE find_in_set lookups

I'm wanting to pull steamid from multiple clubs. Website is based on an Invision Community site.
The output is a list of steamids which is imported into our game servers.
My simple pull from one club code below works perfectly:
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT
core_members.steamid,
core_members.member_id,
core_members.name,
core_clubs_memberships.member_id,
core_clubs_memberships.club_id
FROM core_members
INNER JOIN core_clubs_memberships
ON core_members.member_id = core_clubs_memberships.member_id
WHERE
(core_members.steamid IS NOT NULL AND TRIM(core_members.steamid) <> '') AND (find_in_set('1',core_clubs_memberships.club_id))
ORDER BY core_members.member_id ASC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["steamid"]. " ". $row["name"];
echo ("\r\n");
}
} else {
echo "0 results";
}
$conn->close();
?>
Whenever I try to add a second group, the code doesn't work, for example I tried:
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT
core_members.steamid,
core_members.member_id,
core_members.name,
core_clubs_memberships.member_id,
core_clubs_memberships.club_id
FROM core_members
INNER JOIN core_clubs_memberships
ON core_members.member_id = core_clubs_memberships.member_id
WHERE
(core_members.steamid IS NOT NULL AND TRIM(core_members.steamid) <> '') AND ((find_in_set('1',core_clubs_memberships.club_id) OR (find_in_set('6',core_clubs_memberships.club_id)))
ORDER BY core_members.member_id ASC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["steamid"]. " ". $row["name"];
echo ("\r\n");
}
} else {
echo "0 results";
}
$conn->close();
?>

Working script stop working (php,mysql)

I've got a weird problem.
I created a website on localhost. Everything was working fine. I could connect to my mysql db without any problems.
I decided to put it on external server to show it to my friends. I changed $username, $dbname etc.
Then I decided to work little with the website back on localhost. And then it started. When I run my script which adds new records to my db (name, message, email) this shows up.
No database selected
Everything is just the same it was before I decided to put it on the server. What can be the problem?
EDIT: Here's the code of connect.php
<?php
$username ="root";
$password = "";
$host = "localhost";
$table = "test";
$conn = new mysqli("$host", "$username", "$password", "$table");
if (!$conn) {
die("Nie udało się połączyć z bazą danych: ".mysqli_connect_error());
}
and here's a script implemented into index.php
<?php
include 'connect.php';
$sql = "SELECT s_id, name, s_like, s_dislike, message FROM suggestions ORDER BY rand()";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$s_id = $row["s_id"];
$s_like = $row["s_like"];
$s_dislike = $row["s_dislike"];
echo '<p>' . $row["message"] . '</p>';
echo '<h1 class="podpis">' . $row["name"] . '</h1>';
mysqli_close($conn);
?>
and here's the cde of comments.php where I see the message about No database selected.
<?php
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$message = $_REQUEST['message'];
$username ="root";
$password = "";
$host = "localhost";
$table = "test";
$conn = mysqli_connect($host, $username, $password, $table);
$query = "INSERT INTO suggestions (name, email, message) VALUES ('$name',
'$email', '$message')";
if (mysql_query($query)) {
echo 'Dodano sugestię!';
header('Location: index.php');
} else{ die(mysql_error());}
mysql_close();
?>

i created a table in database to display photos and i made it to display ,but i want them to display from the last to the first

<?php
$conn = mysql_connect("localhost","root","");
if(!$conn){
echo mysql_error();
}
$db = mysql_select_db("imagestore",$conn);
if(!$db ){
echo mysql_error();
}
$q = "SELECT * FROM imagetable";
$r = mysql_query("$q",$conn);
if($r)
{
while($row=mysql_fetch_array($r) )
{
header("Content-type: text/html");
echo "</br>";
echo $row['photoname'];
echo "</br>";
$type = "Content-type: ".$row['phototype'];
header($type);
echo "<img src=image.php?fotoid=". $row['fotoid']."width =300 height = 35. 300/>";
}
}
else{
echo mysql_error();
}
?>
I know ORDER by but is not working to display photos as i want in my page .
I am a beginner .
I used $q = "SELECT * FROM imagetable ORDER BY fotoid DESC";

PHP mysql query x=$variable

How i can send $country to second code (select emr_value from countries where country_name='$country') i don't know why $country = "" or null please help
thanks
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "db";
global $country;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT meta_value FROM `wp_usermeta` where user_id=$user_id and meta_key='custom_field_6' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$country = $row["meta_value"];
echo $country;
}
} else {
echo "0 results";
}
$conn->close();
?>
Second Code (Here i want to display data for this query (select emr_value from countries where country_name='$country'))
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "db";
global $country;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "select emr_value from countries where country_name='$country'";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
$emr_value = $row["emr_value"];
echo "<h1>EMR : " . $emr_value . "</h1>";
}
$conn->close();
?>
Maybe you should have only one code:
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT meta_value FROM wp_usermeta WHERE user_id='$user_id' AND meta_key='custom_field_6'";
$result1 = $conn->query($sql);
if ($result1->num_rows > 0) {
// output data of each row
while ($row1 = $result1->fetch_assoc()) {
$country = trim($row1["meta_value"]);
$sql = "SELECT emr_value FROM countries WHERE country_name='$country'";
$result2 = $conn->query($sql);
while ($row2 = $result2->fetch_assoc()) {
echo "<h1>EMR : " . $row2["emr_value"] . "</h1>";
}
}
} else {
echo "0 results";
}
$conn->close();
?>
Or create two functions in one code: one that returns $country and one that returns emr_value.

Unable to display post titles with SQL Tables

I'm trying to display a list of titles in a modify/delete page. They load fine since I get the correct amount of rows, put for some reason I can't the text printed.
Here's the function which corresponds to the issue.
function manage_content()
{
echo '<div id="manage">';
$sql = "SELECT * FROM content";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)): ?>
<div>
<h2 class="title"><?php $row['title'] ?></h2>
<span class="actions">Edit | Delete</span>
</div> <?php
endwhile;
echo '</div>'; //End of Manage Div
}
And this is the full code:
<?php
class blog {
private $host;
private $username;
private $password;
private $db;
private $link;
public function __construct($host, $username, $password, $db){
$this->db = $db;
$this->link = mysql_connect($host, $username, $password, $db);
mysql_select_db($this->db, $this->link) or die (mysql_error());
}
function get_content($id=''){
if($id !=NULL):
$id = mysql_real_escape_string($id);
$sql = "SELECT * FROM content WHERE id = '$id'";
$return = '<p>Vover al Indice</p>';
else:
$sql = "SELECT * FROM content ORDER by id DESC";
endif;
$res = mysql_query($sql) or die (mysql_error());
if(mysql_num_rows($res) !=NULL):
while($row = mysql_fetch_assoc($res)){
echo '<h1>'.$row['title'].'</h1>';
echo '<p>'.$row['body'].'</p>';
}
else:
echo '<p>Oops, post not found!</p>';
endif;
if(isset($return)){
echo $return;
}
}
function add_content($p){
$title = mysql_real_escape_string($p['title']);
$body = mysql_real_escape_string($p['body']);
if(!$title OR !$body):
if(!$title):
echo "<p>You have to fill the title.</p>";
endif;
if(!$body):
echo "<p>You have to fill the body.</p>";
endif;
echo '<p>Try again!</p>';
else:
$sql = "INSERT INTO content VALUES (null, '$title', '$body')";
$res = mysql_query($sql) OR DIE (mysql_error());
echo "Added sucessfully!";
endif;
}
function manage_content()
{
echo '<div id="manage">';
$sql = "SELECT * FROM content";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)): ?>
<div>
<h2 class="title"><?php $row['title'] ?></h2>
<span class="actions">Edit | Delete</span>
</div> <?php
endwhile;
echo '</div>'; //End of Manage Div
}
}// End of Class
?>
You're forgetting an echo in your manage_content() function:
<?php $row['title'] ?>
Should be:
<?php echo $row['title']; ?>
That's part of the problem anyway.