0

looked around, saw a lot of MySQL answers but not MySQLi.. Im attempting to return 1 line of my choosing. at the moment I can return only the first line.

What im trying to get to is, have my main database be linked by ID, when you click the ID, a closer look at the record is on another page..

<?php

$connect = mysqli_connect("localhost", "root", "", "mydb");
$query = "SELECT name, surname FROM info ORDER BY id";
$record = mysqli_query($connect, $query);
@$num_results = mysqli_num_rows($record);

$row = mysqli_fetch_assoc($record);

$fname = $row['name'];
$surname  = $row['surname'];

print $fname;
print $surname;



?>
  • 2
    Well assuming you want `id` 2 then `SELECT name, surname FROM info WHERE id=2` – AbraCadaver Jul 13 '17 at 19:33
  • Jeez, that was simple.. Hmm okay so keep in mind im a beginner.. if I wanted to make my table have each ID be a link, and when you click the ID you go to the record page.. the way I would code that would be... to turn id= Variable? – Charlesx54321 Jul 13 '17 at 19:36
  • 1
    Don't use `@` while developing, that just hides useful errors from you. Also you need to use `fetch` in a `while` if you want more than 1 record. `where` is how to limit the results of a query, and if using a variable parameterize the query. `SELECT name, surname FROM info WHERE id= ?` http://php.net/manual/en/mysqli.quickstart.prepared-statements.php – chris85 Jul 13 '17 at 19:38
  • Thanks @AbraCadaver and chris85 any advice on my second question? – Charlesx54321 Jul 13 '17 at 19:46
  • `SELECT name, surname FROM info WHERE id= ?` is the actual query. See the link I provided, you bind the `GET` parameter. – chris85 Jul 13 '17 at 19:50
  • oh wow, I get ya now. thanks chris!!! – Charlesx54321 Jul 13 '17 at 19:53
  • Guys sorry to bother again, getting a bit confused.. I get the query but.. how do I create this variable that will take x variable from my table and use it on another page.. how does ? = a specific 'x' what am I echoing? – Charlesx54321 Jul 13 '17 at 20:12
  • The page here is the page being loaded from the link, or the page the link should be built on? – chris85 Jul 13 '17 at 20:16
  • If an answer solved your problem, consider accepting the answer. Here's how http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work then return here and do the same with the tick/checkmark till it turns green. This informs the community, a solution was found. Otherwise, others may think the question is still open and may want to post (more) answers. You'll earn points and others will be encouraged to help you. *Welcome to Stack!* – Jay Blanchard Jul 13 '17 at 20:55

1 Answers1

0

In order to do what you're asking, first create a list of users:

$connect = mysqli_connect("localhost", "root", "", "mydb");
$query = "SELECT name, surname FROM info ORDER BY id";
$record = mysqli_query($query, $connect);

while($row = mysqli_fetch_assoc($record)){
    $user = $row['name'] . ' ' . $row['surname'];
    echo '<a href="user.php?uid=' . $row['id'] . '">' .$user . '</a></br>';
}

The will create a list of all your users which look like:

<a href="user.php?uid=1">Bart Simpson</a></br>
<a href="user.php?uid=2">Matt Damon</a></br>

And so on.

When you click the user's link in the original page, it should be processed by the code in user.php:

$connect = mysqli_connect("localhost", "root", "", "mydb");
$query = "SELECT name, surname FROM info WHERE id = ?"; // returns one line identified by id - you can use something else if you're guarateed the value is unique in your table
$stmt = mysqli_prepare($connect, $query);
mysqli_stmt_bind_param($stmt, 'i', $_GET['uid']);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $name, $surname);
mysqli_stmt_fetch($stmt);

I'll bet you can guess what happens now, can't you? That's right, you can echo out the data for the individual user on this page:

$user = $name . ' ' . $surname;
echo $user;

NOTES:

  1. The connection code could be placed in a separate file and included in pages where needed.
  2. You could write a function to handle every query you write.
  3. In order to prevent the possibility of SQL Injection I have used prepared statements for MySQLi. Even escaping the string is not safe!
  4. Generally I would be a lot more consistent with my coding, performing queries the same way each and every time. doing so will reduce troubleshooting time as well as making your code easier for others to read.
Jay Blanchard
  • 32,731
  • 15
  • 70
  • 112
  • Awesome stuff @Jay Blanchard But the way I made my database is that so other people can come in and leave a new record. so me making a list of < a href links to my different users isn't applicable right? one page will be an ever changing database the other page will be a zoom in of a record. does your explanation above cover this as well? – Charlesx54321 Jul 14 '17 at 18:20
  • Yes, it sure does. – Jay Blanchard Jul 14 '17 at 18:22
  • hmm okay so, I put your second block of code on its own user.php... as well as the echo call right under it... Back on my main I added your code, expecting to get IDs in link formation... but now im getting the ol "mysqli_fetch_assoc() expects paramer 1 to be mysqli_result, Boolean given" Error.. – Charlesx54321 Jul 14 '17 at 18:28
  • You need to add error checking to the code to make sure you're connecting to the database properly before running the query. I made a mistake by not changing `mysqli_query(0` when I copied your code. The query comes first, then the connection. – Jay Blanchard Jul 14 '17 at 18:30
  • sorry, my beginner understanding it acting up again. my connection should be fine, it worked before with my code above. The only thing I changed would be getting rid of everything after my @ and pasting in your While. – Charlesx54321 Jul 14 '17 at 18:37
  • FORGET IT! I made a typo! the links work. now I have to figure out user.php as im getting errors. – Charlesx54321 Jul 14 '17 at 18:39
  • well one, you used GET uid, did you mean id?... and me error is Parse error: syntax error, unexpected 'name' (T_STRING), expecting ',' or ';' ... my echo is... echo '$user = $row['name'] . ' ' . $row['surname']'; – Charlesx54321 Jul 14 '17 at 18:47
  • No - it is `uid` because that is what I placed in the URL. You should `echo $user;` *after the last line of my code above. I'll edit @Charlesx54321 – Jay Blanchard Jul 14 '17 at 18:49
  • Awesome! now 3 new errors ;D.... all of the 3 lines... mysqli_stmt_bind, mysqli_stment_execute, mysqli_fetch_array are All getting errors... the first 2 "expects parameter 1 to be mysqli_stmt" and the 3rd "expects parameter 1 to be "mysqli_result" – Charlesx54321 Jul 14 '17 at 18:55
  • OK - now check errors in your connection, because those errors indicate a problem with the query and since the query is good it has to be the connection. – Jay Blanchard Jul 14 '17 at 18:57
  • wait, @Jay, shouldn't we be adding the $connect somewhere? – Charlesx54321 Jul 14 '17 at 19:00
  • Oooops, my bad. I typed all of this out when I wrote the answer and I forgot the `prepare()`. I have updated the code again. ¯\\_(ツ)_/¯ – Jay Blanchard Jul 14 '17 at 19:01
  • did a little if statement to check connect and check mysqli select db, and neither came back negative. – Charlesx54321 Jul 14 '17 at 19:02
  • Ok, we're almost there ;D... mysqli_fetch_array() expects parameter 1 to be mysqli_result – Charlesx54321 Jul 14 '17 at 19:06
  • that's exactly what I changed before asking, but I got the same error! – Charlesx54321 Jul 14 '17 at 19:10
  • OK - this is one of the reasons I build functions to do things. MySQLi requires so much more verbosity than PDO and I forgot everything (binding results, fetching, preparing....all.the.things) when I typed out this answer. The code above is fully tested :) Please note all of the changes. I'm so sorry for being an idiot @Charlesx54321 – Jay Blanchard Jul 14 '17 at 19:33
  • Beautiful @Jay, working great! thank you for taking all of this time to help me!!! appreciate it greatly!! – Charlesx54321 Jul 14 '17 at 19:44
  • Glad to help and brush up at the same time @Charlesx54321 – Jay Blanchard Jul 14 '17 at 19:45