[英]How to display data from specific table row columns in the database with MySQLI vs MySQL
I am still really new to MySQL and even newer to MySQLI. 我还是MySQL的新手,甚至还不熟悉MySQLI。 I am trying to update my website with MySQLI and I can't seem to fix this last issue.
我正在尝试使用MySQLI更新我的网站,但似乎无法修复最后一个问题。
My website is a single page site with 50 categories on the page. 我的网站是一个单页网站,页面上有50个类别。 Each category has a category title.
每个类别都有一个类别标题。 I have a table in the database with all 50 category titles and id's.
我在数据库中有一个表,其中包含所有50个类别标题和ID。 I was using MySQL/PHP to display each category title at the top of each category by choosing the row and column specifically.
我使用MySQL / PHP通过选择行和列来在每个类别的顶部显示每个类别的标题。 Here is my code..
这是我的代码。
The Query: 查询:
$query_meta = mysql_query("SELECT * FROM `categories`", $connection);
if (!$query_meta) {
die("Connection Failed: " . mysql_error());
}
$meta_query_result = array();
while( $row = mysql_fetch_assoc($query_meta) ) {
$meta_query_result[$row['id']] = array('url' => $row['url'], 'name' => $row['name'], 'title' => $row['title']);
}
The Echo Statement: 回声声明:
<h2><?php echo $meta_query_result[1]['name']; ?></h2>
<h6><?php echo $meta_query_result[1]['title']; ?></h6>
This allows me to choose the row and the column I want to display. 这使我可以选择要显示的行和列。
How can I redo this in MySQLI? 如何在MySQLI中重做此操作? Thanks in advance!
提前致谢!
you need to add the connection on the opposite side with mysqli. 您需要在与mysqli相反的一侧添加连接。
so // mysql_query($q, $db_connnect);
所以//
mysql_query($q, $db_connnect);
// becomes // mysqli_query($db_connnect, $q);
//变成// //
mysqli_query($db_connnect, $q);
Of course you should read documentation , because it looks the same (on first glance) as mysql but has little difference... 当然,您应该阅读文档 ,因为它看起来与mysql相同(乍看之下),但几乎没有区别...
Next example should be good for you: 下一个示例应该对您有利:
<?php
$db = new mysqli('localhost', 'root');
if ($db->connect_errno) {
echo 'Connect failed: '.$db->connect_error;
exit();
}
$db->select_db('test');
$cursor = $db->query('SELECT * FROM categories');
if ($db->error) {
echo $db->error.PHP_EOL;
}
$meta_query_result = [];
while($row = $cursor->fetch_assoc()){
$meta_query_result[$row['id']] = [
'url' => $row['url'],
'name' => $row['name'],
'title' => $row['title'],
];
}
$cursor->close();
Notice:And remember that mysql_query is deprecated as of PHP 5.5.0, use PDO instead. 注意:请记住,从PHP 5.5.0开始不建议使用mysql_query,请改用PDO。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.