简体   繁体   English

PHP + MySQL获取数据并将其放入数据库

[英]PHP+MySQL getting and putting data to database

How can I put and read data from PHP and MySQL? 如何放置和读取PHP和MySQL中的数据? I tried the following: 我尝试了以下方法:

$pwdcrypt = SHA1($pwd);
echo "Now we put some data to the database!";
$userregisterquery="INSERT INTO user (email, password) VALUES ('$email', '$pwdcrypt')";
echo $userregisterquery;
mysqli_query($link, $userregisterquery);

$usergetdataquery="SELECT email, password FROM user WHERE email='$email'";
echo $usergetdataquery.'</br>'; 
$result =  mysqli_query($link, $usergetdataquery);
$row = mysqli_fetch_row($result);
print_r($row);

// get the user_id
$useridquery="SELECT user.id FROM user WHERE email = '$email'";
echo $useridquery.'</br>'; 
$result = mysqli_query($link, $useridquery);
$row = mysqli_fetch_array($result);  
echo "the result is:".print_r($row);
mysqli_close($link);

It outputs, for example: 它输出例如:

Now we put some data to the database!INSERT INTO user (email, password) VALUES ('fdjisadds@as.com', 'cf860129c95e6afcdb9f9a390354915cde81c40c')We put email: fdjisadds@as.com and password: cf860129c95e6afcdb9f9a390354915cde81c40c to the database.SELECT email, password FROM user WHERE email='fdjisadds@as.com'We get: 1

As I checked that id=1, I found that it is some other user I put there via SQL-terminal and no email fdjisadds@as.com. 当我检查id = 1时,我发现它是我通过SQL-terminal放置的其他用户,没有电子邮件fdjisadds@as.com。

Well, obviously $row = mysqli_fetch_array($link, $useridquery) fetches an array of results. 好吧,显然$row = mysqli_fetch_array($link, $useridquery)获取结果数组。 print_r($row) is then printing that array to the output stream and returns true which is concatenated to the string as 1 . 然后print_r($row)将那个数组打印到输出流,并返回true ,它与字符串连接为1

From the PHP manual 从PHP手册

If given a string, integer or float, the value itself will be printed. 如果给出字符串,整数或浮点数,则值本身将被打印。 If given an array, values will be presented in a format that shows keys and elements. 如果给定数组,则值将以显示键和元素的格式显示。 Similar notation is used for objects. 类似的符号用于对象。 When the return parameter is TRUE, this function will return a string. 当return参数为TRUE时,此函数将返回一个字符串。 Otherwise, the return value is TRUE. 否则,返回值为TRUE。

So try this instead: 因此,请尝试以下操作:

// This would concatenate the print_r's result to the string
echo "the result is:".print_r($row, true);

// Or better yet, use the following
echo "the result is:".$row[0]

EDIT Regarding the questionaire's last comment: 编辑关于问卷调查者的最后评论:

ALTER TABLE `user`
CHANGE COLUMN `id` `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT;

Be aware, though, that based on the limited insight you provided, this could break some other logic of yours. 但是请注意,基于您提供的有限见解,这可能会破坏您的其他逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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