0

I've been told that I was using an deprecated version of PHP, mysql_query, instead of the newer version mysqli_query, and soon it will be remove. Knowing that, I quickly tried to update all of my old, deprecated codes into the newer code. Doing that, I quickly ran into a problem, but I'm not sure what I'm doing wrong. Please take a look:

<?php

$connect = mysqli_connect('server','username','password','database');
$fetch = mysqli_query($connect,"SELECT username FROM userLogin");

while($row=mysqli_fetch_array($fetch,MYSQLI_NUM)){

//do something

}

?>

output:

Warning: mysqli_fetch_array() expects parameter 2 to be long, string given in /home/a2056400/public_html/test4.php on line 8

I have a feeling the error message has something to do with the conditional statement inside the while loop, but I'm not sure what is wrong with it. Please help in any way. Thank you.

2
  • Have you read the error message? It seems to me that it clearly refers to the 2nd parameter that's being passed to mysqli_fetch_array(). In your code, the 2nd parameter is mysqli_NUM when you probably mean to use the constant MYSQLI_NUM . Commented May 28, 2015 at 21:01
  • @CharlieS I'm sorry. In the older version, you're suppose to use mysql_NUM as oppose to MYSQL_NUM, so when I switched over, I thought the same rule applies. I guess I know that it doesn't now. Thanks for your answer. Commented May 28, 2015 at 21:37

1 Answer 1

2

You are not connecting to the database properly.

$connect = mysqli_connect('server','username','password','database');
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$fetch = mysqli_query($connect,"SELECT username FROM userLogin");
while($row=mysqli_fetch_array($fetch,MYSQLI_NUM)) {

    //do something

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

1 Comment

Why should the OP "try this"? Please add an explanation of what you did and why you did it 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.