gpt4 book ai didi

PHP SQL,在PHP中从具有多个条件的多个表中获取多个数据

转载 作者:行者123 更新时间:2023-11-30 01:22:33 24 4
gpt4 key购买 nike

我目前正在制作一个基于 PHP 和 mySQL 的时间表网站,我遇到了一个问题,我将在具有多个条件的表中提取多个数据,以下是一些代码:

$db_connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$uid = $_SESSION['user_id'];


$data_query = "SELECT users.user_name, jobs.job_code, jobs.job_desc, jobs.job_client, jobs.job_year, jobs.job_month, jobs.job_category, job_taker.job_hours ".
"FROM jobs,job_taker,users ".
"WHERE 'login.jobs.job_id' = 'login.job_taker.job_id' AND 'login.users.user_id' = ('.$uid.')".
"ORDER BY 'login.jobs.job_id' DESC";
$data_result = mysqli_query($db_connection,$data_query) or die(mysql_error());

这里是我展示结果的地方:

<?php
while($info = mysqli_fetch_array($data_result)) {
?>
<tr>
<td> <input type="checkbox" name="<?php echo $info['job_code']?>" id="<?php echo $info['job_code']?>" value="<?php echo $info['job_code']?>" /></td>
<td><?php echo $info['user_name'] ?></td>
<td><?php echo $info['job_code'] ?> </td>
<td><?php echo $info['job_client'] ?> </td>
<td><?php echo $info['job_year'] ?> </td>
<td><?php echo $info['job_month']?> </td>
<td><?php echo $info['job_date']?> </td>
<td><?php echo $info['job_category']?> </td>
</tr>
<?php } ?>

这是我的数据库中的 SQL 转储:

CREATE TABLE IF NOT EXISTS `jobs` (
`job_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`job_code` varchar(32) NOT NULL,
`job_desc` varchar(500) NOT NULL,
`job_client` varchar(500) NOT NULL,
`job_year` int(11) NOT NULL,
`job_month` int(11) NOT NULL,
`job_date` int(11) NOT NULL,
`job_category` varchar(25) NOT NULL,
PRIMARY KEY (`job_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

CREATE TABLE IF NOT EXISTS `job_taker` (
`user_id` bigint(20) unsigned NOT NULL,
`job_id` int(11) unsigned NOT NULL,
`job_hours` int(11) unsigned NOT NULL,
PRIMARY KEY (`user_id`,`job_id`),
KEY `user_id` (`user_id`),
KEY `user_id_2` (`user_id`),
KEY `job_id` (`job_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `users` (
`user_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'auto incrementing user_id of each user, unique index',
`user_name` varchar(64) COLLATE utf8_unicode_ci NOT NULL COMMENT 'user''s name',
`user_position` varchar(25) COLLATE utf8_unicode_ci NOT NULL,
`user_status` varchar(25) COLLATE utf8_unicode_ci NOT NULL,
`user_password_hash` char(60) COLLATE utf8_unicode_ci NOT NULL COMMENT 'user''s password in salted and hashed format',
`user_email` varchar(64) COLLATE utf8_unicode_ci NOT NULL COMMENT 'user''s email',
`user_active` tinyint(1) NOT NULL DEFAULT '0' COMMENT 'user''s activation status',
`user_activation_hash` varchar(40) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'user''s email verification hash string',
`user_password_reset_hash` char(40) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'user''s password reset code',
`user_password_reset_timestamp` bigint(20) DEFAULT NULL COMMENT 'timestamp of the password reset request',
PRIMARY KEY (`user_id`),
UNIQUE KEY `user_name` (`user_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='user data' AUTO_INCREMENT=8 ;

ALTER TABLE `job_taker`
ADD CONSTRAINT `job_taker_ibfk_2` FOREIGN KEY (`job_id`) REFERENCES `jobs` (`job_id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `job_taker_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE;

我的问题是,当我尝试打开显示它的页面时,它根本没有显示任何数据。它没有给出任何 SQL 语法错误,只是纯空白数据页。

以下是我尝试echo $data_result时的结果

Catchable fatal error: Object of class mysqli_result could not be converted to string in C:\xampp\htdocs\seclog\views\user_edit_job.php on line 11

有什么办法可以解决这个问题吗?

感谢任何试图提供帮助或阅读本文的人!

最佳答案

job_takerusers 设置条件,例如

job_taker.user_id=users.user_id

查询,

$data_query ="SELECT users.user_name, jobs.job_code, jobs.job_desc, 
jobs.job_client, jobs.job_year, jobs.job_month, jobs.job_category,
job_taker.job_hours FROM jobs,job_taker,users WHERE
jobs.job_id = job_taker.job_id AND users.user_id ='$uid' AND
job_taker.user_id=users.user_id ORDER BY 'login.jobs.job_id' DESC";

关于PHP SQL,在PHP中从具有多个条件的多个表中获取多个数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18394661/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com