I am adding all names in to single variable but it is showing only one value last one.
my code is:
include 'dbconnect.php';
$result = mysql_query("SELECT * FROM bookedtates WHERE SID='$ServiceHosterIdv' AND BOOKEDDATE='$q'");
//$result = mysql_query("SELECT * FROM bookedtates WHERE SID='$ServiceHosterIdv' AND BOOKEDDATE='$q'");
while ($row = mysql_fetch_assoc($query)) {
$csk = "'".$row['NAME']."',";
}
echo $csk;
No u just assign variable use it to plus add a "." before equtaion
$csk .= "'".$row['NAME']."',";
But I would suggest to use array so u can use for JS(if ajax) or php for more flexible things
$csk = array();
while ($row = mysql_fetch_assoc($query)) {
$csk[] = array($row['NAME']);
}
echo $csk; //for ajax use echo json_encode($csk);
just test with
include 'dbconnect.php';
$result = mysql_query("SELECT * FROM bookedtates WHERE SID='$ServiceHosterIdv' AND BOOKEDDATE='$q'");
//$result = mysql_query("SELECT * FROM bookedtates WHERE SID='$ServiceHosterIdv' AND BOOKEDDATE='$q'");
$csk = '';
while ($row = mysql_fetch_assoc($query)) {
$csk .= "'".$row['NAME']."',";
}
echo $csk;
You are resetting the variable to the value of $row['NAME'] on each iteration of the loop.
What you need to do is append the variable to the end of $csk:
$csk .= "'".$row['NAME']."',";
^---- notice the extra . here
The extra . indicates that you want to append the value to $csk.
Related
I need to print out all the usernames of my members in my site as a list, but I don't know how to do it. I tried to use 'mysqli_fetch_array' function to fetch names from username column, but I've been failing. So, please assist me on how I can do this in a most efficient way.
This is my code
$sql = "SELECT username FROM users";
$query = mysqli_query($db_con, $sql);
$array = Array;
while($row = mysqli_fetch_array($query)) {
$array = row[];
}
foreach ($names as $array) {
echo $names;
}
Also I want to format the output, like separating each username by " | "
You are quite close..
$sql = "SELECT username FROM users";
$query = mysqli_query($db_con, $sql);
while($row = mysqli_fetch_assoc($query)) {
echo $row['username'].'|';
}
Couple of things, "Array" should be "array()" and you're doubling up, by assigning array values into an array, results are returned in an array already. Code below should work...
$sql = "SELECT username FROM users";
$query = mysqli_query($db_con, $sql);
$array = mysqli_fetch_array($query));
foreach ($array as $name) {
echo $name["username"] . "|";
}
Looks like you also had your foreach loop round the wrong way
no need to use extra foreach, just do this way-
$sql = "SELECT username FROM users";
$query = mysqli_query($db_con, $sql);
$array = array();
while($row = mysqli_fetch_assoc($query)) {
echo $row["username"] . "|";
}
Don't worry it is really very easy
<?php
//All your preceding code
$sql = "SELECT username FROM users";
$query = mysqli_query($db_con, $sql);
?>
<ul>
<?php
while($row = mysqli_fetch_array($query)) {
echo "<li>" . $row['username'] . "</li>";
}
?>
</ul>
I have a database which looks like so -
I am trying to fetch the top 10 entries based on time (entries with top 10 values in time column). I have the following code.
<?php
include_once("connect.php");
$sql = "SELECT * FROM scores order by time desc limit 10";
$query = mysql_query($sql) or die("systemResult=Error");
$counter = mysql_num_rows($query);
if($counter>0)
{
print("systemResult=Success");
$array = mysql_fetch_array($query);
foreach($array as $data)
{
$athleteName = $data["athleteName"];
$email = $data["email"];
$time = $data["time"];
$timeStamp = $data["timeStamp"];
$country = $data["country"];
print "&athleteName=" . $athleteName;
print "&email=" . $email;
print "&time=".$time;
print "&timeStamp=".$timeStamp;
print "&country=".$country;
}
}
else
{
print("systemResult=Error");
}
?>
The output I am getting is
systemResult=Success&athleteName=7&email=7&time=7&timeStamp=7&country=7&athleteName=7&email=7&time=7&timeStamp=7&country=7&athleteName=4&email=4&time=4&timeStamp=4&country=4&athleteName=4&email=4&time=4&timeStamp=4&country=4&athleteName=G&email=G&time=G&timeStamp=G&country=G&athleteName=G&email=G&time=G&timeStamp=G&country=G&athleteName=n&email=n&time=n&timeStamp=n&country=n&athleteName=n&email=n&time=n&timeStamp=n&country=n&athleteName=2&email=2&time=2&timeStamp=2&country=2&athleteName=2&email=2&time=2&timeStamp=2&country=2&athleteName=I&email=I&time=I&timeStamp=I&country=I&athleteName=I&email=I&time=I&timeStamp=I&country=I
As can be seen, the output I am getting is not what is on the table in database. I am getting wierd values. What am I doing wrong?
You don't need to use for each in your case, and if so, just print $data, try to remove foreach loop, and if you want to get all records, then, use while:
while($data = mysql_fetch_array($query))
{
$athleteName = $data["athleteName"];
$email = $data["email"];
$time = $data["time"];
$timeStamp = $data["timeStamp"];
$country = $data["country"];
print "&athleteName=" . $athleteName;
print "&email=" . $email;
print "&time=".$time;
print "&timeStamp=".$timeStamp;
print "&country=".$country;
}
try
while($data = mysql_fetch_array($query)) {
$athleteName = $data["athleteName"];
//...
i noticed that there are a lot of topics like this in stackoverflow, but the ones that i read are with just 1 variable, and i have two and i'm a little confuse.
so this is my code,
$query = mysql_query("SELECT Provider.provider_id, provider.company_name FROM Provider");
while($row = mysql_fetch_array($query)){
echo "'".$row['provider_id']."':'".$row['company_name']."',";
}
I am using this to generate javascript code for Jeditable.
I need to take out the last comma, so I can wrap up with the rest of the code...
I like to use an array:
$query = ...;
$out = Array();
while($row = mysql_fetch_assoc($query)) {
$out[] = "'".$row['provider_id']."':'".$row['company_name']."'";
}
echo implode(",",$out);
It looks like you're trying to output some kind of JSON though, so perhaps you could use:
$query = ...;
$out = Array();
while($row = mysql_fetch_assoc($query)) {
$out[$row['provider_id']] = $row['company_name'];
}
echo json_encode($out);
So i usually just stack things like this into an array and then implode:
$parts = array();
while($row = mysql_fetch_array($query)){
$parts[] = "'".$row['provider_id']."':'".$row['company_name']."'";
}
echo implode(',', $parts);
That said it looks like youre outputting the body of a JSON string. If so you shouldnt manually build it you should use json_encode instead.
$data = array();
while($row = mysql_fetch_array($query)){
$data[$row['provider_id']] = $row['company_name'];
}
echo json_encode($data);
$query = mysql_query("SELECT Provider.provider_id, provider.company_name FROM Provider");
$result = "";
while($row = mysql_fetch_array($query)){
$result .= "'".$row['provider_id']."':'".$row['company_name']."',";
}
$result = rtrim($result, ',');
OR
$query = mysql_query("SELECT Provider.provider_id, provider.company_name FROM Provider");
$result = array;
while($row = mysql_fetch_array($query)){
$result[] = "'".$row['provider_id']."':'".$row['company_name']."',";
}
$result = implode(',' $result);
OR
get count of records (mysql_num_rows($query)) and when you hit last row just do what you want
I have a collection of results from a MySQL query being fetched in a loop.
I need to store them as a single variable with a space in between each result.
$result = mysql_query("SELECT Names FROM table");
while($row = mysql_fetch_array($result)){
echo $row['Names'] . " ";
}
So later, I can call 'echo $Names;' and get the string of names with a space in between.
ex) Clinton Bush Huckabee Romney etc....
Thanks for your help!
$Names = '';
$result = mysql_query("SELECT Names FROM table");
while($row = mysql_fetch_array($result)){
$Names.= $row['Names'] . " ";
}
$result = mysql_query("SELECT Names FROM table");
$names = "";
while($row = mysql_fetch_array($result)){
$names .= $row['Names'] . " ";
}
echo $names;
First of all, please, stop using mysql extention. It's deprecated. Try mysqli or PDO.
$names = array();
$result = mysql_query("SELECT Names FROM table");
while($row = mysql_fetch_array($result)){
$names[] = $row['Names'];
}
echo implode(' ', $names);
I would use an array and then implode it using a space:
$result = mysql_query("SELECT Names FROM table");
$arr = array();
while($row = mysql_fetch_array($result)) $arr[] = $row['Names'];
echo implode(' ', $arr);
$query = mysql_query("SELECT * FROM fruit WHERE color='red' ");
while ($row = mysql_fetch_array($query)) {
echo $row['name']."|" ;// this echo apple|strawberry|cherry
}
How to make a php variable equal to the result of mysql_fetch_array? I need to make a equal like: $newname="apple|strawberry|cherry", so that I can use the new variable $newname for another process. Thanks.
Rather than echo, concatenate:
$newname = '';
while ($row = mysql_fetch_array($query)) {
$newname .= $row['name']."|" ;// this echo apple|strawberry|cherry
}
$newname = rtrim($newname,'|');
$newname .= $row['name']."|" ;
$query = mysql_query("SELECT * FROM fruit WHERE color='red' ");
$newname= '';
while ($row = mysql_fetch_array($query)) {
$newname.= $row['name']."|" ;
}
This gives you a string $newname like you want.
I know everyone else went with string concatenation, but I would personally suggest an array push followed by a join - I haven't benchmarked it, but I believe it would perform slightly better.
while($row = mysql_fetch_array($query)) {
$newname[] = $row['name'];
}
echo join('|', $newname);
i use this in my joomla :
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('price');
$query->from($db->quoteName('#__google_api'));
$query->where($db->quoteName('id')." = ".$db->quote('1'));
// Reset the query using our newly populated query object.
$db->setQuery($query);
$db->setQuery($query);
$price = $db->loadResult();
Fetches data for a single colume and stores it in $price