Function not returning data as expected - php

I've got a function that should return me a set of links based on an user id. What the function does momentarily is that it returns me just one link instead a set of links based on an user id. The function looks like this:
function retrieve_image_link($user_id)
{
$query = mysql_query("SELECT `image_link` FROM `imgit_images` WHERE user_id = '" . intval($user_id) . "'");
while ($row = mysql_fetch_assoc($query))
{
$link = $row['image_link'] . '<br />';
}
mysql_free_result($query);
return $link;
}
So this is the code that should get me multiple links instead of just one. Where is the problem that the query returns a string instead of an array?
Please help!

you need to apply links to an array like $array[] = "link";
function retrieve_image_link($user_id)
{
$query = mysql_query("SELECT `image_link` FROM `imgit_images` WHERE user_id = '" . intval($user_id) . "'");
while ($row = mysql_fetch_assoc($query))
{
//add bracktes to $link to return an array
$link[] = $row['image_link'] . '<br />';
}
mysql_free_result($query);
return $link;
}
or use .= opperator if you want all the links in one big string
function retrieve_image_link($user_id)
{
$query = mysql_query("SELECT `image_link` FROM `imgit_images` WHERE user_id = '" . intval($user_id) . "'");
while ($row = mysql_fetch_assoc($query))
{
//add . to = to append string to $link
$link .= $row['image_link'] . '<br />';
}
mysql_free_result($query);
return $link;
}

It seems the problem is here
$link = $row['image_link'] . '<br />';
At the end of the loop, $link should have last value instead of all the values. You have to append each link or create an array to push each link.

$link = $row['image_link'] . '<br />';
You're overwriting the value of $link in the loop. Add [] to make it an array:
function retrieve_image_link($user_id)
{
$query = mysql_query("SELECT `image_link` FROM `imgit_images` WHERE user_id = '" . intval($user_id) . "'");
$link = array( );
while ($row = mysql_fetch_assoc($query))
{
$link[] = $row['image_link'] . '<br />';
}
mysql_free_result($query);
return $link;
}

use .=
function retrieve_image_link($user_id)
{
$query = mysql_query("SELECT `image_link` FROM `imgit_images` WHERE user_id = '" . intval($user_id) . "'");
while ($row = mysql_fetch_assoc($query))
{
$link .= $row['image_link'] . '<br />';
}
mysql_free_result($query);
return $link;
}

Related

function wont return result php

I have this function:
function dulide($uid) {
global $mysqli, $sm;
$city = $mysqli->query("SELECT city FROM users WHERE id = '" . $sm['user']['id'] . "'");
$haha = $city->fetch_assoc();
$citye = $haha['city'];
$sql = "SELECT id,age,name,email,city,fixed FROM users WHERE fixed=1 AND city = '" . $citye . "' LIMIT 5";
$query = $mysqli->query($sql);
if ($query->num_rows > 0) {
while ($user = $query->fetch_object()) {
$return .= '<tr><td> ' . $user->name . ' </td></tr><tr><td>';
$uid = $user->id;
$photos = $mysqli->query("SELECT photo FROM users_photos WHERE approved = 1 and profile = 1 and u_id = '" . $uid . "' order by id desc LIMIT 1");
if ($photos->num_rows > 0) {
while ($up = $photos->fetch_object()) {
$return .= '<img src="' . $up->photo . '" alt="Smiley face" height="50" width="50">';
}
}
$return .= ' </td></tr> ';
}
}
return $return;
}
When I try to call the function with
<?=dulide($uid); ?>
Nothing happens. Do anyone have a solution for this?
Huge amount of charma and blessings is given out! :)
Thanks on behalf
So turns out there was a database fault that made my function not work..Thanks for your help, another time would be good to make }else{ after num rows to post if no result :)

PHP array implode keys and values to function

I'm not too familiar with PHP arrays, I have the following code that generates query to output the results needed.
$allstore = $_POST['store'];
function createSelect($allstore)
{
if (empty($allstore))
return "";
$querySelect = "";
$queryJoin = "";
$baseTable = "";
foreach ($allstore as $store => $value) {
if (!$querySelect) {
$baseTable = $store;
$querySelect = "SELECT " . $store . ".item_no, " . $store . ".actual_price, " . $store . ".selling_price, " . $store . ".qty as " . $store;
} else {
$querySelect .= ", " . $store . ".qty as " . $store;
$queryJoin .= "
INNER JOIN " . $store . " ON " . $baseTable . ".item_no = " . $store . ".item_no";
}
}
$querySelect .= " FROM " . $baseTable;
$query = $querySelect . $queryJoin;
return $query;
}
//Stores to be shown
$allstore = ['s_M9' =>0 , 's_M10' =>1];
$query = (createSelect($allstore));
$result = mysql_query($query);
//rest of code...
As you can see above, at the very top there is $allstore = $_POST['store']; Which collects values based from previous form POST method that has checkbox with the name=store[] .
Now According to the function shown, if I create my own keys and values like this
$allstore = ['s_M9' =>0 , 's_M10' =>1];
the output shows exactly what i'm looking for. But the problem goes on how to let $allstore implode those stores s_M9, s_M10 based on what the user has selected on the previous page ( checkbox )? I mean, the user can select either one of the stores or Both stores . How can I implode the checked results between those brackets without inserting them manually?
Thank You
Edit :
<?php
echo "<form action='somewhere.php' method='POST'>";
$query = "SELECT * from stores_list ORDER BY short Asc";
$result = mysql_query($query);
if(mysql_num_rows($result)>0){
$num = mysql_num_rows($result);
for($i=0;$i<$num;$i++){
$row = mysql_fetch_assoc($result);
echo "<input type=checkbox name=store[] value={$row['short']} style='width:20px; height:20px;'>{$row['short']}";
}
}
else{
//No Stores Available
echo "No Stores Found !";
}
echo "</td><input type='submit' value='Search'/></form>";
$allstore = [];
if (!empty($_POST['store'])) {
foreach ($_POST['store'] as $value) {
$allstore[$value] = 1; // or 0, it doesn't matter because your function adds all the keys
}
}
$query = (createSelect($allstore));
$result = mysql_query($query);
And of course you have to take care of your createSelect function to avoid SQL Injections, please read here

PHP And JS Code Not Executed

i Have This PHP and JS Code on my web page.
<?php
include 'connect1.php';
$query = "SELECT URL FROM remontee_nlf ORDER BY URL ASC";
$result = mysql_query($query) or die (mysql_error());;
$counter = 0;
// write the values from the database into the javascript array
echo "<script type='text/javascript'>";
echo "this.styleListArray = new Array();";
if ($result) {
while($row = mysql_fetch_array($result)) {
echo("this.nameArray[" . $counter . "] = '" . $row['URL'] . ", " . $row['user_fname'] . "';"); // displays 'lname, fname'
$counter += 1;
}
}
echo("</script>");
?>
The Problem is that when i execute the page containing the code, a part of this code doesn't get executed and it just shows on the page as a simple text :
"); echo "this.styleListArray = new Array();"; if ($result) { while($row = mysql_fetch_array($result)) { echo("this.nameArray[" . $counter . "] = '" . $row['URL'] . ", " . $row['user_fname'] . "';"); // displays 'lname, fname' $counter += 1; } } echo(""); ?>
I tried to figure it out, but i couldn't get it, if you can help brothers, that would be wonderful.
Try rewriting your code like this:
include 'connect1.php';
$query = "SELECT URL FROM remontee_nlf ORDER BY URL ASC";
$result = mysql_query($query) or die (mysql_error());
$counter = 0;
// write the values from the database into the javascript array
echo <<<HTML
<script type='text/javascript'>
this.styleListArray = new Array();
HTML;
$strLine = '';
if ($result) {
while($row = mysql_fetch_array($result)) {
$strLine.= "this.nameArray[" . $counter . "] = '" . $row['URL'] . ", " . $row['user_fname'] . "';";
$counter += 1;
}
}
echo $strLine;
echo("</script>");
First of all change your queries to use mysqli or pdo connections
secondly try following code
$sql = 'SELECT URL FROM remontee_nlf ORDER BY URL ASC';
$res = mysql_query($sql, $con);
$rows = array();
while ($row = mysql_fetch_assoc($res))
$rows[] = $row['URL'];
$str = implode('", "', $rows);
$data = '["'.trim($str).'"]';
echo '<script type="text/javascript">';
echo "var data = $data;";
echo 'console.log(data)';
echo '</script>';
check you console log.
You have $result = mysql_query($query) or die (mysql_error());; change it to
$result = mysql_query($query) or die (mysql_error());
Also make sure $row['URL'] and $row['user_fname'] are available.

Dynamic pages using php

I am trying to create dynamic pages on my website, but it fails.
When I try the code on Xampp it works perfect. There is another thing that I don't understand.
It will catch the id, but not the title or anything with characters from the database.
When I try $title = $_GET['title']; it won't work. It works only with $_GET['id'];
Any help?
Here is the code:
index.php
<?php
include('inc/code.inc.php');
$fetch = mysql_query("SELECT * FROM `star` ORDER BY `title`");
while ($output = mysql_fetch_assoc($fetch))
{
echo ''. $output['title'] .'<br />';
}
?>
run.inc.php
<?php
include_once('inc/code.inc.php');
$newID = $_GET['id'];
$fetch = mysql_query("SELECT * FROM `star` WHERE `id` = $newID");
while ($output = mysql_fetch_assoc($fetch))
{
echo $output['title'] . '<br />' . $output['explain'];
}
?>
Here is the code that won't work:
index.php
<?php
include('inc/code.inc.php');
$fetch = mysql_query("SELECT * FROM `star` ORDER BY `title`");
while ($output = mysql_fetch_assoc($fetch))
{
echo ''. $output['title'] .'<br />';
}
?>
run.inc.php
<?php
include_once('inc/code.inc.php');
$newID = $_GET['title'];
$fetch = mysql_query("SELECT * FROM `star` WHERE `title` = $newID");
while ($output = mysql_fetch_assoc($fetch))
{
echo $output['title'] . '<br />' . $output['explain'];
}
?>
Add title to your URL GET parameter please..Like you have added ID
<?php
include('inc/code.inc.php');
$fetch = mysql_query("SELECT * FROM `star` ORDER BY `title`");
while ($output = mysql_fetch_assoc($fetch))
{
echo '<a href = "run.inc.php?id='. $output['id'] .'"'.'?title='. $output['title'] .'</a><br />';
}
?>
Modify the url according to your needs please.
If you are comparing a string like $_GET['title'] to the column, you need to escape it (as opposed to a numeric ID, which does not need to be escaped).
Try this for run.inc.php
$newID = $_GET['title'];
$fetch = mysql_query("SELECT * FROM `star` WHERE `title` = '$newID'");
while ($output = mysql_fetch_assoc($fetch))
{
echo $output['title'] . '<br />' . $output['explain'];
}
?>

how to return multiple rows using a function in php

Look at the following code :
<?php
function getmangainfo($teamname)
{
$rValue = "";
$lValue = "";
$query = ("SELECT pic, mn_title FROM table Where mn_team LIKE '%" . $teamname . "%' Limit 0,4 ");
$row_data = mysql_query($query);
while ($row_data = mysql_fetch_array($row_data)) {
$rValue = $row['pic'];
$lValue = $row['mn_title'];
return "<a class='linksrepeated' href='" . $ABSPATH . "/" . $lValue . "/'> <img src='" . $rValue . "'/></a>";
}
}
this function is not returning anything! I am thinking it is because that the return statement is inside the while loop. I tried many things hoping it will return the 4 results but nothing happened. the SQL query works 100%. the problem is with my function. please let me know what is wrong and how to fix it.
change the $row_data to $row in your while statement
while($row = mysql_fetch_array($row_data))
because as I see the codes inside the while
$rValue = $row['pic'];
$lValue = $row['mn_title'];
you get your data as $row but your in while statement is $row_data
the problem is not on the while loop as the execution reaches the return statement, the execution pointer will exit the function(of course in the while statement)
but for me to make your code cleaner as i see you expect only one row in return pull out the return statement on your while
$rValue = "";
$lValue = "";
while ($row_data = mysql_fetch_array($row_data)) {
$rValue = $row['pic'];
$lValue = $row['mn_title'];
break; //just to make sure for one row return
}
return "<a class='linksrepeated' href='" . $ABSPATH . "/" . $lValue . "/'> <img src='" . $rValue . "'/></a>";
but as the others says that you expect 4 row returns you can create a variable that would store all the returns in a single string
$rValue = "";
$lValue = "";
$links = "";
while ($row_data = mysql_fetch_array($row_data)) {
$rValue = $row['pic'];
$lValue = $row['mn_title'];
$links .="<a class='linksrepeated' href='" . $ABSPATH . "/" . $lValue . "/'> <img src='" . $rValue . "'/></a>";
}
return $links
reference:
http://php.net/manual/en/function.mysql-fetch-array.php
function getmangainfo($teamname){
$rValue = "";
$lValue = "";
$query = ("SELECT pic, mn_title FROM table Where mn_team LIKE '%".$teamname."%' Limit 0,4 ");
$row_data = mysql_query($query);
$output=array();
while($row = mysql_fetch_array($row_data))
{
$rValue = $row['pic'];
$lValue = $row['mn_title'];
$output[]="<a class='linksrepeated' href='".$ABSPATH."/".$lValue."/'> <img src='".$rValue."'/></a>";
}
return $output;
}
Edit: Updated variable name
Its not giving full result set because you are 'returning' from within the loop. try the following it should help.
function getmangainfo($teamname){
$rValue = "";
$lValue = "";
$query = ("SELECT pic, mn_title FROM table Where mn_team LIKE '%".$teamname."%' Limit 0,4 ");
$row_data = mysql_query($query);
$return = '';
while($row_data = mysql_fetch_array($row_data))
{
$rValue = $row['pic'];
$lValue = $row['mn_title'];
$return .= "<a class='linksrepeated' href='".$ABSPATH."/".$lValue."/'> <img src='".$rValue."'/></a>";
}
return $return;
}

Categories