I'm having trouble imploding a MySQL database array, below is a sample of the code I am using:
mysql_select_db($database_wlast, $wlast);
$query_category = "SELECT product_name FROM raw_materials WHERE category = '$_POST[category]'";
$category = mysql_query($query_category, $wlast) or die(mysql_error());
$row_category = mysql_fetch_array($category);
$result= implode(',',$row_category);
echo $result;
The result is the following:
Amber glass bottle 100ml, Amber glass bottle 100ml
i.e: it is spitting out the first value in the array twice and nothing else.
Please help!
while($row_category = mysql_fetch_array($category)){
$result[] = implode(',',$row_category);
}
echo implode("\n",$result);
That's how mysql_fetch_array() returns the result, twice: a numeric key and a string key. You possibly want mysql_fetch_assoc().
You want to implode all your product name from your database? if yes, than your code above is wrong.
mysql_fetch_array($category) only return 1 row of your database. If you want to implode of your all product name than code should like this:
$result="";
while($row_category=mysql_fetch_array($category))
$result=$result.",".$row[0];
or you some thing like this. You hold all your product name on array and implode it. Like this:
while($row_category=mysql_fetch_array($category))
$result[]=$row[0];
$newResult=implode(",",$result);
use mysql_fetch_array($category, MYSQL_NUM) or mysql_fetch_row($category) or mysql_fetch_assoc($category)
The problem is that by default mysql_fetch_array returns enumerated and associative arrays combined.
Related
im new in field of manipulating databases so i have a problem.
So after making a query to select all values from a table row with something like :
while($row = $result->fetch_assoc()) { }
For example if table has names in it we would get :
John
Bob
Steve
Mark
If we put these results in a form of a link or a button, is there any way to get their value?
i tried something like :
if (isset($_POST['result_button'])) {
$value =$row['name'];
}
I tried this when i put all the results from query in to a form of button, but that only pulls the last value from table row.
I tried putting results into array like :
$result= array();
$result[]=$row['name'];
echo $result[2];
I don't fully understand how arrays works so might have done something wrong there but from what i tried this works only if i put echo$result[0];
Ok, firstly:
$row = $result->fetch_assoc()
This does not work how you're expecting it to. What this is basically saying, is that for each returned value (from the MySQL query), store in $row for this iteration of the while loop.
So, in you're example, $row will first be John, then Bob, then Steve and finally Mark. They are not all stored in the variable at the same time. Bob overwrites John, etc.
I'm going to skip your second bit of code, as it's clear you do not have an understanding of arrays.
Arrays
There is plenty of material about arrays on the web. I'm not sure why you had to post the question here, however...
To define an array:
$my_array = array();
An array with values:
$my_array = array(
"bar",
"foo"
);
Assign variables to an array:
$my_array = array();
$my_array[0] = "Help" // $my_array[0] now contains "Help"
$my_array[1] = "Me"; // $my_array[0] now contains "Me"
So, taking you're final example:
$result= array();
$result[]=$row['name'];
echo $result[2];
You haven't stored anything in the 2nd element of the array.
This would be more appropriate:
$result= array();
$result[0] = $row['name'];
echo $result[0]; // This would print what was stored in $row['name']
I´m new to PHP and SQL and it´s the first time I post here, hope I do it right =)
I need to find out the numeric index from an certain field in my database. Thing is I´m not done with the database structure yet, so for this specific field I´d like to have it automated so I won't have to change the code every time I add or remove columns.
I also need it to have BOTH index types so I can call the columns by name in part of the code and by index for automated parts of it.
I´ve managed to achieve the result I wanted trough an very ugly code, because all the array_search and array_keys I´ve tried didn´t work out for some reason.
Here is the snippet of my working but ugly code. Do you have an more elegant solution?
Thanks in advance for your answers, and for all I´ve learned so far from reading other peoples questions!
$dadosRes = mysqli_query($con, "SELECT * FROM Alunos WHERE Id=1");
$student = mysqli_fetch_array($dadosRes) or die(mysql_error());
$c = 0; // This block is to find out where the column "cont1" is and assign it to $cont1
$d=array_keys($student);
$cont1=0;
foreach ($student as $a=>$b){
if ($a==='cont1') {
$cont1=$d[$c-1];
}
$c++;
}
for ($i=$cont1; $i<$cont1+12; $i+=3) { // Displays the students contacts, each in up to 3 rows
if ($student[$i]) {
echo "<p><ul><li>".$student[$i]."</li>";
if ($student[$i+1]) echo "<li>".$students[$i+1]."</li>";
if ($student[$i+2]) echo "<li>".$students[$i+2]."</li>";
echo "</ul></p>";
}
}
Finding the numeric index:
$student = mysqli_fetch_array($dadosRes,MYSQLI_ASSOC) or die(mysql_error());
$numericIndex = array_search("cont1",array_keys($student));
Giving MYSQLI_ASSOC will force the query to fetch only the associative keys; otherwise it will give you both numeric and associative array.
PHP function, array_search in this case, requires the third parameter set to TRUE:
<?php
$dadosRes = mysqli_query($con, "SELECT * FROM Alunos WHERE Id=1");
$student = mysqli_fetch_array($dadosRes, MYSQLI_BOTH) or die(mysql_error());
$student_keys = array_keys($student);
$cont1 = $student_keys[array_search("cont1", $student_keys, true) - 1];
for ($i=$cont1; $i<$cont1+12; $i+=3) { // Displays the students contacts, each in up to 3 rows
if ($student[$i]) {
echo "<p><ul><li>".$student[$i]."</li>";
if ($student[$i+1]) echo "<li>".$student[$i+1]."</li>";
if ($student[$i+2]) echo "<li>".$student[$i+2]."</li>";
echo "</ul></p>";
}
}
?>
MYSQLI_BOTH is used in the mysqli_fetch_array function because first the array is searched for the column name, then later the array is looped over with numeric indexes. This results in the $student array having integer 0 as the first element and therefore causing array_search to choke. Adding true as the third parameter causes a === comparison unchoking the function. :) (Explanation: PHP array_search consistently returns first key of array )
I want to explode an array, read each value and print them back in an array...
I dont understand where i am getting wrong. Please help me..this is my code..
I am getting an array to string conversion error
$query="SELECT categories FROM shops";
$result = mysql_query($query);
while($column = mysql_fetch_assoc($result)){
$categories=explode(",",$column['categories']);
foreach($categories as $value){
$new_query="SELECT name from categories where id='$value'";
$name = mysql_query($new_query);
$name_column= mysql_fetch_assoc($name);
array_push($shops_list,$name_column);
}
}
echo implode(",",$shops_list);
$shop_list is not defined, before using it in this line array_push($shops_list,$name_column);. And, this line
array_push($shops_list,$name_column);
needs to be, as you need to mention the key name,
array_push($shops_list,$name_column['name']); //or better
$shop_list[] = $name_column['name'];
Several issues:
$name_column = mysql_fetch_assoc($name);
$name_column = $name_column['name'];
name_column is an array.
shops_list is never initialized.
You should use [] instead of array_push.
The other guys hit it on the nose, but when you did your array push on $name_column, since $name_column is an array, you end up with:
Array
(
[0] => Array
(
[name] => boo
)
)
Obviously doing an implode on that is going to not work.
That being said, what you really need to do here is not keep your category mappings as a comma delimited string in the database. Standard DB architecture dictates you use a mapping table.
Table shops
Table categories
Table shop_category_map that has shop_id and category_id
use group_concat to retrieve values. and after getting the result, use them directly for searching. like
$result_array = explode(",",$row['category']);
foreach($result_array as $ra)
{
//sql command. fetch here.
$new_query="SELECT name from categories where id='$value'";
$name = mysql_query($new_query);
$name_column= mysql_fetch_assoc($name);
$shops_list[] = $name_column;
}
try else go for better solution
// explode an array and then implode until a particular index of an array
$a = '192.168.3.250';
$b = explode('.',$a);
$ar = array();
for($i=0;$i<=2;$i++)
{
array_push($ar,$b[$i]);
}
$C = implode($ar,'.');
print_r($C);
I have this code.
<?php
require('connection/conn.php');
mysql_select_db($db_name,$ligação);
//$rsArticle = mysql_query("CALL get_article(1,518)");
$rsArticle = mysql_query("SELECT * FROM tblarticles WHERE ArticleID = 518");
while($rowArticle = mysql_fetch_array($rsArticle)){
echo $rowArticle;
}
?>
And instead of getting the text that exists in the database I just get the word: Array
The line that is commented its for calling a stored procedure. In a desperate measure I made a simple select, in the next line
Can anyone explain me what I'm doing wrong??
Thanks
The reason that you are getting the word Array is because what you are echoing is an Array. Use something like echo $rowArticle['column-name']; to echo the data from a specific column of your query.
You can't echo an array. try print_r() instead
if you want the values individually, do something like this:
while ($row = mysql_fetch_array($rsArticle, MYSQL_NUM)) {
//echo the first column of the record (index 0)
echo $row[0];
}
look in the php.net documentation for more info
mysql_fetch_array returns an array with elements for every field in your database table.
Either print the whole array with print_r() or use echo $rowArticle[COLOUMN_NAME] to echo certain values from your resultset.
mysql_fetch_array() returns you the array, when you use echo is actually return the object type in case of array.
use print_r($rowArticle); instead of echo.
I'm a bit stuck on this one. In my PHP code, I query for some records which are returned. I loop through the rows, but for each row I want to retrieve a column value using its name as an index.
while ($row=mysql_fetch_row($result))
{
echo $row[7];
}
This prints out what I want, but I wanted to do something like:
echo $row["description"];
Where description is the name of the column whose value I want to print? Can this be done?
Thank you very much.
You could also use this:
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
Then you can switch back to numeric with MYSQL_NUM or both with MYSQL_BOTH
You need to use mysql_fetch_assoc to get the associative array.
You want to use the mysql_fetch_assoc function instead.