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.

2
  • 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". Commented Jun 10, 2015 at 2: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 stackoverflow.com/questions/60174/… which shows the correct way to put parameters into your SQL. Commented Jun 10, 2015 at 2:23

1 Answer 1

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

2 Comments

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 ?
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!

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.