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;
}
?>
Related
categoryID
10
20
30
For example. Above is my categoryID column with values of 10, 20, and 30 PHP MySQL. What I want to do is to echo those values using an array. Like -> 10 20 30. Below is my code. In my code. I just stored all row data from categoryID into my $array variable. My concern is. How do I echo all the values?
Thank you for the help!
<?php
include ("dbconnect.php");
$sql = "SELECT categoryID FROM post";
$result = mysqli_query($con, $sql);
$array = array();
while($row = mysqli_fetch_assoc($result)) {
$array[] = $row;
}
?>
Use $array variable as below:-
$array[] = $row['categoryID'];
and to print the array try:-
print_r($array);
and to print them as 10 20 30 try the following:-
echo implode(' ',$array);
The best way is to use json_encode() function
echo json_encode($array);
so try this
<?php
include ("dbconnect.php");
$sql = "SELECT categoryID FROM post";
$result = mysqli_query($con, $sql);
$array = array();
while($row = mysqli_fetch_assoc($result)) {
$array[] = $row;
}
echo json_encode($array);
exit;
?>
How can i count items qho had the same name in a mysql table with php , i tried this but he always print the only first one but i have 5items with the same Custumer name.. this my code..
<?php
include ('Connection.php');
$con= mysqli_connect($host,$user,$pass,$db);
if($_SERVER['REQUEST_METHOD']=='GET'){
$Codigo=$_GET['nbCliente'];
$sql = "
SELECT $Codigo
, COUNT(*)
FROM pedidos
GROUP
BY nbCliente = '$Codigo'
";
$r = mysqli_query($con,$sql);
$res = mysqli_fetch_array($r);
$result = array();
array_push($result,array(
"codigo"=>$res['Codigo'],
"articulos"=>$res['articulos'],
"precio"=>$res['precio'],
"cantidad"=>$res['cantidad'],
"Nomb_Cliente"=>$res['nbCliente'],
"NombVende"=>$res['nbvendedor'],
"image"=>$res['image_path']
)
);
echo json_encode(array("result"=>$result));
mysqli_close($con);
}
I think the query you are looking for should look like this:
select nbCliente, count(nbCliente) from pedidos where nbCliente = $Codigo group by nbCliente
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.
When I try to mysql_fetch_row the array that is created contains 2 fields from my selection at each index. I would like to ask why is this happening?
<?php
$categoryid = $_GET['id'];
include('connect.php');
$query = "SELECT
Categories_SubCategories.IdCategory,Categories_SubCategories.idSub_Category,
Categories.Name, Sub_Categories.Name from Categories_SubCategories JOIN
Categories on Categories.idCategory = Categories_SubCategories.idCategory
JOIN Sub_Categories on Categories_SubCategories.idSub_Category =
Sub_Categories.idSub_Category
WHERE Categories_SubCategories.IdCategory = $categoryid";
$result = mysql_query($query);
$rows = mysql_num_rows($result);
for($i=0; $i<$rows; $i++){
$display = mysql_fetch_row($result);
echo "$display[3]";
}
?>
most PHP programmers use while() loop when they want to work with mysql_fetch_array().
Take a look at this sample code:
$query = mysql_query("SELECT id,name FROM tbl_members");
if (mysql_num_rows($query)) {
while ($result = mysql_fetch_array($query)) {
echo('User #'.$result['id'].' is: '.$result['name'].'<br />');
}
}
// Output can be something like this:
// User #1 is: John
// User #2 is: Sarah
replace mysql_num_rows with mysql_fetch_array or mysql_fetch_assoc
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'].",";
}