简体   繁体   中英

retrieving data from a mysql database

I have a sign up page that has input boxes where you would enter your name, email address and password. After submitting that form there is a sign in page where it checks to see if you are in the database by SELECT * FROM users WHERE name = '$_POST[name]' AND email = '[email]' . This all works fine, but when you actually get into the site on your account i want to have a message at the top that says 'Welcome back (name from database). To do this i used $name = mysql_query("SELECT first_name FROM users WHERE email = $email"); and had <?php echo $name ?> at the top. This won't work though. Why?

"mysql_query" has been deprecated, you want to use "mysqli_query" now. See here: http://php.net/manual/en/function.mysql-query.php

To answer your question, performing the query creates an array, you need to followup on that by fetching from the array.

If you are doing this in a procedural style then the final code should look something like this:

$link = mysqli_connect("localhost", "my_user", "my_password"); // connect to the database
$query="SELECT first_name FROM users WHERE email = '".$email."'"; // create the query. Note the quotes arounds the email variable.
$result=mysqli_query($link, $query); // run the query
$row=mysqli_fetch_array($result); // fetch the array that is returned from the query
$name=$row['first_name']; // assign the first_name field to the $name variable

echo "Hello ".$name.","; // output the variable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM