0

I am wondering how I can change the code below, so that I can pull the results form the database and store them as a numeric array. I have tried searching for an answer online and I know you use SQLI_NUM, but I haven't been able to implement it. Any help would be really appreciated.

/

$lecturers_temp = array();
$i = 0;
$q3 = "SELECT `uninum` FROM `availabilityindex` ORDER BY `availability`";
$result3 = @mysqli_query($dbcon,$q3);
while ($row3 = mysqli_fetch_array ($result3, MYSQLI_ASSOC)){
    $lecturers_temp = $row3['uninum']; //

    $lecturers_temp[$i] = $row3['uninum']; //'uninum'
    //echo $lecturers_temp [$i] . "<BR><BR>";
    $i++;
}
3
  • What do you mean by a numeric array, do you mean you want $row3['uninum']; to be numeric rather than a string? Commented Aug 24, 2015 at 18:00
  • You may need to gove us an example of what you mean Commented Aug 24, 2015 at 18:02
  • Sorry, I meant I want to be able to refer to the results by using numeric indexes like $lecturers_temp [0], $lecturers_temp [1] etc Commented Aug 24, 2015 at 18:02

2 Answers 2

1

Simple you dont actually have to do anything clever at all you just load each row into arrayname[] PHP will look after incrementing the array index automatically.

$lecturers_temp = array();

$q3 = "SELECT `uninum` FROM `availabilityindex` ORDER BY `availability`";

$result3 = mysqli_query($dbcon,$q3);
if ( ! $result3 ) {
    echo mysqli_error($dbcon);
}

while ($row3 = mysqli_fetch_array ($result3, MYSQLI_ASSOC)){
    $lecturers_temp[] = $row3['uninum']; //
}

You should never use the @ error supression on a mysqli statement. Its always best to know that something has gone wrong. Of course on a live system you would want to log these errors to a file so that admin s can check for errors and users do not see uninteligable errors.

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

1 Comment

Many thanks RiggsFolly! Much appreciated and I will mark it as answered in a moment.
0
$lecturers_temp = array();
$i = 0;
$q3 = "SELECT `uninum` FROM `availabilityindex` ORDER BY `availability`";
$result3 = @mysqli_query($dbcon,$q3);
$row = $result3->fetch_array(MYSQLI_NUM);
...
?>

1 Comment

Why should the OP try this? A good answer will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO.

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.