Jump to content

Problems with an array


Go to solution Solved by selliottsxm,

Recommended Posts

Hello,

 

Can anyone tell me what I'm doing wrong here?  I am trying to run a query against a table for scenes from movies.  Each movie would have up to 8 scenes.  For the life of me I can't get this loop to work.  The movie id is being passed in the url, tested to make sure it's being passed and it is.  The code is pasted below. 

require_once ('../mysqli_connect.php');

$movie_id = $_GET['movie_id'];

$q = "SELECT * FROM scenes WHERE movie_id = $movie_id";

$r = @mysqli_fetch_array($dbc, $q);

$row = @mysqli_fetch_array($r, MYSQLI_ASSOC);

while ($row = @mysqli_fetch_array($r, MYSQLI_ASSOC));{
 
echo '<a href=" '.$row['scene_id'].'.mp4 ">'.$row['scene_name'].'</a>';}

In advance thanks a million!!

Edited by Zane
Link to comment
https://forums.phpfreaks.com/topic/288837-problems-with-an-array/
Share on other sites

You need to run the query with mysqli_query() before calling mysqli_fetch_array(). More information can be found here:

http://www.php.net/mysqli_query

 

Also note that you could use mysqli_fetch_assoc() instead of modifying mysqli_fetch_array() with the MYSQLI_ASSOC flag. More information can be found here:

http://www.php.net/mysqli_fetch_assoc

I tried this and it still won't work.  I'm real new to this and I can't get this one right..

 

$q = "SELECT * FROM scenes WHERE movie_id = $movie_id";

$r = @mysqli_query($q, $dbc);

$row = @mysqli_fetch_array($r, MYSQLI_ASSOC);

while ($row = @mysqli_fetch_array($r, MYSQLI_ASSOC));{
 
echo '<a href=" '.$row['scene_id'].'.mp4 ">'.$row['scene_name'].'</a>';}
 

Perhaps the issue is due to calling mysqli_fetch_array() prior to the loop. With how the code is written, the first result is always going to be thrown away.

$row = @mysqli_fetch_array($r, MYSQLI_ASSOC);  //<-- TRY REMOVING THIS LINE
 
while ($row = @mysqli_fetch_array($r, MYSQLI_ASSOC));{

I still can't get it to work, is it the way I'm constructing the a href?

 

$movie_id = $_GET['movie_id'];

$q = "SELECT * FROM scenes WHERE movie_id = $movie_id";

$r = @mysqli_query($q, $dbc);

while ($row = @mysqli_fetch_array($r, MYSQLI_ASSOC));{
 
echo '<a href=" '.$row['scene_id'].'.mp4 ">SCENE</a>';}

Have you tried turning on all PHP error. You can do that by adding the following to the top of your script:

 

<?php
//REPORT ALL PHP ERRORS
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
 
You'll also want to remove the error-suppression characters (@) from your code.
 
And MySQL errors can be displayed using mysqli_error():
 
 
 
Also when posting code, please surround it with

tags. It makes your posts and code easier to follow.  :happy-04:

Side note:

Just in case you're not aware, your query is currently open to SQL injection attacks. Assuming that $movie_id is supposed to be a number, you can make sure it is with ctype_digit():

http://www.php.net/manual/en/function.ctype-digit.php

 

If the ID isn't a number, display an error. Otherwise, run the query as normal.

Hiya,

 

Still getting errors.  I'm real new to this and I don't understand what the errors are trying to tell me  :(

 

require_once ('../mysqli_connect.php');

$movie_id = $_GET['movie_id'];
$q = "SELECT * FROM scenes WHERE movie_id = $movie_id";
$r = mysqli_query($q, $dbc);
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC));{

echo'
   <a href="movies/' .$row['scene_id']. '.mp4">
     <img src="img/scenes/movie_'. $r['movie_id'] .'_1_scene_thumb.jpg">
    </a> ';
}
 

 

[28-May-2014 06:54:38 America/Vancouver] PHP Warning:  mysqli_query() expects parameter 1 to be mysqli, string given in /Applications/MAMP/htdocs/movie2.php on line 84
[28-May-2014 06:54:38 America/Vancouver] PHP Warning:  mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /Applications/MAMP/htdocs/movie2.php on line 85

Still getting errors.  I'm real new to this and I don't understand what the errors are trying to tell me  :(

 

Sorry, I went a little crazy with responses...so you may have missed Reply 7. Try changing this:

$r = mysqli_query($q, $dbc);

To this:

$r = mysqli_query($dbc, $q);

Tried that too:

 

[28-May-2014 07:17:12 America/Vancouver] PHP Warning:  mysqli_query(): Couldn't fetch mysqli in /Applications/MAMP/htdocs/movie2.php on line 84
[28-May-2014 07:17:12 America/Vancouver] PHP Warning:  mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /Applications/MAMP/htdocs/movie2.php on line 85
 

I sent you a message...

[28-May-2014 07:17:12 America/Vancouver] PHP Warning:  mysqli_query(): Couldn't fetch mysqli in /Applications/MAMP/htdocs/movie2.php on line 84

 

Perhaps the MySQL connection was never opened or maybe it was closed for some reason before the query could execute.

 

What does your mysqli_connect.php code look like? Please obscure your database log-in information before posting the code.  :pirate:

I think that I should send you the entire code.  I have a section above that used a sql query and an array, although it didn't need to loop as all the data was from one record only.

So before I started the sql for the array that needed to loop I closed the database connection and then reopened thinking that the array needed to be emptied.

Check you messages I'll send you the code there (I also sent you a message earlier).  The database connection is fine, connects ok.

@selliottsxm, slow down. you actually need to read the documentation at php.net for the functions you are trying to use so that you know what they do and how they are used. there are also pretty good beginning code examples in the php.net documentation.

 

also, it's against the forum rules to pm members directly asking for help.

null given suggests that the query call failed.  Add this:

 

 

$r = mysqli_query($q, $dbc);
if (!$r)
{
   echo "Query did not run - error msg is: ".mysqli_error();
   exit();
}
while ($row = mysqli_fetch_assoc($r))
{
...

 

One should always CHECK the results of things to be sure they ran.  Also you should not have a semi after the while statement.

  • Solution

THANKS a million!!  I got it working

 

$q = "SELECT movie_id, scene_id, scene_name FROM scenes WHERE movie_id = $movie_id";
$r2 = mysqli_query($dbc, $q);
if (!$r2)
{
   echo "Query did not run - error msg is: ".mysqli_error();
   exit();
}
while ($row = mysqli_fetch_array($r2)){
echo'

      <a href="movies/'.$row['scene_id'].'.mp4">
     <img src="img/scenes/'.$row['scene_id'].'.jpg">
    </a> ';
}
 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.