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?

4
  • 1
    Idea1: Stop using mysql_* and use Pdo instead. Commented Jan 3, 2014 at 9: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 Commented Jan 3, 2014 at 9:41
  • 1
    And this is why you shouldn't use mysql_* functions: stackoverflow.com/questions/12859942/… Commented Jan 3, 2014 at 9: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?! Commented Jan 3, 2014 at 15:59

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry for this was a mistyped.. my code is like this that you provide me. Any ideas?
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?

1 Comment

It's for my personal use.. $list is has already look like this myfile.php?theurl=1,2,3,4
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.

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.