I'm Facing problem in adding variable or row from database into $_SESSION.
Here i'm fetch rows from database and storing it into $_SESSION with the help of array. But i'm not able to update/add/delete row quantity in $_SESSION[].
I will be very thankful if you tell me what is the proper way of adding arrays to $_SESSION[] and how to update/delete its particular row only in $_SESSION[]. When i use in_array() it only finds the last row values. when i try to find values of 2nd or 3rd row it says not found.
Note : I have searched in stackoverflow for this question. but those answere not come close to my question. Here are those links below link1 ,link2, link3
Here is my database table :
my Output :
PHP page :
<?php
$link = new mysqli('localhost','root','admin','pavancart');
if ($link->connect_error){
die("Connection failed: ".$link->connect_error);
}
$sql = "SELECT * FROM product";
$res= $link->query($sql);
while($row = $res->fetch_assoc()){
$_SESSION['item']=$row;
echo '<pre>';
echo print_r ($_SESSION["item"],true);
echo '</pre>';
}
if(in_array("1",$_SESSION['item'])){
echo "found";
}else{
echo "Not found";
}
?>
Your while loop is fine but $_SESSION['item'] is replaced by new row every time so, in_array("1",$_SESSION['item']) statement after the loop always looks in last item fetched by while loop. If you want to check in every row put if ... else statement inside while loop,
if you want to put all records in $_SESSION['item'], you could not accomplish like this, you should first declare $_SESSION['item'] as an array then push each row in it... like-
// declare $_SESSION['item'] as an array
$_SESSION['item'] = [];
while($row = $res->fetch_assoc()){
// Now push each row in $_SESSION['item']
// ........... place your if ........ else block here
array_push($_SESSION['item'],$row);
}
echo '<pre>';
// now you will get $_SESSION["item"] in 2D array
echo print_r ($_SESSION["item"],true);
echo "</pre>";
If you want to make search functionality over 2D array please Read This
Related
I have echoed the first row value of a column with success. Now I was thinking if there is a way to echo the second and the third values separately .
Here is the working code
...
sql="SELECT `semester` FROM `$course`";
$retval=mysqli_query($link,$sql) ;
$row=mysqli_fetch_array($retval) ;
echo $row[0];
....
I tried to index the mysql array so that I can echo with indices but its not working.
Just want to echo the values separately for js manipulation
Guys I found the solution.
Just define a new variable and access the $result with
MYSQLI_FETCH_ROW which always returns the next row in the query. Thanks and all the best
If you want to access each row separately using an index you need to first fetch all rows into PHP array.
$sql = "SELECT `semester` FROM `table`";
$retval = $link->query($sql);
$rows = $retval->fetch_all(MYSQLI_BOTH);
echo $rows[0]['semester']; // value from first row
echo $rows[2]['semester']; // value from third row
What seems to be happening is $result_tag is only holding the first row of the sql data retrieved.
When I run the result query on SQl in returns a table with multiple rows.
However, when I var_dump() it, it only returns the first row and nothing else.
while($row = $results->fetch_array(MYSQLI_BOTH)) {
echo ....stuff that dont matter
//Now I want some SQL results based on a result from ^^^ $row['ID']
$result = $conn->query("SELECT tags.Tag FROM tags WHERE tags.results_ID =".$row['ID'] );
$result_tag = $result->fetch_array(MYSQLI_NUM);
//I got the results. Now I want to compare them to another array '$explode'
//and echo out elements that are the same
foreach($explode as $explodeValue){
foreach($result_tag as $tag){
if($tag == $explodeValue){
echo $explodeValue;
}
}
}
}//end of 1st while
You want to use fetch_all(); fetch_array() returns just one row (as an array)
See http://php.net/manual/en/mysqli-result.fetch-all.php
This is in fact what the outermost while is doing, fetching one row at a time with fetch_array()
Change to
$result = $conn->query("SELECT * FROM tags WHERE results_ID =".$row['ID'] );
I'm probably missing something easy, but I seem to be blocked here... I have a MySQL database with two tables and each table has several rows. So the goal is to query the database and display the results in a table, so I start like so:
$query = "SELECT name, email, phone FROM users";
Then I have this PHP code:
$result = mysql_query($query);
Then, I use this to get array:
$row = mysql_fetch_array($result);
At this point, I thought I could simply loop through the $row array and display results in a table. I already have a function to do the looping and displaying of the table, but unfortunately the array seems to be incomplete before it even gets to the function.
To troubleshoot this I use this:
for ($i = 0; $i < count($row); $i++) {
echo $row[$i] . " ";
}
At this point, I only get the first row in the database, and there are 3 others that aren't displaying. Any assistance is much appreciated.
You need to use the following because if you call mysql_fetch_array outside of the loop, you're only returning an array of all the elements in the first row. By setting row to a new row returned by mysql_fetch_array each time the loop goes through, you will iterate through each row instead of whats actually inside the row.
while($row = mysql_fetch_array($result))
{
// This will loop through each row, now use your loop here
}
But the good way is to iterate through each row, as you have only three columns
while($row = mysql_fetch_assoc($result))
{
echo $row['name']." ";
echo $row['email']." ";
}
One common way to loop through results is something like this:
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
print_r($row);
// do stuff with $row
}
Check out the examples and comments on PHP.net. You can find everything you need to know there.
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 have a typical database query:
$query = mysql_query('SELECT titulo,referencia FROM cursos WHERE tipo=1 AND estado=1');
and I can convert it in an array and print the data:
while ($results=mysql_fetch_array($query)): ?>
echo $results['referencia'];
// and so...
endwhile;
but in some cases I need to print the same data in another part of the web page, but the $results array seems to be empty (I use var_dump($results) and I get bool(false)). I plan to use what I learned reading Create PHP array from MySQL column, but not supposed to mysql_fetch_array() creates an array? So, what happen?
Tae, the reason that your $result is false at the end of the while loop is that mysql_fetch_array returns false when it reaches the end of the query set. (See the PHP Docs on the subject) When you reach the end of the query set $results is set to false and the while loop is exited. If you want to save the arrays (database row results) for later, then do as Chacha102 suggests and store each row as it is pulled from the database.
$data = array();
while($results = mysql_fetch_array($query)) {
$data[] = $results;
}
foreach ($data as $result_row) {
echo $result_row['referencia'];
... etc.
}
Try this
while($results = mysql_fetch_array($query))
{
$data[] = $results;
}
Now, all of your results are in $data, and you can do whatever you want from there.
As Anthony said, you might want to make sure that data is actually being retrieved from the query. Check if any results are being returned by echo mysql_num_rows($query). That should give you the number of rows you got