Jump to content

mysql fetch does not work


Guest

Recommended Posts

<?php



require("connection.php");




$query = 'SELECT * FROM job_post WHERE id_jobpost= :Id';
$start = $bdd->prepare($query);
$start->execute(array(':Id' => $_GET['id_jobpost'])); 
$result = $start->fetch();

foreach ($result as $results) {
    $jobtitle = $result['jobtitle'];
    $id_jobpost = $result['id_jobpost'];
    $description = $result['description'];
    
}


?> 

https://prnt.sc/uk3913

Whats wrong with this code

Link to comment
Share on other sites

require("connection.php");

$query = 'SELECT * FROM job_post WHERE id_jobpost = :id';
$start = $bdd->prepare($query);
$start->execute(array(':id' => $_GET['id_jobpost'])); 
$result = $start->fetchAll();

foreach ($result as $results) {
    $jobtitle = $result['jobtitle'];
    $id_jobpost = $result['id_jobpost'];
    $description = $result['description'];
    
}


?>   

 

when I use fetchAll it doesn't work either

Link to comment
Share on other sites

3 minutes ago, requinix said:

Check your variable names.

By the way, when a word is plural that normally means there is more than one, and the singular version is when there is just one.

<?php



require("connection.php");




$query = 'SELECT * FROM job_post WHERE id_jobpost = :Id';
$start = $bdd->prepare($query);
$start->execute(array(':Id' => $_GET['id_jobpost'])); 
$result = $start->fetch();

foreach ($result as $results) {
    $jobtitle = $result['jobtitle'];
    $id_jobpost = $result['id_jobpost'];
    $description = $result['description'];
    
}
<?php if (isset($jobtitle)) { echo $jobtitle; } ?>

https://prnt.sc/uk4652

:  Undefined index: id_jobpost in 

:  Invalid argument supplied for foreach() in 

Link to comment
Share on other sites

in your previous thread - https://forums.phpfreaks.com/topic/311357-problem-with-mysql-data-to-html-table/ you had code that successfully fetched and displayed one row of data. based on the sql query you are showing now, you would want to use that same design pattern, not the design pattern that you were given that operates on multiple rows of data.

1 hour ago, Endrick said:

Whats wrong with this code

based on your statements, edit: and the undefined index error you just posted, the get input probably doesn't contain what you think. your code should ALWAYS -

  1. validate all inputs before using them. if the get input isn't set or is empty (after being trimmed and cast as an integer), that's a user error and you should set up and display a message telling the user what was wrong with what they did or didn't do.
  2. list out the columns you are selecting so that anyone reading the query/code will know what you are trying to do without needing to know your database table definition.
  3. test if a select query matched any data before trying to use that data. if the query was successful (no errors), but didn't match any data, you should set up and display a message telling the user that there is no data to display, rather than trying to echo data that doesn't exist.
  4. don't use a loop to fetch the result from a query that you expect to match at most one row of data. just fetch that single row of data (refer back to your original code in the previous thread i linked to). your current loop, since $result contains a single fetched row of data, is looping over all the columns in that single row of data and is meaningless. 
  5. don't copy variables to other variables. just use the original variable that data is in, like you were doing in the original code in the previous thread i linked to.

short answer - 1.) you need to provide a good User eXperience (UX), by letting the user know if they did something wrong or that there is no (expected) data to display, and 2) code you had about a month ago did what you are trying to do now. learn from and build upon things you did before.

Edited by mac_gyver
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.