DarkxPunk Posted January 13, 2013 Share Posted January 13, 2013 Hey everyone, I am not sure whether this should be posted in PHP or SQL but here is what I have: <?php$db_host = '###';$db_name = '###';$db_user = '###';$db_pass = '###';$con = @mysql_connect($db_host , $db_user , $db_pass) or die(mysql_error());@mysql_select_db($db_name) or die(mysql_error());$articles = @mysql_query(SELECT article_content FROM articles);?><!doctype html><html><head><title>Search Protocol</title></head><body><?phpecho ($articles);?></body></html><?php @mysql_close($con); ?> Now I know its connecting and I even ran that exact query using my IDE, and it works. But for some reason I am getting: Parse error: parse error in - on line 8 What am I missing? Link to comment Share on other sites More sharing options...
Don E Posted January 13, 2013 Share Posted January 13, 2013 Trying putting the select query in quotes for the mysql_query function. ie: "SELECT article_content FROM articles" Link to comment Share on other sites More sharing options...
DarkxPunk Posted January 13, 2013 Author Share Posted January 13, 2013 When I do that I get Resource id #2 which I can safely say is not the content in the table. Link to comment Share on other sites More sharing options...
jeffman Posted January 13, 2013 Share Posted January 13, 2013 (edited) A resource is what mysql_query returns. Look at the examples here to see how to use it. Edited January 13, 2013 by Deirdre's Dad Link to comment Share on other sites More sharing options...
DarkxPunk Posted January 13, 2013 Author Share Posted January 13, 2013 (edited) Solved it... After tons of research. <?php $db_host = '###'; $db_user = '###'; $db_pass = '###'; $db_name = '###'; $con = mysqli_connect($db_host, $db_user, $db_pass, $db_name); $request = mysqli_query($con, "SELECT article_content FROM articles"); $result = mysqli_fetch_assoc($request);?><!doctype html><html> <head> <title>Search Protocol</title> </head> <body><?php echo $result['article_content'];?> </body></html><?php mysqli_close($con); ?> Now I added more content to the db, but it only displayes the first row... What is going on? Edited January 13, 2013 by DarkxPunk Link to comment Share on other sites More sharing options...
dsonesuk Posted January 13, 2013 Share Posted January 13, 2013 You have to loop through each record that meets the critera set out by the SQL query, while or foreach as shown here http://w3schools.com/php/php_mysql_select.asp Link to comment Share on other sites More sharing options...
DarkxPunk Posted January 13, 2013 Author Share Posted January 13, 2013 Thank you, now I wanted to quickly look over a var dump to see how to maybe select an individual row but I get this array(2) { [0]=> string(30) "This is a test article." ["article_content"]=> string(30) "This is a test article." } array(2) { [0]=> string(25) "Other test content" ["article_content"]=> string(25) "Other test content" } I am using this $request = mysqli_query($con, "SELECT article_content FROM articles");?><!doctype html><html> <head> <title>Search Protocol</title> </head> <body><?php while ($row = mysqli_fetch_array($request)) { var_dump($row); }?> </body></html> there is nothing defining each row... How could I just pull this info and then throw onto the page what I want? Link to comment Share on other sites More sharing options...
dsonesuk Posted January 14, 2013 Share Posted January 14, 2013 Not sure what you want? to look for specific record you would look for a unique reference usually an id reference to identify that specific record $request = mysqli_query($con, "SELECT article_content FROM articles WHERE ArtId = 5");$request = mysqli_query($con, "SELECT article_content FROM articles WHERE Art_Title = 'Toronto'"); The search value can be from form search using $_POST and this can be assigned to a search variable and used within the SQL if(isset($_POST['search_item']) && !empty($_POST['search_item'])){$request = mysqli_query($con, "SELECT article_content FROM articles WHERE Art_Title = '$_POST['search_item']'"); // show to specific search item}else{$request = mysqli_query($con, "SELECT article_content FROM articles"); //Show ALL} Link to comment Share on other sites More sharing options...
DarkxPunk Posted January 14, 2013 Author Share Posted January 14, 2013 That's great actually I will need that in the future. But specifically I usually see arrays show like index[0] "first row" index[1] "second row", but it shows both as 0... Link to comment Share on other sites More sharing options...
birbal Posted January 14, 2013 Share Posted January 14, 2013 check the data of var_dump() closely the "0" is refering to two different array. you can specify the index to point out particular data to work on each iteration. Link to comment Share on other sites More sharing options...
DarkxPunk Posted January 14, 2013 Author Share Posted January 14, 2013 I understand exactly what it is doing, and my question is why? Should it not put all the results into one array? Why is it not, and how do I make it do so? Link to comment Share on other sites More sharing options...
justsomeguy Posted January 14, 2013 Share Posted January 14, 2013 You can put all of the rows in one array if you want to, right now you're just printing each row and moving to the next. You can push all of those rows into one array, and then each row will have its own index. You keep overwriting the row and printing it, you're not saving it or anything. Link to comment Share on other sites More sharing options...
DarkxPunk Posted January 17, 2013 Author Share Posted January 17, 2013 I have been researching and keep finding the same solution to my problem, but when I implement them I don't get the results. $con = mysqli_connect($db_host, $db_user, $db_pass, $db_name); $request = mysqli_query($con, "SELECT article_content FROM articles");?><!doctype html><html> <head> <title>Search Protocol</title> </head> <body><?php $result = array(); if(mysqli_num_rows($request) > 0) { while($rows = mysqli_fetch_assoc($request)) { $result[] = $rows['article_content']; echo $result; } } With this I get, arrayarray. What am I doing wrong? Link to comment Share on other sites More sharing options...
dsonesuk Posted January 17, 2013 Share Posted January 17, 2013 because all you are echoing is the object of array, and not its contents. 2 option use $count or print_r() $result = array(); $count=0; if(mysqli_num_rows($request) > 0) { while($rows = mysqli_fetch_assoc($request)) { $result[] = $rows['article_content']; echo $result[$count].'<br>'; $count++; } print_r ($result); } Link to comment Share on other sites More sharing options...
DarkxPunk Posted January 18, 2013 Author Share Posted January 18, 2013 Okay I got it working and am begining to understand. Now what I would like if I could associate my content to say its date that I pull off the database. The reason is so I could simply type echo $row['date'] and get the associated content.I found it once when I was looking for what you gave me, but now after a few hours of searching I can't find it X(Thanks for any help. Link to comment Share on other sites More sharing options...
DarkxPunk Posted January 20, 2013 Author Share Posted January 20, 2013 Nothing? Link to comment Share on other sites More sharing options...
dsonesuk Posted January 20, 2013 Share Posted January 20, 2013 I don't know what you are looking for? if the date has content stored along with it within another field, you would just do sql query to search through date field, and retrieve content that is stored in the content field which has that same date you set by the sql where condition. OR as the while loop searches through each records do an if condition to retrieve record, that that matches the date you specify. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now