PHP mysql query x=$variable - mysql

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.

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();
?>

Why does this not work? MySQL (TRYING TO MAKE A WHITELIST UPDATE)

Trying to make a whitelist system with the arguments below, but It is not echoing anything. The code is not function as it should atm. The problem is that it is not updating it. This is the code:
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "premium";
$key = $_GET["key"];
$user = $_GET["user"];
$id = $_GET["id"];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$result = mysqli_query("SELECT * FROM `whitelists` WHERE `key` = '$key'");
if( $result === false ) {
echo 'nope';
}
else
{
echo 'yes';
mysqli_query("UPDATE `whitelists` SET `id` = '$id', `user` = '$user' WHERE `key` ='$key' ");
}
$conn->close();
?>
first thing you should do is Use mysqli_* functions or PDO, because your code is Deprecated.
example:
$result = mysqli_query($conn, "UPDATE whitelists SET `id` = '$id', `user` = '$user' WHERE `key` ='$key' ");
Secondly you should use a false statement instead of num_rows.
if($result === false)
{
echo "nope";
}
else
{
// Execute your query here.
}

exclude status in dropdown list

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>";
?>

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();
?>

converting single PHP mySQL update into mass MySQL update

Ok so thank you for the feedback until now, with it I have got to the following point:
<?php
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "madtags";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = mysqli_query($conn, "SELECT id from users");
$userinfo = array();
while ($row_user = $sql->fetch_assoc())
$userinfo[] = $row_user;
foreach ($userinfo as $user) {
$url = "http://www.pdga.com/player/".$user['id'];
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$html = curl_exec($ch);
curl_close($ch);
$dom = new DOMDocument();
#$dom->loadHTML($html);
$xpath = new DomXPath($dom);
$class = 'current-rating';
$divs = $xpath->query("//*[contains(concat(' ', normalize-space(#class), ' '), ' $class ')]");
foreach($divs as $div) {
preg_match('/Current Rating:\s+(\d+)/', $div->nodeValue, $results);
}
$sql = "UPDATE users SET rating=$results[1] WHERE id=id";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully <br>";
} else {
echo "Error updating record <br> " . $conn->error;
}
$conn->close();
}
?>
By changing the "where" command on row 45 "$sql = "UPDATE users SET rating=$results[1] WHERE id=id";", i can get it to run properly (this is a test array of 6 rows)
Record updated successfully
Error updating record
Error updating record
Error updating record
Error updating record
Error updating record
But I guess I am defining something wrong as it is not able to update the record after the first run.
Ideas?
original post
I am creating a competition website, parallel to a rating system used by a worldwide website. So basically I have created the code below to call for a rating from the website.
Since creating this, the member base has ballooned greatly. So creating this code individually for each member is not useful anymore. So I would need to develop this to basically run for each row in my MySQL table (this code is run every 2 weeks approx.)
<?php
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "madtags";
$url = "http://www.pdga.com/player/***ID***";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$html = curl_exec($ch);
curl_close($ch);
$dom = new DOMDocument();
#$dom->loadHTML($html);
$xpath = new DomXPath($dom);
$class = 'current-rating';
$divs = $xpath->query("//*[contains(concat(' ', normalize-space(#class), ' '), ' $class ')]");
foreach($divs as $div) {
preg_match('/Current Rating:\s+(\d+)/', $div->nodeValue, $results);
}
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE users SET rating=$results[1] WHERE id=***ID***";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>
So basically variable for each individual line are the parts marked ID ($url and $sql update).
Further, the table currently has 4 columns >> id, name, tag, rating
Thanks a bunch ahead of time.