0

There was a similar question on these site before but my varies a bit that I want to do a while loop inside the function(the other did not ask for that).

What I want to do is run different SELECT queries on different parts of my site, so to avoid repeating those similar versions of SELECT queries, i want to create a function that does that for me inside a functions.php file. So i am thinking of doing these on the functions.php file:

function imasde($level) { 
    /* $level is to select custom tables for different pages not just the same table */
    $value = "";    
    $sql = "SELECT * FROM labs WHERE level = $level";
    $resu = mysql_query($sql);
    $row = while( $fila=mysql_fetch_array($resu));
    $value = $row;
    return $value;
}

So on the HTML template i all put these code to use the function.

<?php echo imasde('Medium'); ?>
<div class="task medium"> /*It should loop the different values on the Selected tables not just one value */
    <div class="title"><?php echo $value["name"]; ?></div>
    <div class="date"><?php echo $value["category"]; ?></div>
</div>

Of course there is a bug on these cause it doesn't work, sorry if it sounds stupid query but i have lost practice on back end coding for a while, too use to WordPress little coding to be done with a premium theme and plugins...

If possible explain me a bit what I am doing wrong.

Politank-Z
  • 3,653
  • 3
  • 24
  • 28
bonini81
  • 50
  • 6
  • Instead of saying that you saw a similar question you could link it that would be more convenient for other people. You might as well want to fix a bit your grammar, especially the "i". – meneldal Jun 10 '15 at 02:19
  • Error messages and other information would help us to understand and diagnose your problem. I'm not a PHP programmer, but won't your example lose the single quotes, so the SQL will become `SELECT * FROM labs WHERE level = Medium` instead of `SELECT * FROM labs WHERE level = 'Medium'` ? Also, see the related question http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1 which shows the correct way to put parameters into your SQL. – Turophile Jun 10 '15 at 02:23

1 Answers1

1

Suggestion: Stop using mysql_* function as they deprecated (read more here)

instead, consider using mysqli_* functions or PDO

And for your question, you basically are keep setting the same value on the while. and then handle it as array which the $row aren't array to fix this, you can use it as an array like this:

function imasde($level)
{ 
    /* $level is to select custom tables for different pages not just the same table */
    $value = "";    
    $sql = "SELECT * FROM labs WHERE level = $level";
    $resu = mysql_query($sql);

    // Declare the rows array
    $rows = array();

    // Insert each row to the array
    while($row = mysql_fetch_array($resu))
        $rows[] = $row;

    // Return the array
    return $rows;
}

Then:

$rows = imasde('Medium');

foreach($rows as $row)
{
    echo '<div class="task medium">';
    echo '<div class="title">'. $row["name"] .'</div>';
    echo '<div class="date">'. $row["category"] .'</div>';
    echo '</div>';
}
Ido
  • 2,034
  • 1
  • 17
  • 16
  • Well thanx for the reply and clear explanation, also the tip to use more mysqli is pretty useful for me as well. i got a question, well the script is not working, i all have to test it again and again, but just a quick query isnt the while command supposed to go with brackets or: while($row = mysql_fetch_array($resu)) { //something inside here} $rows[] = $row; Is there a syntax error on the while ? – bonini81 Jun 10 '15 at 03:51
  • Well solved the issue but there is 2 mistakes on your script. 1st: WHERE level = '$level' ('level' with '') 2nd: while loop should have these syntax: while($row = mysql_fetch_array($resu)){ $rows[] = $row; } Other than is the correct answer thanx for the help! – bonini81 Jun 12 '15 at 04:16