i have a problem , i am storing a message on my database with $row[name] The text in my database is
Helloo $row['name'] how are you
$txt[1] is "Helloo $row['name'] how are you "
This is the code i tryed the first echo works, but the seccond is not working
<?php
$check = $db->prepare("SELECT * FROM appointments WHERE trimis='0'");
$check->execute();
$checkdb = $check -> fetchAll();
foreach($checkdb as $row){
echo $row['name']; // THIS WORKS
$check = $db->prepare("SELECT * FROM settings WHERE id='1'");
$check->execute();
$checkdb2 = $check -> fetchAll();
foreach($checkdb2 as $txt){
echo $txt[1]."<BR>"; // THIS DOESEN'T
}
}
?>
You should select the specific fields from your table when you write your query,
Because using wildcard SELECT * + accessing the rows by index like $txt[1] is the recipe for bugs:
$check1 = $db->prepare("SELECT name FROM appointments WHERE trimis='0'");
$check1->execute();
while($rows1 = $check1->fetch()){
echo $row['name'];
$check2 = $db->prepare("SELECT custom_text FROM settings WHERE id='1'");
$check2->execute();
while($rows2 = $check2->fetch()){
echo $rows2['custom_text']."<BR>";
}
}
Related
I cant figure out how to get multiple entries of data from my database. You can see below that I have newsID = 1, which allows me to get that to show on my database, but how should I change the code to be able to access other entries like newsID = 2 as well?
<?php
$queryAffinity = "SELECT * FROM affnews WHERE newsID = 1";
$stmt = $pdo->query($queryAffinity);
$row = $stmt->fetchObject();
?>
<?php if($row->newsID == 1) echo "<p>{$row->newsDescription}</p>"; ?>
this is the code im trying to use to echo the id 1
Edit your WHERE-Clause and put it in a loop. For example:
$queryAffinity = "SELECT * FROM affnews WHERE newsID = 1 OR newsID = 2";
$stmt = $pdo->query($queryAffinity);
while($row = $stmt->fetchObject())
{
//do something with $row
}
You Can use the following code:
$queryAffinity = "SELECT * FROM affnews WHERE newsID IN (1, 2)";
$stmt = $pdo->query($queryAffinity);
while($row = $stmt->fetchObject())
{
echo $row->id;
}
I try to print the username in members panel.
That means when the member login, I will print to him, for example, "Welcome MEMBER'S NAME".
The problem is, when I use this script to print the member name, it print to me all members from my database:
<?php
$id = #$_GET['id'];
$name = #$_GET['name'];
$select = "SELECT * FROM tblname WHERE id='$id'";
$run = mysqli_query($connect,$select);
while($row = mysqli_fetch_array($run)){
echo $row['name'];
}
?>
Can anyone help?
You should sanitize the $_GET as mysql_real_escape_string($_GET['id']); and instead of looping through the record resource you can use
$select = "SELECT * FROM tblname WHERE id='$id'";
$run = mysqli_query($connect,$select);
$row = mysqli_fetch_array($run);
echo $row['name'];
I am trying to write a query within a While loop using a dynamic variable but can't seem to get it working correctly. I am new to PDO so I'm not 100% if this is even the correct way to go about doing this. The connection to the database ($db) works fine, and the query runs fine without the second $STH2 lines. Please help :)
<?php
//This will list player info so the user can get the correct player ID
$STH = $db->query('SELECT id, name, tag from wdlTeams');
//Setting the fetch mode
$STH->setFetchMode(PDO::FETCH_ASSOC);
//Create listbox object and populates options with team names
echo "<select name='teamID'>";
while($row = $STH->fetch()) {
$id = $row['id'];
$name = $row['name'];
$tag = $row['tag'];
$seasonId = $row['seasonId'];
$STH2 = $db->prepare('SELECT name from wdlSeasons where id=$seasonId');
$STH2->execute();
$seasonName = $STH2->fetchColumn();
echo "<option value='$id'>$tag - $name ($seasonName)</option>";
echo "<br />";
}
echo "</select>";
?>
I have also tried changing to
$STH2 = $db->prepare("SELECT 'name' from 'wdlSeasons' where id='$seasonId'");
but with no luck
Try this:
$STH2 = $db->prepare('SELECT name from wdlSeasons where id=:id');
$STH2->bindParam(':id', $seasonId);
$STH2->execute();
I am fetching rows from a mysql table (jobs). Inside of that fetch, I am also fetching from another table (accounts) [to receive account api keys all depending on what ID_ASSOC is attacted to the job]: below is the code
$sql = "SELECT * FROM jobs";
$query = mysqli_query($db_conx, $sql);
while($row = mysqli_fetch_assoc($query)){
echo $row['action'];
echo "<br/>";
$job_poster_id = $row['id_assoc'];
$sql = "SELECT * FROM accounts WHERE id_assoc='$job_poster_id'";
$query = mysqli_query($db_conx, $sql);
while($rows = mysqli_fetch_assoc($query)){
$username = $rows['twitter_username'];
$consumer_key = $rows['consumer_key'];
$consumer_secret = $rows['consumer_secret'];
$access_token = $rows['access_token'];
$access_token_secret = $rows['access_token_secret'];
}
echo $job_poster_id ;
echo "<br/>";
echo $twitter_username;
echo "<br/>";
echo "----------------------------------";
echo "<br/>";
}
OUTPUT:
specific-message
4
admin
----------------------------------
When I do this, I only get one row output..and I can't seem to find out why. I want the above out put to repeat as many times as it has rows, and it's only doing one row (with the account fetch in the code). However when I do it without the internal fetch (accounts fetch), it returns multiple rows just as desired. Why is this? (below is sample code WITHOUT the accounts fetch):
$sql = "SELECT * FROM jobs";
$query = mysqli_query($db_conx, $sql);
while($row = mysqli_fetch_assoc($query)){
echo $row['action'];
echo "<br/>";
$job_poster_id = $row['id_assoc'];
echo $job_poster_id ;
echo "<br/>";
echo "----------------------------------";
echo "<br/>";
}
OUTPUT:
specific-message
4
----------------------------------
specific-message
1
----------------------------------
specific-message
2
----------------------------------
$query = mysqli_query($db_conx, $sql);
while($row = mysqli_fetch_assoc($query)){
echo $row['action'];
echo "<br/>";
$job_poster_id = $row['id_assoc'];
$sql = "SELECT * FROM accounts WHERE id_assoc='$job_poster_id'";
$query = mysqli_query($db_conx, $sql);
The problem is that you're using $query for the inner and the outer query.
When the inner query runs, and it steps through the loop, it's iterating to the end of the result set; when the outer while loop runs, mysqli_fetch_assoc($query) is returning false, because you're already at the end of the result set - just not the result set you were expecting.
You can fix this by renaming one of the $query variables.
I'm trying to get a single result from my database, just one name.
I tried using;
$row = mysql_fetch_array(mysql_query("SELECT * FROM persons WHERE id = '$id'"));
echo $row['name'];
But that din't work, any other way to simply show only one result?
Thanks in advance!
[EDIT:]
(I'm using PHP 5.3)
<?php
include("connection.php");
$id = $_GET['deletid'];
$result = mysql_query("SELECT * FROM persons WHERE id = '$id' LIMIT 1");
if(!$result){
echo mysql_error();
}
if ($row = mysql_fetch_array($result)){
echo $row['name'];
}
echo "<p>id:$id</p>";
?>
If you need just the name and you need just one result you should rewrite your query as follow:
$row = mysql_fetch_array(mysql_query("SELECT name FROM persons WHERE id = '". (int) $id ."' LIMIT 1"));
Now to get the result you should just get it with a
$row['name'];
EDIT
Now that you posted your entire code i got what's wrong: You are deleting that result before getting its name. Basically you delete that user and then you attempt to get its name.
EDIT
<?php
include("connection.php");
if (empty($_GET['deleteid'])) {
exit('"deleteid" is empty');
}
$id = mysql_real_escape_string($_GET['deletid']);
$result = mysql_query("SELECT name FROM persons WHERE id = '". (int) $id ."' LIMIT 1");
if(!$result){
echo mysql_error();
}
$row = mysql_fetch_assoc($result); // for just one result you don't need of any loop
echo $row['name'];
echo "<p>id:". htmlspecialchars($id) ."</p>";
?>
try
$row = mysql_fetch_array(mysql_query("SELECT name FROM persons WHERE id = ". (int) $id));
echo $row['name'];