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
Related
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
When I use mysqli_fetch_array() I get an array, but how do I read the values? Is a While-Loop the only option, or can I pick any value from a row and column like a multidimensional array with index like row[0] column [3] ?
while loop fetches you a row per iteration.
You can get all rows as multidimensional array with mysqli_fetch_all
After that you can use pick your values with [rowNum][colNum]
But beware when your result has lot of rows - array can be very big and cause memory or other issues.
Update to clarify:
if you want to receive multidimensional array of rows there are two ways:
First: iterate over mysqli_result with fetch_assoc/fetch_array and append row to array:
$rows = array();
while ($row = mysqli_fetch_array($res)) {
$rows[] = $row;
}
echo $rows[0]['whatever'];
Second: receive all results with one function call:
$rows = mysqli_fetch_all($res, MYSQLI_ASSOC);
echo $rows[0]['whatever'];
That's all. No more methods are available.
It depends how you are returning your results from the database.
there are flags in your mysqli_fetch_array function which you can set to modify your returned result.
If you use $row = mysqli_fetch_array($result, MYSQLI_ASSOC); or in OOP $row = $result->fetch_array(MYSQLI_ASSOC);
then you can access your returned result as column name in your while loop like $row['name'] or $row['age']
Or if you use $row = mysqli_fetch_array($result, MYSQLI_NUM); or in OOP $row = $result->fetch_array(MYSQLI_NUM);
then you can access your returned result in while loop like $row[0] or $row[3]
Simple example would be
while($row = mysqli_fetch_array($result)) {
echo $row['name'] . " " . $row['age'];
}
For further information. Read PHP.net Fetch Array
Your question suggests that your goal is just to get a single value. If that is the case, you should limit your query to only return what you're looking for rather than getting everything and then finding what you want in the results with PHP.
For the column, specify the one column you want instead of using *, (SELECT one_column FROM your_table) and for the row, either use a WHERE clause to select a specific id (provided that is defined on your table) or use a LIMIT clause to select a specific row number from the result set. Then you won't have to fetch in a loop or fetch all. Your query result (if it's successful) will just have one row with one column, and you can fetch once and get your value.
Granted, if you're going to need to do this repeatedly (i.e. in a loop), it isn't the best approach.
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 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.
OK, this is an amateur question but I am having the most trouble displaying the values of my database. I want to display the entire 'phone' column so my mysql query is this:
$result = mysql_query('SELECT phone FROM contactList WHERE phone != 1') or die(mysql_error());
$row = mysql_fetch_array($result)
My PHP script is as this:
while(isset($rows))
{
echo $rows['phone'] . "html break tag";
}
It looks like though I'm only getting the first result instead of looping through the entire column.
Do I need to increment the ID in my loop and get value via ID? The only problem with that is my auto-incremental ID column starts # 130 and skips some numbers here and there.
try this instead
While($row = mysql_fetch_array($result))
echo $row['phone'];
That should help you looping through the whole array.