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

时间:2010-11-14 14:11:56

标签: php mysql

如何从PHP和MySQL中读取和读取数据?我尝试了以下方法:

$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);

它输出,例如:

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

当我检查id = 1时,我发现它是我通过SQL终端放置的其他用户而没有电子邮件fdjisadds@as.com。

1 个答案:

答案 0 :(得分:1)

嗯,显然$row = mysqli_fetch_array($link, $useridquery)获取了一系列结果。然后print_r($row)将该数组打印到输出流并返回true,并将1连接到字符串。

从PHP手册

  

如果给定字符串,整数或浮点数,则将打印值本身。如果给定一个数组,则值将以显示键和元素的格式显示。类似的符号用于对象。   当return参数为TRUE时,此函数将返回一个字符串。否则,返回值为TRUE。

所以试试这个:

// 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]

修改 关于问卷的最后评论:

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

请注意,基于您提供的有限洞察力,这可能会破坏您的其他逻辑。