I'm trying to show all usernames in the users table onto my index page.
Here is the PHP code:
$result = $db->prepare("SELECT username FROM users");
$result->execute();
while ($row = $db->fetchAll(PDO::FETCH_ASSOC))
{
$user = $row['users'];
$username = $row['username'];
}
echo $user;
echo $username;
I think you are printing username outside of the while loop, so that you're getting the last username every time.
$result = $db->prepare("SELECT username FROM users");
$result->execute();
while ($row = $db->fetchAll(PDO::FETCH_ASSOC))
{
echo $username = $row['username'];
}
Related
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();
?>
Apparently i have something wrong because this query will not execute, Can anyone see why ?
$username=username;
$mysqli = #new mysqli($localhost, $user, $pass, $database);
$sql = "SELECT * FROM player_log WHERE user = '$username'AND log_status=online";
$result = $mysqli->query($sql);
if ($result->num_rows > 0){
echo "its works";
}
Multiple issues see modified code below
Added standard way to call mysqli (space after $username is not an issue)
$username=username;
$mysqli = #new mysqli($localhost, $user, $pass, $database);
$sql = "SELECT * FROM player_log WHERE user = '$username'AND log_status='online' ";
if ($result = $mysqli->query($sql)){
printf("Select returned %d rows.\n", $result->num_rows);
/* free result set */
$result->close();
echo "its works";
}
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.
if its not ñ I'm getting the right result but when I'm using it
UNDEFINED VARIABLE.
here's my approach to search the $location
and all Collation and operation in database is SET to "utf8_general_ci"
<?php
/*$location = $_GET['location'];*/
$location = $_GET['location'];
$result = $db->prepare("SELECT * FROM list WHERE loc = '".$location."'");
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
$title1=$row['title'];
$fname=$row['fname'];
$mname=$row['mname'];
$lname=$row['lname'];
$addr1=$row['addr'];
$post1=$row['position'];
$location=$row['loc'];
}
?>
my connection in db
<?php
include('connect.php');
$db_host = 'localhost';
$db_user = 'root';
$db_pass = '';
$db_database = 'xxx';
$db = new PDO('mysql:host='.$db_host.';dbname='.$db_database, $db_user, $db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>
I'm trying to send a list of recent entries into a mysql table via email once a week using cron jobs. Typically to call the list of recent entries I use this:
$result = mysql_query("SELECT * FROM stock WHERE PurchaseDate < '$TODAY' AND PurchaseDate > '$LASTWEEK'")
or die(mysql_error());
while ($list = mysql_fetch_array($result))
But obviously I can't put this code into the $message variable in php mail.
Any ideas?
$result = mysql_query("SELECT * FROM stock WHERE PurchaseDate < '$TODAY' AND PurchaseDate > '$LASTWEEK'") or die(mysql_error());
$entries = 'Entries: ';
while ($list = mysql_fetch_array($result)) {
$entries .= $list[entry] . ', ';
}
mail('someone#test.com', 'Stock', $entries);
This is just an example. Not sure what your table looks like.
The mail functions takes a string, and you are returning an array.
So you need to implode it implode(',', $list); or build a string with the result set.
You should use PHPMailer, Zend Mail or Swift_Mailer libraries that are safer and prevent for example from header injection.
try {
$result = mysql_query("SELECT * FROM your_table WHERE blank = 'blank' AND blank2 ='blank2'");
$num_rows = mysql_num_rows($result);
if($num_rows < 1) {
throw new Exception('There is no user who qualifies...');
}
if(!$result) {
throw new Exception('An Error Occurred..');
}
//mail off whatever you need to each user that qualifies based on your query criteria..
while($row = mysql_fetch_array($result)) {
$firstname = stripslashes($row['firstname']);
$lastname = stripslashes($row['lastname']);
$email = stripslashes($row['email']);
//and any other variables you need for the email...
$subject = 'Some Subject';
$message = 'Hello '.$firstname.' blah..blah...';
mail($email, $subject, $message);
//do something else...
echo 'All users emailed.';
}
}
catch (Exception $e) {
echo $e->getMessage();
}