Get statement not working

I am trying to read out a database row by id but it is not passing my (isset) if statement. I know the fetch all funtion works as I can var_dump the data but when I run it through the (fetch data)function. No data comes through. What am I missing?

class Article {
	public function fetch_all(){
		global $pdo;
		

		$query= $pdo->prepare("SELECT * FROM article");
		$query->execute();
		
		return $query->fetchAll();
	}
	
	public function fetch_data($article_id)
	{
		global $pdo;
		
		$query = $pdo->prepare("SELECT * FROM article WHERE article_id =?");
		$query->bindValue(1, $article_id);
		$query->execute();
		return $query->fetch();
	}
	
}

then I am calling on another page -

include_once('includes/connection.php');
include_once('includes/article.php');

///$article = new Article;
//$articles = $article->fetch_data();
///var_dump($article);
if (isset($_GET['id'])){
	$id = $_GET['id'];
	$data = $article->fetch_data($id);
	

The new Article call works but its never getting the ID to pass. Any help would be amazing. I need this for my final tomorrow.

Thank You,
Devin

By the way, I noticed that I had commented out the $article = new article; that is enabled when I am testing as I know I need that function.
Thank you

So you first need to be more specific about where the code doesn’t work as expected. Your isset() is not triggered? Then the key within the URL is missing, have a look at everything you got with var_dump($_GET, $_POST). Your fetch_data doesn’t return the expected data? var_dump($article_id) and use this value for the placeholder in the SQL statement, run the statement in a tool like PHPMyAdmin.

So my fetch doesn’t find the correct data or is null. I think I am missing something.
Function is below (not retrieving data)

public function fetch_data($article_id)
{
	global $pdo;
	
	$query = $pdo->prepare("SELECT * FROM article WHERE article_id =?");
	$query->bindValue(1, $article_id);
	$query->execute();
	return $query->fetch();
}

I am running this

<?php include_once('includes/connection.php'); include_once('includes/article.php'); $article = new Article; $articles = $article->fetch_data(); var_dump($articles_id); if (isset($_GET['id'])){ $id = $_GET['id']; $data = $article->fetch_data($id); ?>

The VAR dump gives me this
Warning : Missing argument 1 for Article::fetch_data(), called in D:\xampp\htdocs\Sol\article_show.php on line 6 and defined in D:\xampp\htdocs\Sol\includes\article.php on line 14

Notice : Undefined variable: article_id in D:\xampp\htdocs\Sol\includes\article.php on line 19

Notice : Undefined variable: articles_id in D:\xampp\htdocs\Sol\article_show.php on line 8
NULL

Line 19 refers to my $query->BindValue statement in the function.

It appears that $_GET is not retrieving any data but I am not sure how to get it. I was actually following something online

So what’s unclear about this? You’re relying on $_GET['id'] AFTER calling the function - and it’s at least not even the same name.

Hey Chris,

I get what you are saying and bear with me as I am trying to figure this out. The problem is the $GET[‘id’] as I have no idea where its getting this from as its not calling the function until it gets the id.

Any suggestions would be very helpful. I followed this tutorial if this helps because supposedly it works for this person.

Any recommended changes in my code would be greatly appreciated.

$_GET values come from the url. Since this is a CMS, there would have been a previous operation where you retrieve information about all (or a searched for sub-set) of the articles and produced href links with the article id and article title. Clicking on one of the links would then supply the article id to use to fetch the data for a specific article.

of course it does in your code:

$article->fetch_data();

So I placed that in the code to see if it makes a difference. In the tutorial it just shows
$article = new Article;

if (isset($_GET[‘id’])){
$id = $_GET[‘id’];
$data = $article->fetch_data($id);

print_r($data);

So from my understanding (very limited as you can see), if looks for $_Get [id] to be set and then goes to the fetch data function. The only call for data is the connection inside of fetch data so I don’t see where ID can be set yet. I think that’s my confusion because it works like this in the tutorial but not in my code…

You won’t get the argument 1 missing ... message then. What does var_dump($_GET, $_POST) show? At least any ID must be in the URL in the id key.

So I am getting array(0) { } array(0) { } when running var_dump($_GET, $_POST);. No id present in either. not sure where to go from here since the read is in the function

Perhaps you missed this -

What is your code producing the links to the individual articles?

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service