selliottsxm Posted May 28, 2014 Share Posted May 28, 2014 (edited) 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 May 28, 2014 by Zane Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted May 28, 2014 Share Posted May 28, 2014 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 Quote Link to comment Share on other sites More sharing options...
selliottsxm Posted May 28, 2014 Author Share Posted May 28, 2014 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>';} Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted May 28, 2014 Share Posted May 28, 2014 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));{ Quote Link to comment Share on other sites More sharing options...
selliottsxm Posted May 28, 2014 Author Share Posted May 28, 2014 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>';} Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted May 28, 2014 Share Posted May 28, 2014 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(): http://www.php.net/mysqli_error Also when posting code, please surround it with tags. It makes your posts and code easier to follow. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted May 28, 2014 Share Posted May 28, 2014 Ah, it looks like the arguments for mysqli_query() need to be reversed. More information can be found here: http://www.php.net/mysqli_query Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted May 28, 2014 Share Posted May 28, 2014 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. Quote Link to comment Share on other sites More sharing options...
selliottsxm Posted May 28, 2014 Author Share Posted May 28, 2014 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 Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted May 28, 2014 Share Posted May 28, 2014 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); Quote Link to comment Share on other sites More sharing options...
selliottsxm Posted May 28, 2014 Author Share Posted May 28, 2014 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... Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted May 28, 2014 Share Posted May 28, 2014 [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. Quote Link to comment Share on other sites More sharing options...
selliottsxm Posted May 28, 2014 Author Share Posted May 28, 2014 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. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted May 28, 2014 Share Posted May 28, 2014 Try "$row['movie_id'] instead of $r['movie_id'] Quote Link to comment Share on other sites More sharing options...
selliottsxm Posted May 28, 2014 Author Share Posted May 28, 2014 Thanks ginerjim, should have seen that. The array still isn't populating I think. Even a simple echo with just a scene id doesn't work Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 28, 2014 Share Posted May 28, 2014 @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. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted May 28, 2014 Share Posted May 28, 2014 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. Quote Link to comment Share on other sites More sharing options...
selliottsxm Posted May 28, 2014 Author Share Posted May 28, 2014 You are right, the query isn't running. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted May 28, 2014 Share Posted May 28, 2014 I'm guessing that either movie_id is not the field name or it is not defined as numeric value. Quote Link to comment Share on other sites More sharing options...
Solution selliottsxm Posted May 28, 2014 Author Solution Share Posted May 28, 2014 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> ';} Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.