0

I have problem in this code, I'm taking information from two tables and "where news_id=$dz" is not working. Here is the page which include this code.

Here is my code:

<?php
mysql_query("SET NAMES UTF8");
$result = mysql_query("SELECT dz,title FROM dzeglebi where raioni='ყვარლის მუნიციპალიტეტი' && mxare='kaxeti' ORDER BY title ASC", $db);
$myrow = mysql_fetch_array ($result);

printf(
       "<h2><li>
       <strong><a href='../../dzeglebi.php?id=%s'>%s</a>
       </strong></li></h2>",$myrow["dz"],$myrow["title"]);


function FetchImage($id)
{
    $images=array();
    $x=0;
    $d=mysql_query("select * from `images` where news_id=$dz");
    while($data=mysql_fetch_array($d))
    {
        $images["big"][$x]=$data["image"];
        $images["small"][$x]=$data["small"];
        $x++;
    }   
return $images;
}
function CountImages($id)
{
$d=mysql_query("select * from `images` where news_id=$dz");
return mysql_num_rows($d);  
}
$imgs=FetchImage($id);

 for($i=0;$i<CountImages($id);$i++)
{
echo'

 <img src="../'.$imgs["big"][$i].'" >';
}



?>
6
  • What's the expected vs actual output? Commented Oct 10, 2013 at 20:04
  • You should connect to the $db at first. In functions you cant access $dz. In php inside functions you can access only arguments and local variables. So you should pass $dz az a function argument. (Or access it as a global variable, but passing as an argument is a better way.) Probably there are others issues also... Commented Oct 10, 2013 at 20:05
  • You have $id parameter in FetchImage($id) and CountImages($id) and using where on where news_id=$dz there should be $id Commented Oct 10, 2013 at 20:19
  • Check your $db variable Commented Oct 10, 2013 at 20:20
  • I understand you but I want to this that "news_id"=$dz how can I do this? Commented Oct 11, 2013 at 12:33

2 Answers 2

1

function CountImages($id) { $d=mysql_query("select * from images where news_id=$dz"); - here you should use $id

next problem: - DO NOT USE for($i=0;$i<CountImages($id);$i++) change it to

$len = CountImages($id);
for($i=0;$i<$len;$i++)

because you do not need to perform SQL query for each FOR iteration, or even remove it - as you have $images array, iterate over it

next problem:

$d=mysql_query("select * from `images` where news_id=$id");
return mysql_num_rows($d);  

DO NOT USE mysql_num_rows, use SQL count(*) - because it will send only 1 number from mysql to php, instead of whole result set

Sign up to request clarification or add additional context in comments.

Comments

0

In the 1st query, change && mxare='kaxeti' by AND mxare='kaxeti'. Also, in functions FetchImage and CountImages, change news_id=$dz by news_id=$id

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.