1

I have a file file.php and inside my file I am using the code bellow to pull some data from my database and display some information.

My code is

$array = $_GET['theurl']; // My url looks like myfile.php?theurl=1,2,3 (id,s)
$sqlnt4 = "select * from mytable WHERE `id` IN ($array)";
$rsdt4 = mysql_query($sql);
$tc4a = mysql_fetch_assoc($rsdt4);
$mycomma4 = ",";
if ($tc4a['a_youtube'] == "#"){
}else{
    while ($tc4 = mysql_fetch_assoc($rsdt4))
    {
        echo $tc4['a_youtube'];
        echo ",";
    }
}

I expect to echo the infos of the two id's (in array) inside my while function, but it returns the results only from the first.

Any ideas?

Matthias
  • 3,582
  • 2
  • 30
  • 41
Irene T.
  • 1,393
  • 2
  • 20
  • 40
  • 1
    Idea1: Stop using mysql_* and use Pdo instead. – Goikiu Jan 03 '14 at 09:39
  • 1
    you have 2 rows in result, you fetch once outside while-loop with no output, then once inside with one output - so you'll have only 1 row in your output – user15 Jan 03 '14 at 09:41
  • 1
    And this is why you shouldn't use `mysql_*` functions: http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – Marijke Luttekes Jan 03 '14 at 09:41
  • The OP's been told countless times about the use of `mysql_*` functions. I'm starting to think she's grown a particular fondness of working with "old code". @Goikiu Hey, maybe she likes the attention?! – Funk Forty Niner Jan 03 '14 at 15:59

3 Answers3

4

I am confusing on $sql :

$sqlnt4 = "select * from mytable WHERE `id` IN ($array)";
$rsdt4 = mysql_query($sql);

Can you take a look after changing below:

$sqlnt4 = "select * from mytable WHERE `id` IN ($array)";
$rsdt4 = mysql_query($sqlnt4);
2

First that's extremely vulnerable to security issues - I hope this isn't used in production and just for playing around.

I recommend switching to PDO, or at the very least securing your variables.

To put that array into the query, you need to implode it into a list, as such.

$list = implode(',', $array);

You can then use the list in the statement, which will look like 1,2,3.

Edit:

I've just realized your $array value isn't actually an array - have you missed code out or is it badly named?

Ryan
  • 3,552
  • 1
  • 22
  • 39
0

mysql_fetch_assoc: "Returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead." http://pt2.php.net/mysql_fetch_assoc

Try mysql_fetch_rows to return all matching rows into an array.

Tiago
  • 1,984
  • 1
  • 21
  • 43