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.
Related
$sql = "SELECT temail FROM teacherusers WHERE tfullname='$teachername' limit 1";
$result = mysql_query($sql);
$value = mysql_fetch_object($result);
$teacheremail2 = $value->temail;
echo $teacheremail2;
echo $teacheremail2 returns nothing.
$teachername is valid and i have checked multiple times.
It should be a two-dimensional array , you need
$value[0]->temail
The result of mysql_fetch_object($result) is an object(stdClass).
The explanation of object(stdClass) ican be found at this link
$sql = "SELECT temail FROM teacherusers WHERE tfullname='$teachername' limit 1";
$result = mysql_query($sql);
while ($value = mysql_fetch_object($result))
{
$teacheremail2 = $value->temail;
echo $teacheremail2;
}
First off, you'll want to run the query directly against your database to ensure that the query returns some kind of result.
Secondly, if that works, you'll want to echo $value directly to check that you are getting results back on the webpage.
Then you can check if temail is a field of $value
$sql = "SELECT temail FROM teacherusers WHERE tfullname='$teachername' limit 1";
$result = mysql_query($sql);
while ($value = mysql_fetch_object($result))
{
$teacheremail2 = $value->temail;
echo $teacheremail2;
}
hope this help
hi I have modules as a links in student page so after click on any one of these links i want to pass the id of the Specific module to the next page,so how can i pass the id value and this is my code, please help.
$sql = "SELECT * FROM MODULE,USER where user_access_level = module_level AND '$user_id' = user_login ";
$result = $db->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo '<li>'.$row["module_id"].' '.$row["module_name"].' '.$row["module_points"].'</li> ';
}
}
Here is your code with all of the quotes, concatenations, backticks and backward logic corrected:
$sql = "SELECT * FROM `MODULE`, `USER` WHERE `user_access_level` = 'module_level' AND `user_login` = '". $user_id."' ";
$result = $db->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo '<li>'.$row["module_id"].' '.$row['module_name'].' '.$row['module_points'].'</li> ';
}
}
I would bet that module_level is also supposed to be a variable. $G looks problematic, what sets the array $G? You should replace the $G with $row if I am reading this correctly.
...AND user_id = '".mysqli_real_escape_string ($user_login)."'";
I assume?
Anyway, DB_field = $param. And maybe mysql_, not mysqli_, but hope you check the user input before you pass it to the query...
Check the syntax error:
echo '<li>' . $row["module_id"] . ' ' . $row["module_name"] . ' ' . $row["module_points"] . '</li>';
This is my code below
$query = mysql_query("SELECT * FROM table ORDER BY id LIMIT 10");
while($f = mysql_fetch_array($query)){
$match = 0; //In reality it's an array search function which returns 1 on match
if($match == 1) {
echo"Show content!";
}
}
Im trying to make an list with 10 rows, and i have a function which uses "name" from table to run an search query with an array generated by twitter API. In example if i get 3 matching records, i still want to show a list with 10 rows but hide the matching elements from there.
At the moment the script hides the matching elements and shows 7 rows instead of 10.
That is what i need help with, cheers :)
Process the data you are getting from the twitter api, collect the data you need in an array and query the database afterwards.
Something like that:
<?php
$collectArray = array();
foreach ($twitterData as $index => $data) {
if ($someCriteria === TRUE) {
$collectArray[] = $data;
}
}
$implodedCollectArray = "'" . implode("', '", $collectArray) . "'";
$sql = "SELECT * FROM `table_name` WHERE `some_column` IN (" . $implodedCollectArray . ")";
$query = mysql_query($sql) or die(mysql_error());
while($f = mysql_fetch_array($query)){
echo $f['column'];
}
?>
reinitilize match variable as the code:
$query = mysql_query("SELECT * FROM table ORDER BY id LIMIT 10");
while($f = mysql_fetch_array($query)){
if($f['name']!='variable_name_twiter'){
$match = 1; //In reality it's an array search function which returns 1 on match
}
if($match == 1) {
echo"Show content!";
}
$match = 0;
}
So what I want to do is I want to search for a certain name in a column called 'filename' and display the 'count' on that row.
This might help explain:
I want to search for Packages in the 'filename' column but I want to display the 'count' on it (4)
id filename count
1 Packages 4
2 Another 7
$filename = 'Packages';
$result = mysql_query("SELECT `count` FROM `downloads` WHERE `filename` = $filename");
Also note that if the filename can come from user input you have to be aware of injection attacks.
$filename = 'Packages'; // From user
$filename = mysql_real_escape_string($filename);
$result = mysql_query("SELECT `count` FROM `downloads` WHERE `filename` = $filename");
Here is an example that selects all Filenames and counts and lists them in a table:
$result = mysql_query("SELECT `filename` `count` FROM `downloads`");
?><table><tr><td>Filename</td><td>Count</td></tr><?php
while($row = mysql_fetch_assoc($result)){
?>
<tr><td><?php echo $row['filename']; ?></td><td><?php echo $row['count']; ?></td></tr>
<?php
}
?></table><?php
Assuming what you actually wants is a report of download counts for each filename, you need to use the GROUP BY clause in your SQL statement:
<?php
$res = mysql_query('SELECT id, filename, COUNT(*) AS count FROM downloads GROUP BY filename');
if (is_resource($res)) {
while (false !== ($row = mysql_fetch_assoc($res))) {
printf('%d - %s - %d', $row['id'], $row['filename'], $row['count']);
}
}
?>
$query = "SELECT count FROM downloads WHERE filename='Packages'";
EDIT:
$result = mysql_query($query, $con) or die(mysql_error());
$arr_down = mysql_fetch_assoc($result);
echo $arr_down["count"];
UPDATE: For one or more rows with 'Packages' in it, you can show the results in a table like this:
$con = mysql_connect("localhost", "MySQL_username", "MySQL_password") or die ("Unable to connect to MySQL Server " . mysql_error());
$fname = "Packages";
$query = "SELECT filename, count FROM downloads WHERE filename='" . $fname . "'";
$result = mysql_query($query, $con) or die(mysql_error());
$html = "<table>\n<tr><th>Filename</th><th>Count</th></tr>\n";
while ($row = mysql_fetch_assoc($result))
$html .= "<tr><td>" . $row['filename'] . "</td><td>" . $row['count'] . "</td></tr>\n";
$html .= "</table>\n";
echo $html;
$count = mysql_num_rows($result)
echo($count)
Check out the PHP mysql documentation for more. In particular you'll want to learn about the fetch() functions, which you'll use to display the data you retrieve with your query.
the raw query would be:
select count( filename ) from downloads;
Edit: Okay, you want to loop them onto the page:
foreach( $result as $i )
{
if( !empty( $i ) )
{
echo "<div>$i['column_name']</div>";
}
}
I'm using a multiple select option form to get a table of venues. Each venue has an ID and this is what I used:
<?php
require("db_access.php");
if(isset($_POST['select3']))
{
$aVenues = $_POST['select3'];
if(!isset($aVenues))
{
echo("<p>You didn't select any venues!</p>\n");
}
else
{
$nVenues = count($aVenues);
echo("<p>You selected $nVenues venues: ");
for($i=0; $i < $nVenues; $i++)
{
echo($aVenues[$i] . " ");
}
echo("</p>");
$sql = "SELECT * FROM venues WHERE id IN (" . implode(",",$aVenues) . ")";
$comma_separated = implode(",", $aVenues);
echo $comma_separated;
}
}
?>
It results in this:
However I thought that the code would use those two numbers below and draw out a table with those id's I used :/ ? Am I missing something?
$array is used in implode(",", $array); but is not defined anywhere else that we can see. It is perhaps intended to be:
implode(",", $aVenues);
UPDATE
Per comments, it does not draw a table because you never actually query your database.
You build your SQL statement, but you need to execute it and fetch the result set.
// Make sure you actually have a database connection
$conn = mysql_connect('localhost', $username, $password);
mysql_select_db($database);
$sql = "SELECT * FROM venues WHERE id IN (" . implode(",",$aVenues) . ")";
$comma_separated = implode(",", $array);
echo $comma_separated;
// Execute query and fetch result rowset
$result = mysql_query($sql);
if ($result) {
$rowset = array();
while ($row = mysql_fetch_array($result)) {
$rowset[] = $row;
}
var_dump($rowset);
}
else echo mysql_error();