Concatenating a MySQL query and text - php

I have the code
$link = "group.php?=";
$fulllink;
while($row = mysql_fetch_array($result))
{
$other = $link.$row;
echo $row;
echo " $row[mygroup] ";
echo "</br>";
}
which I would like to link to each group's group.php page (such as group.php?=samplegroup). However, mysql_fetch_array returns an array, which I am unable to concatenate to the $link variable. What should I do?

You just have to access the array:
$other = $link.$row['mygroup'];
(or whatever the array keys are)
Your code a little bit nicer:
<?php
// other code
$link = "group.php?=";
?>
<?php while(($row = mysql_fetch_array($result))): ?>
<a href="<?php echo $link, $row['mygroup']; ?>">
<?php echo $row['mygroup']; ?>
</a>
</br>
<?php endwhile; ?>

Related

Nested while loop in PHP in getting data from database

<?php
$sqlquerypmenu = "select *
from subsubmenu
where submenu_id=1
and position='left'
and status=1";
if($querypmenu = sqlsrv_query($conn,$sqlquerypmenu)){
if(sqlsrv_has_rows($querypmenu) === true){
while($rowdata = sqlsrv_fetch_array($querypmenu, SQLSRV_FETCH_ASSOC)){
?>
<h4 class="title-small folder_name"> <?php echo $rowdata ['website_title']; ?> </h4>
<?php
$id = $rowdata['id'];
$filequerymenu = "select *
from upload_files
where main_menu='value_name'
and sub_menu='value_key'
and subsub_menu= $id ";
if($filemenu = sqlsrv_query($conn,$filequerymenu)){
if(sqlsrv_has_rows($filemenu) === true){
while($filedata = sqlsrv_fetch_array($filemenu, SQLSRV_FETCH_ASSOC)){ ?>
<a class="smalltext font_val" href="<?php echo DOCUMENT_URL.$filedata ['file_name']; ?> " target="_blank" ><?php echo $filedata['document_name']; ?></a>
<?php
}
}
}
}
}
}
?>
In the nested while loop the second while loop only shows the first row of data and does not show the rest of the data.
How can I fix this?
Consider querying the database once with one JOIN query. Possibly opening up another fetch within a while loop causes instances issues:
<?php
...
$sql = "select s.website_title, u.file_name, u.document_name
from upload_files u
inner join subsubmenu s ON u.subsub_menu = s.id
where u.main_menu = 'value_name'
and u.sub_menu = 'value_key'
and s.submenu_id = 1
and s.[position] = 'left'
and s.[status] = 1
order by s.id, s.website_title;"
$title = "";
if($result = sqlsrv_query($conn, $sql)){
if(sqlsrv_has_rows($result) === true){
while($rowdata = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)){
if($title != $rowdata['website_title']) {
$title = $rowdata['website_title']
?>
<h4 class="title-small folder_name"> <?php echo $rowdata ['website_title']; ?> </h4>
<?php
}
?>
<a class="smalltext font_val" href="<?php echo DOCUMENT_URL.$filedata ['file_name']; ?> " target="_blank" ><?php echo $filedata['document_name']; ?></a>
<?php
}
}
}
?>

Passing session variables to another page

I am trying to pass session variable to the next page, index.php but i have gotten an error undefined index on foreach($_SESSION['result'] as $row). May i know what's wrong?
<?php
session_start();
$search = preg_replace("/[^A-Za-z0-9]/", " ", $_POST['query']);
$search = $db->real_escape_string($search);
if (strlen($search) >= 1 && $search !== ' ') {
$query = 'SELECT * FROM tablename WHERE name LIKE "%'.$search.'%"';
$result = $db->query($query) or trigger_error($db->error."[$query]");
while($results = $result->fetch_array()) {
$resultArray[] = $results;
}
if (isset($resultArray)) {
foreach ($resultArray as $result) {
$show_name = preg_replace("/".$search."/i", "<b class='highlight'>".$search."</b>", $result['name']);
$show_url = 'index.php';
$out = str_replace('name', $show_name, $html);
$out = str_replace('url', $show_url, $out);
$_SESSION['result']= $result['name'];
echo($out);
}
}
}
?>
index.php
session_start();
<table>
<?php foreach($_SESSION['result'] as $row){ ?>
<tr>
<td>
<?php echo $row['name'];?>
</td>
<td>
<?php echo $row['description'];?>
</td>
</tr>
<?php } ?>
</table>
if $_SESSION['result'] is an array then try like below
$_SESSION['result'][]= $result['name'];
as per your code session result variable is not an array
You never start the session in the first file. Start the file with
<?php
session_start();
Because you're missing that, when you assign a value to $_SESSION['result'] after your queries, nothing is saved in session.
There is another error. You mean for $_SESSION['result'] to be an array, but you're saving it as a string. Change:
$_SESSION['result']= $result['name'];
To
$_SESSION['result'][]= $result['name'];

php & pdo & mysql countrows returning 0

I have now spent several hours without getting anywhere.
I want to display number of rows in my query and here here is my code:
<?php
$user_id = 1;
$statement = $dbConn->query("select badoo_interest.*, badoo_category.icon_class from badoo_interest, badoo_user_interest, badoo_category where badoo_user_interest.interest_id = badoo_interest.id and badoo_user_interest.user_id = :id_user_profile and badoo_category.id = badoo_interest.category_id");
$statement->execute(array(':id_user_profile'=> $id_user_profile));
$rResult = $statement->fetchAll();
echo count($rResult);
echo "<h2>Interesi</h2>";
while( $row = $statement->fetch(PDO::FETCH_ASSOC) ){
?>
<div class="interest" id = "int<?php echo $row['id'];?>">
<span class = "intr-ico <?php echo $row['icon_class'];?>"></span>
<?php echo $row['name'];?>
</div>
<?php
}
?>
And i getting number 10 but now im not getting any interests displayed.
If you use this code now:
<?php
//$user_id = 1;
$statement = $dbConn->query("select badoo_interest.*, badoo_category.icon_class from badoo_interest, badoo_user_interest, badoo_category where badoo_user_interest.interest_id = badoo_interest.id and badoo_user_interest.user_id = :id_user_profile and badoo_category.id = badoo_interest.category_id");
$statement->execute(array(':id_user_profile'=> $id_user_profile));
echo "<p>" . $statement->rowCount(). " interests:</p>";
while( $row = $statement->fetch(PDO::FETCH_ASSOC) ){
?>
<div class="interest" id = "int<?php echo $row['id'];?>">
<span class = "intr-ico <?php echo $row['icon_class'];?>"></span>
<?php echo $row['name'];?>
</div>
<?php
}
?>
And now im getting all the interests displayed but it counts: 0 interests.
Any ideas who i can count number of interests?
If you read the documentation for PDOStatement::rowCount you will see this line
If the last SQL statement executed by the associated PDOStatement was
a SELECT statement, some databases may return the number of rows
returned by that statement. However, this behaviour is not
guaranteed for all databases and should not be relied on for portable
applications.
The work around is to count() function from php like you did. If that did not work then use COUNT(*) as result_count. then when you fetch read that value.
And i getting number 10 but now im not getting any interests displayed
problem
The reason why the result aren't displaying is because you fetched all the row already so you cannot use fetch() again.
solution
Use the array returned by fetchAll() instead and use a foreach instead
$rows = $statement->fetchAll();
echo "<p>" . count($rows). " interests:</p>";
foreach ($rows as $row){
//echo $row['name']
}
<?php
//$user_id = 1;
$statement = $dbConn->query("select badoo_interest.*, badoo_category.icon_class from badoo_interest, badoo_user_interest, badoo_category where badoo_user_interest.interest_id = badoo_interest.id and badoo_user_interest.user_id = :id_user_profile and badoo_category.id = badoo_interest.category_id");
$statement->execute(array(':id_user_profile'=> $id_user_profile));
echo "<p>" . $statement->rowCount(). " interests:</p>";
$counter = 0; //declare counter
while( $row = $statement->fetch(PDO::FETCH_ASSOC) ){
$counter++; //increment counter
?>
<div class="interest" id = "int<?php echo $row['id'];?>">
<span class = "intr-ico <?php echo $row['icon_class'];?>"></span>
<?php echo $row['name'];?>
</div>
<?php
}
?>
[NOTE: Edit by #DinoAmino removed the emphasis characters around the two uses of $counter, which caused the code to not compile for the question asker]
I solved my problem like this:
Here is the updated solving version. Thank you all for the input:
<?php
$user_id = 1;
$statement = $dbConn->query("select badoo_interest.*, badoo_category.icon_class from badoo_interest, badoo_user_interest, badoo_category where badoo_user_interest.interest_id = badoo_interest.id and badoo_user_interest.user_id = :id_user_profile and badoo_category.id = badoo_interest.category_id");
$statement->execute(array(':id_user_profile'=> $id_user_profile));
$rows = $statement->fetchAll();
if(count($rows) >= '1') {
?>
<div class="your-interests">
<?php
echo "<p>" . count($rows). " interests:</p>";
foreach ($rows as $row){
?>
<div class="interest" id = "int<?php echo $row['id'];?>">
<span class = "intr-ico <?php echo $row['icon_class'];?>"></span>
<?php echo $row['name'];?>
</div>
<?php
}
?>
</div>
<?php } ?>

PHP MySQL display data by id from database - freedom placement

I would like to have the freedom to place a row entry from my database wherever i' prefer in the page. Right now, the php code that I use is as follows (it is clean working code):
<html><head></head>
<body>
<?php
$db = mysql_connect("xxx","xxx","xxx") or die("Database Error");
mysql_select_db("caisafety",$db);
$id = $_GET['id'];
$id = mysql_real_escape_string($id);
$query = "SELECT * FROM `cert_rr` WHERE `id`='" . $id . "'";
$result = mysql_query($query);
echo $row['id']; while($row = mysql_fetch_array( $result )) {
echo "<br><br>";
echo $row['basic3'];
echo $row['basic2'];
echo $row['basic1'];
}
?>
</body>
</html>
I call id through the browser Eg. http://site.com/getid.php?id=10 . But I do not have the freedom to place my row entry within my html. For eg. like this:
<table><tr>
<td align="center">BASIC INFO 1: <?php echo $row['basic1']; ?></td>
<td align="center">BASIC INFO 2: <?php echo $row['basic2']; ?></td>
</tr></table>
I can place html within echo PHP tags but then I have to clean up my html and thats a lot of work. Retaining HTML formatting would be preferred. Any help or guidelines on this would be much appreciated.
<?php
$db = mysql_connect("xxx","xxx","xxx") or die("Database Error");
mysql_select_db("caisafety",$db);
$id = $_GET['id'];
$id = mysql_real_escape_string($id);
$query = "SELECT * FROM `cert_rr` WHERE `id`='" . $id . "'";
$result = mysql_query($query);
//you need to retrieve every row and save to an array for later access
for($rows = array(); $tmp = mysql_fetch_array($result);)
{
$rows[] = $tmp;
}
//now you can use the $rows array where every you want e.g. with the code from Zhube
?>
....
<table><?php foreach($rows as $r):
<td><?php echo $r['id'] ?></td><?php endforeach ?>
</table>
By
while($row = mysql_fetch_array( $result )) {
echo "<br><br>";
echo $row['basic3'];
echo $row['basic2'];
echo $row['basic1'];
}
you save only the last row
Instead of having your HTML tags as part of the PHP variable, do something like this instead:
<table><?php foreach($row as $r):?>
<td><?php echo $r['id'] ?></td><?php endforeach ?>
</table>

Display only one entry from the database with PHP

The following code retrieves and displays the correct data from the database however it gets all the data. I need a way to assign each value it retrieves from the database to a PHP variable. For example, if it gets "Joe", "Henry", and "Robert" from the database, I'd like one variable for each of those and right now it returns and array with all the values.
<?php
dbCon();
$query1 = mysql_query("SELECT * FROM hosts WHERE name!=''");
while($row1 = mysql_fetch_assoc($query1)) {
$res = $row1['name'] . '<br />';
echo $res;
}
?>
<?php
function echoName($id) {
dbCon();
$query1 = mysql_query("SELECT * FROM hosts WHERE id='$id'");
while($row1 = mysql_fetch_assoc($query1)) {
$res = $row1['name'] . '<br />';
echo $res;
}
}
?>
<div id="joe_div">
<?
echoName("1");
?>
</div>
.....
<div id="henry_div">
<?
echoName("2");
?>
</div>
Declare variable variables as such:
while($row1 = mysql_fetch_assoc($query1)) {
$$row1['name'] = $row1['name']
}
echo $Joe; //returns Joe
Though I don't know why you would ever need that
if it gets "Joe", "Henry", and "Robert" from the database, I'd like one variable for each of those
So try this:
<?php
dbCon();
$query1 = mysql_query("SELECT * FROM hosts WHERE name!=''");
while($row1 = mysql_fetch_assoc($query1)) {
$$row1['name'] = $row1['name'];
}
echo $Joe;
echo $Henry;
echo $Robert;
?>
P.S: I don't know why you want to do this, but i am sure you have a better approach to solve your problem.

Categories