Jump to content

PHP not selecting from MySQL


DarkxPunk

Recommended Posts

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

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 by DarkxPunk
Link to comment
Share on other sites

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

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

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

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

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

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

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...