androidd Posted May 1, 2013 Share Posted May 1, 2013 Ok so more on my lil comic app... (and all was going so well) GOAL: Trying to take a value from a form and create sql statement to look for that comic title in the DB and show what has been entered. (I.E search for Spider-Man -> Shows all Rows that Pertain to Spider-Man) So I can't figure out what is wrong with my code. Soon as I enter in my while statement my php failes however my query seems to be doing just fine. I've used the same kind of template on other pages and it works just fine the only difference is when I added a WHERE statement to my sql query. * NOTE: If I take the output of my query and plug it into mysql workbench I get the correct tables and results so I'm yeah kinda confused. * NOTE: The way I retrieve the $data variable is from a query from the form page which pulls all the comic names from the DB and displays in a drop down so the values that it passes are the exact same names / titles from the DB itself. <?php require('./scripts/con_db.php'); // Query DB for Comic_Names // $data = $_POST['comic_name']; $query = "SELECT * FROM `comic_db`.`comic_db` WHERE `comic_name`='$data'"; $com_sql = mysqli_query($comic_connect, $query); if (!$com_sql){ echo "Failed to connect to db" . mysqli_errorno(); } ?> <body> <p align="center"><img src="./images/comic_front.png" /></p> <table align="center"> <tr> <?php while($row = mysqli_fetch_array($com_sql)) { echo "<td>" .$row "</td>"; } ?> </tr> </table> <br /> <br /> <?php require('./scripts/footer.php'); ?> I should also say i've tried these other passes as well with no luck : <?php while($row = mysqli_fetch_assoc($com_sql)) { echo "<td>" .$row "</td>"; } ?> while($row = mysqli_fetch_assoc($com_sql)) { echo "<td>" .$row['comic_name'] "</td>"; } ?> <?php while($row = mysqli_fetch_assoc($com_sql)) { echo "<td>" .$row['$data'] "</td>"; } ?> I've also tried something I found in a book that says to do extract($row) then do my echo "<td>" .$row "</td>"; but that didn't work either so I'm missing something here is what the output for the SELECT string gives me as well from the browser which seems to be fine and again if I plug into mysql workbench it displays what I need.. SELECT * FROM `comic_db`.`comic_db` WHERE `comic_name`='Iron Man - V5' TY for help guys I've try to keep the ??'s down but I'm missing something (probably pretty obvious) so I need some guidence. Quote Link to comment https://forums.phpfreaks.com/topic/277477-hmm-seemed-to-work-for-other-pages/ Share on other sites More sharing options...
Q695 Posted May 1, 2013 Share Posted May 1, 2013 sounds like you need to put the $row into a while loop like while ($row=mysql_fetch_assoc(________________)){ //output table rows here } Quote Link to comment https://forums.phpfreaks.com/topic/277477-hmm-seemed-to-work-for-other-pages/#findComment-1427419 Share on other sites More sharing options...
computermax2328 Posted May 1, 2013 Share Posted May 1, 2013 $query = "SELECT * FROM `comic_db`.`comic_db` WHERE `comic_name`='$data'"; Take out the first comic_db and the period. Quote Link to comment https://forums.phpfreaks.com/topic/277477-hmm-seemed-to-work-for-other-pages/#findComment-1427420 Share on other sites More sharing options...
androidd Posted May 1, 2013 Author Share Posted May 1, 2013 sounds like you need to put the $row into a while loop like while ($row=mysql_fetch_assoc(________________)){ //output table rows here } Thought that was what I had in most of those examples or a variant. Could you explain what you mean a lil more maybe I'm missing it. or show me with the code provided. $query = "SELECT * FROM `comic_db`.`comic_db` WHERE `comic_name`='$data'"; Take out the first comic_db and the period. Nope usually I call the scheme then the table just cause I have multiple tables I use so I figured it's easier to make a global connection to the DB then when I need something from the DB Tables I could jump from one to the other without the need to make different connections.. Hence the `comic_db`.`comic_db` <--- also this works when I call the DB comic names to fill out the form as well as the page that does the INSERT statement that inputs the comic variables. So while I see what your saying didn't help in this situation. TY though. Quote Link to comment https://forums.phpfreaks.com/topic/277477-hmm-seemed-to-work-for-other-pages/#findComment-1427421 Share on other sites More sharing options...
Q695 Posted May 1, 2013 Share Posted May 1, 2013 the handling of row information is like so: $var_name=$row[table_column_name]; Quote Link to comment https://forums.phpfreaks.com/topic/277477-hmm-seemed-to-work-for-other-pages/#findComment-1427422 Share on other sites More sharing options...
androidd Posted May 1, 2013 Author Share Posted May 1, 2013 the handling of row information is like so: $var_name=$row[table_column_name]; ahh the spaces I see my bad... after testing it out still didn't work. :/ I will keep at it. TY Quote Link to comment https://forums.phpfreaks.com/topic/277477-hmm-seemed-to-work-for-other-pages/#findComment-1427426 Share on other sites More sharing options...
oaass Posted May 1, 2013 Share Posted May 1, 2013 First of all you are trying to echo out an array. What do you get if you echo $row[0] Second, your understanding of extract() is not correct. The extract() function works as follows $array = array( 'foo' => 'Hello world', 'bar' => 'Foo bar' ); extract($array); echo $foo; // Returns Hello world echo $bar; // Returns Foo bar Quote Link to comment https://forums.phpfreaks.com/topic/277477-hmm-seemed-to-work-for-other-pages/#findComment-1427440 Share on other sites More sharing options...
mac_gyver Posted May 1, 2013 Share Posted May 1, 2013 you probably have a non-printing character as part of your submitted form data that doesn't match anything when the query runs in the php code but when you copy/paste the echoed query statement, that non-printing character is stripped out. what is your code that is building the form? Quote Link to comment https://forums.phpfreaks.com/topic/277477-hmm-seemed-to-work-for-other-pages/#findComment-1427474 Share on other sites More sharing options...
androidd Posted May 4, 2013 Author Share Posted May 4, 2013 you probably have a non-printing character as part of your submitted form data that doesn't match anything when the query runs in the php code but when you copy/paste the echoed query statement, that non-printing character is stripped out. what is your code that is building the form? This is the page that queries the db for all the publishers in the collection then puts them in a drop down then submits to the form (OP coding) to query db to find all the rows for the desired publisher. // Query DB for Publisher Info // $query = "SELECT * FROM `comic_info_db`.`comic_name`"; $result = mysqli_query($comic_connect, $query); ?> <table height="10%"> <tr> <td> </td> </tr> </table> <p align="center"><img src="/images/comic_front.png" /></p> <form action="./comic_results.php" method="POST"> <table height=10% align="center"> <tr> <td> Comic Name: </td> <td> <select name="comic_name"> <option name="Null" value="--">--</option> <?php while($row = mysqli_fetch_array($result)) { echo "<option>" . $row['comic_name'] . "</option>\n"; } ?> </select> </td> <td><input name="submit" value="submit" type="submit" /></td> </tr> </table> </form> Quote Link to comment https://forums.phpfreaks.com/topic/277477-hmm-seemed-to-work-for-other-pages/#findComment-1428252 Share on other sites More sharing options...
mac_gyver Posted May 5, 2013 Share Posted May 5, 2013 your SELECT query for building the form is not using the same database as your query in the form processing code. Quote Link to comment https://forums.phpfreaks.com/topic/277477-hmm-seemed-to-work-for-other-pages/#findComment-1428277 Share on other sites More sharing options...
androidd Posted May 5, 2013 Author Share Posted May 5, 2013 your SELECT query for building the form is not using the same database as your query in the form processing code. yeah it pulls the publishers out of comic_info_db.comic_info and then it posts the data into comic_db.comic_db - As I get new publishers I wanted to add into db and it auto populate my forms. - The search would pull the exact publisher data that has been inputted into the data base and then post into the main comic database and the query would ask that table what publishers or titles I currently own. I did it this way also so that when I ask the DB what info it has it will have as part of the search the exact string that is contained in the DB Quote Link to comment https://forums.phpfreaks.com/topic/277477-hmm-seemed-to-work-for-other-pages/#findComment-1428313 Share on other sites More sharing options...
androidd Posted May 5, 2013 Author Share Posted May 5, 2013 should I be running the $_POST['comic_name'] through anything? Not sure if maybe when it asks the db for the info the search query passes the spaces and dashes as something else? I don't know how to ask this in google lol so I don't know if make sense sry. Quote Link to comment https://forums.phpfreaks.com/topic/277477-hmm-seemed-to-work-for-other-pages/#findComment-1428316 Share on other sites More sharing options...
Q695 Posted May 5, 2013 Share Posted May 5, 2013 Yes, run it through an if isset else to set the variable, because if you do it without the value it may create an error. Quote Link to comment https://forums.phpfreaks.com/topic/277477-hmm-seemed-to-work-for-other-pages/#findComment-1428429 Share on other sites More sharing options...
mac_gyver Posted May 6, 2013 Share Posted May 6, 2013 yeah it pulls the publishers out of comic_info_db.comic_info and then it posts the data into comic_db.comic_db the code you posted isn't doing that. it is trying to SELECT from comic_db.comic_db Quote Link to comment https://forums.phpfreaks.com/topic/277477-hmm-seemed-to-work-for-other-pages/#findComment-1428582 Share on other sites More sharing options...
androidd Posted May 7, 2013 Author Share Posted May 7, 2013 yeah I guess I should explain better MySQL DB Info: ---------------------------------- |comic_info_db.comic_info | < ------------- This houses all the publishers and comic names (ex Marvel, Spider-Man) ---------------------------------- --------------------------- |comic_db.comic_db | <-------------- Houses all current comics in the collection (This is where I'm trying to get results from) --------------------------- PhP Pages ----------------- |Comic_form | <----| Comic_input | <---|---------------------------------This group takes info and inputs it into the comic_db.comic_db as I get more comics (Works) ------------------ --------------------- | Comic_Search | <------ (Problem Area) * Part of the form queries comic_info_db.comic_info to get the comic names | Comic_Results | <------- (Problem Area) Shows the search results that is queried from comic_db.comic_db ---------------------- So basically what should be happening is I go to store get new comic goto comic_form input all info about comic (Publisher, Comic Name, Comic Number, Misc Info etc..) then input the data into db and then time goes by I go into comic search and select from a drop down (That was populated from the db) and outputs all the comics it finds in the DB pertaining to what was selected in drop down. IE. drop down menu selects Superior Spider-Man and shows results. I don't want to type my values and possible mess up so I already added all the publishers and all the comic names into the db those values are what populate in the drop downs, I did this to make sure I didn't have to deal with Spider Man, Spider-Man, spider man, spider-Man, etc.... it would show only the data that was in DB and search for that data exactly as it exists in the DB. What I posted in the posts a couple before this shows a query to comic_info_db.comic_info to get the pub to make drop down then that form sends it to the OP form to process results. so it's a circular thing hope this helps explain the structure of what I'm trying to do... Sorry so long. Quote Link to comment https://forums.phpfreaks.com/topic/277477-hmm-seemed-to-work-for-other-pages/#findComment-1428746 Share on other sites More sharing options...
androidd Posted May 7, 2013 Author Share Posted May 7, 2013 * sorry correction this for connects to comic_info_db.comic_names then should be asking comic_db.comic_db what comics do you have that match Spider-Man or Spawn etc... Quote Link to comment https://forums.phpfreaks.com/topic/277477-hmm-seemed-to-work-for-other-pages/#findComment-1428750 Share on other sites More sharing options...
Solution androidd Posted May 10, 2013 Author Solution Share Posted May 10, 2013 <?php while ($row = mysqli_fetch_assoc($com_sql)){ print "<tr>\n"; foreach ($row as $col=>$val){ print " <td>$val</td>\n"; } print "</tr>\n\n"; } ?> This is what worked. Found it in a book PhP5/MySQL Programming for Absolute Beginner (which I am ) XD Thanks for all the help everyone! Quote Link to comment https://forums.phpfreaks.com/topic/277477-hmm-seemed-to-work-for-other-pages/#findComment-1429416 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.