Imagine I had an "id" column and each of its rows contained a number. Lets say I have 3 rows at the moment. row 1 contains 1111, row 2 contains 2222, row 3 contains 3333.
I want to get the row values into a variable, and separate each row's data by a comma. The final result I expect is $variable = 1111,2222,3333.
I got this far code-wise:
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("streamlist") or die(mysql_error());
$sql = mysql_query("SELECT web_id FROM streams") or die(mysql_error());
while($row = mysql_fetch_array($sql)) {
$streamlist = $row['web_id'].",";
echo $streamlist;
}
?>
The problem is that I then need each row's information singularly:
<?php
$numbers = explode(',', $streamlist);
$firstpart = $numbers[0];
echo $firstpart;
?>
And the above code doesn't work inside the while() statement, nor does this:
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("streamlist") or die(mysql_error());
$sql = mysql_query("SELECT web_id FROM streams") or die(mysql_error());
while($row = mysql_fetch_array($sql)) {
$streamlist = $row['web_id'].",";
}
$numbers = explode(',', $streamlist);
$firstpart = $numbers[0];
echo $firstpart;
?>
So basically, how can I get all of the information of the rows into a variable, and make them seperated by commas, in order to then get each number singularly?
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("streamlist") or die(mysql_error());
$sql = mysql_query("SELECT web_id FROM streams") or die(mysql_error());
while($row = mysql_fetch_assoc($sql)) {
$streamlist[] = $row['web_id'];
}
foreach ($streamlist as $row) {
// Do whatever you want with the data
echo $row.',';
}
$comma_separated = implode(",", $streamlist);
You're on the right track, try this on for size:
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("streamlist") or die(mysql_error());
$sql = mysql_query("SELECT web_id, nextrow, nextrow2 FROM streams") or die(mysql_error());
while($row = mysql_fetch_array($sql, MYSQL_NUM)){
// $array_data will contain the array with all columns from the query (usable for your individual data needs as well)
$array_data[] = $row;
$var_string .= implode(",",$row);
// $var_string will contain your giant blob comma separated string
}
echo "<pre>"; print_r($array_data);echo "</pre>";
echo $var_string;
// These are for outputting the results
?>
The $streamlist scope doesn't seem right. Try:
$streamlist = '';
while($row = mysql_fetch_array($sql)) {
$streamlist .= $row['web_id'].",";
}
Related
How to get total number of row by using this function php mysql ?
i use this code for display data from mysql. It's work good,
Buy i want to know can i get total number of row by using this code ?
<?PHP
include("connect.php");
$query = "SELECT * FROM table";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
}
?>
Try this mysql_num_rows ($result)
Use this line of code
<?PHP
include("connect.php");
$query = "SELECT * FROM table";
$result = mysql_query($query) or die(mysql_error());
$number_of_rows = mysql_num_rows($result); // this will return the number of rows found by the $result query.
echo $number_of_rows;
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
}
?>
I'm having a problem when I try to use implode array in WHERE IN clause and display it. Some of the record in the database has an apostrophe in it.
Example the data in my database are testing,test'ing,tes't. And my code below is how I implode the data to be used in WHERE IN clause.
<?php
$arr = array();
$qry = mysqli_query($con,"SELECT sampleTxt FROM Table")or die(mysqli_error($con));
while(list($txt) = mysqli_fetch_row($qry)){
$t = mysqli_real_escape_string($con,$txt);
$arr[] = "'".$t."'";
}
$sampl = implode(',',$arr);
?>
And here is my sample code on how I used it on WHERE IN clause.
<?php
$qry2 = mysqli_query($con,"SELECT sampleTxt2 FROM Table2 WHERE sampleTxt IN (".$sampl.")")or die(mysqli_error($con));
while(list($txt2) = mysqli_fetch_row($qry2)){
echo $txt2;
}
?>
The output should be
testingtest'ingtes't
but instead the output is just the testing.
Check for below code
<?php
$arr = array();
$qry = mysqli_query($con,"SELECT sampleTxt FROM Table")or die(mysqli_error($con));
while(list($txt) = mysqli_fetch_row($qry)){
//$t = mysqli_real_escape_string($con,$txt);
$arr[] = '"'.$txt.'"';
}
$sampl = implode(',',$arr);
?>
And then
<?php
$qry2 = mysqli_query($con,'SELECT sampleTxt2 FROM Table2 WHERE sampleTxt IN ('.$sampl.')')or die(mysqli_error($con));
while(list($txt2) = mysqli_fetch_row($qry2)){
echo $txt2;
}
?>
Some of my rows have a "," comma as the initial character in the field. So I need to loop through, check if each row has the initial commma, remove it if it does, and update the row.
I am running the following code, which seems to go on an endless loop when the update is called.
When I am just echoing out the result at the end, everything looks fine in the browser. But on execution of the update line below the echo, it seems as if a single datum from the column "Tags" is being populated for every record, instead of just the rows that have the initial commma that I am removing.
Would love help :)
$query = mysql_query("SELECT Tags FROM products")
or die (mysql_error());
while ($row = mysql_fetch_assoc($query))
{
$str = $row['Tags'];
$initial = substr($str,0,1);
if ($initial = ",") {
$str = (ltrim($str, ','));
}
echo "result: " .$str . "<br/>";
$result = "UPDATE products SET Tags = '" .$str ."'";
mysql_query($result);
}
Thank you.
You should pass the particular row id to the one you're making changes to, by using a WHERE clause:
$query = mysql_query("SELECT Tags FROM products")
or die (mysql_error());
while ($row = mysql_fetch_assoc($query)) {
$str = $row['Tags'];
$initial = substr($str,0,1);
if ($initial == ",") {
// == not =
$str = (ltrim($str, ','));
}
$id = $row['id'];
echo "result: " .$str . "<br/>";
$result = "UPDATE products SET Tags = '$str' WHERE id = $id";
mysql_query($result);
}
By the way, if possible kindly change to the better extension which is mysqli or PDO instead.
You're if() statement has an error in it.
You're using one equal:
if($initial = ",") {
}
Instead of two for actual comparison:
if($initial == ",") {
}
Here is the complete code. Thank you everyone.
$query = mysql_query("SELECT ProductID, Tags FROM products")
or die (mysql_error());
while ($row = mysql_fetch_assoc($query)) {
$str = $row['Tags'];
$initial = substr($str,0,1);
if ($initial == ",") {
$str = (ltrim($str, ','));
$id = $row['ProductID'];
//echo $id . " ";
//echo $str . "<br/>";
$result = "UPDATE products SET Tags = '$str' WHERE ProductID = $id";
echo $result ."<br>";
mysql_query($result);
}
}
So grateful for the help. I will update to mysqlli also.
I'm attempting to populate a table in HTML with data from a DB. It's not working properly (a blank white page is displayed), but I can't find the source of the error.
<?php
$sql = "SELECT * FROM Orders";
$result = mysql_query($sql)or die(mysql_error());
echo "<table>";
while($row = mysql_fetch_array($result)){
$order_id = $row['orderID'];
$order_due = $row['order_due'];
$order_subject = $row['order_subject'];
$order_level = $row['order_level'];
$order_pages = $row['order_pages'];
$order_cost = $row['order_cost']);
echo "<tr><td>".$order_id."</td><td>".$order_due."</td><td>".$order_subject."</td><td>".$order_level."</td><td>".$order_pages."</td><td>".$order_cost."</td></tr>";
}
echo "</table>";
?>
$order_cost = $row['order_cost']);
you have an extra paranthesis
Also change this
while($row = mysql_fetch_array($result))
to
while($row = mysql_fetch_assoc($result))
I think you need to change third line to:
result = mysql_query($sql) or die(mysql_error());
(space before "or")
I'm new to using php and mysqp.
Using the code below, I am calling and echoing four names from a mysql database (in a 'names' column) without repeat.
However, what I'd like to do is assign a unique CSS id selector to each name.
The intent is to have four individually styled boxes around the page, each with a different name chosen randomly upon load.
What would be the best code to do this?
(Fyi I am using an xxamp installation.)
Thanks!
<?php
$sandbox = mysql_connect("localhost", "root", "password")
or die(mysql_error());
mysql_select_db("sandbox", $sandbox);
$sql = "SELECT * FROM names ORDER BY RAND() LIMIT 4";
$result = mysql_query($sql, $sandbox);
while ($row = mysql_fetch_array ($result)) {
$name1 = $row['Name'];
echo $name1 . '<br>' ;
}
Would this work for you:
<?php
$sandbox = mysql_connect("localhost", "root", "password")
or die(mysql_error());
mysql_select_db("sandbox", $sandbox);
$sql = "SELECT * FROM names ORDER BY RAND() GROUP BY Name LIMIT 4";
$result = mysql_query($sql, $sandbox);
$i = 0;
while ($row = mysql_fetch_array ($result)) {
echo '<div id=box"'.$i++.'">'.$row['Name'].'</div>';
}
This gives you an unique CSS selector using the primary key in your table?
This should work.
$sandbox = mysql_connect("localhost", "root", "password")
or die(mysql_error());
mysql_select_db("sandbox", $sandbox);
$sql = "SELECT * FROM names ORDER BY RAND() LIMIT 4";
$result = mysql_query($sql, $sandbox);
$i = 0;
while ($row = mysql_fetch_array ($result)) {
$id = $row['ID'];
echo '<div id=box"'.$i++.'">your content</div>';
}