简体   繁体   中英

While loop php arrays

I have three php arrays that i am filling up with information returned from a msql query. The first two arrays fill up perfectly with the information need but the last array does not fill up. The only thing that gets entered is a 'v'. It works perfectly if i don't make it an array and just a single value but when i make it an array it stops working. If you have any ideas thanks.

$query = mysql_query("SELECT * FROM chanels WHERE cname = '$cname' ");
$numrows = mysql_num_rows($query);

while($row = mysql_fetch_array($query))
{
    $videolocation[$counter2] = $row['videolocation'];
    $title[$counter2] = $row['title'];
    $imagelocation[$counter2] =  $row['videoimagelocation'];
    $counter2++;

}

ImageLocation array is the one that does not work while the other two fill up perfectly. I have checked spelling and everything is entered perfectly.

Instead of using fetch_array use mysql_fetch_assoc for your method so that you bring back the rows with keys in the array named as the columns in the results - like this:

while ($row = mysql_fetch_assoc($result)) {
    echo $row["videolocation"];
    echo $row["title"];
    echo $row["videoimagelocation"];
}

Also, you should probably move away from the old mysql_* functions and tinker with the new shiny PDO functions. These will let you connect in a much safer manner as well as providing much more flexibility.

Edit: When you bring back an assoc array, it is still a single row of the database. However, your current code returns something like this:

$row[0]='someLoc' // Data from videoLocation
$row[1]='someTitle' // Data from title column
$row[2]='someImageLoc' // Data from videoImageLocation

And Assoc Array will work like this:

$row['videolocation']='someLoc' 
$row['title']='someTitle' 
$row['videoimagelocation]='someImageLoc' 

Edit 2: It seems that I chased down a bit of a red herring with the array vs assoc issue. The first thing that I could check is the spelling, but you have done that, the next is check the case of the column names. On windows, the case is by default not important, but on Linux machines it is .

I find the easiest way is to do this:

mysql_query("SELECT videolocation, title, videoimagelocation FROM chanels WHERE cname = '$cname' ");

If a column is the wrong case, this should return an error for you.

Not an answer, but a suggestion to make better use of arrays. Use a single one to store all data:

while($row = mysql_fetch_array($query))
{
    $details[$counter2] = array('videoLocation' => $row['videolocation'],
                                'title' =>   $row['title'],
                                'imageLocation' => $row['videoimagelocation']
                                );
    $counter++;
}

That way, first index will store 1st row details, second index will store 2nd row details and so on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM